blob: 987293d03ebcf0e6bf1c6b81e8a4e68c7965e903 [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
Libinb11895c2013-08-21 08:50:39 +080019 * automatically managed. There are two worker pools for each CPU (one for
20 * normal work items and the other for high priority ones) and some extra
21 * pools for workqueues which are not bound to any specific CPU - the
22 * number of these backing pools is dynamic.
Tejun Heoc54fce62010-09-10 16:51:36 +020023 *
24 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Paul Gortmaker9984de12011-05-23 14:51:41 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/sched.h>
30#include <linux/init.h>
31#include <linux/signal.h>
32#include <linux/completion.h>
33#include <linux/workqueue.h>
34#include <linux/slab.h>
35#include <linux/cpu.h>
36#include <linux/notifier.h>
37#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060038#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070039#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080040#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080041#include <linux/kallsyms.h>
42#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070043#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020044#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070045#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050046#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070047#include <linux/rculist.h>
Tejun Heobce90382013-04-01 11:23:32 -070048#include <linux/nodemask.h>
Tejun Heo4c16bd32013-04-01 11:23:36 -070049#include <linux/moduleparam.h>
Tejun Heo3d1cb202013-04-30 15:27:22 -070050#include <linux/uaccess.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020051
Tejun Heoea138442013-01-18 14:05:55 -080052#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tejun Heoc8e55f32010-06-29 10:07:12 +020054enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 /*
Tejun Heo24647572013-01-24 11:01:33 -080056 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070057 *
Tejun Heo24647572013-01-24 11:01:33 -080058 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070059 * While associated (!DISASSOCIATED), all workers are bound to the
60 * CPU and none has %WORKER_UNBOUND set and concurrency management
61 * is in effect.
62 *
63 * While DISASSOCIATED, the cpu may be offline and all workers have
64 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080065 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070067 * Note that DISASSOCIATED should be flipped only while holding
68 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080069 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070070 */
Tejun Heo11ebea52012-07-12 14:46:37 -070071 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080072 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080073 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020074
Tejun Heoc8e55f32010-06-29 10:07:12 +020075 /* worker flags */
76 WORKER_STARTED = 1 << 0, /* started */
77 WORKER_DIE = 1 << 1, /* die die die */
78 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020079 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020080 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020081 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070082 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020083
Tejun Heoa9ab7752013-03-19 13:45:21 -070084 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
85 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020086
Tejun Heoe34cdddb2013-01-24 11:01:33 -080087 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070088
Tejun Heo29c91e92013-03-12 11:30:03 -070089 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020090 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091
Tejun Heoe22bee72010-06-29 10:07:14 +020092 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
93 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
94
Tejun Heo3233cdb2011-02-16 18:10:19 +010095 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
96 /* call for help after 10ms
97 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020098 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
99 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +0200100
101 /*
102 * Rescue workers are used only on emergencies and shared by
103 * all cpus. Give -20.
104 */
105 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700106 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoecf68812013-04-01 11:23:34 -0700107
108 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 * Structure fields follow one of the following exclusion rules.
113 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200114 * I: Modifiable by initialization/destruction paths and read-only for
115 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200116 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 * P: Preemption protected. Disabling preemption is enough and should
118 * only be modified and accessed from the local cpu.
119 *
Tejun Heod565ed62013-01-24 11:01:33 -0800120 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200121 *
Tejun Heod565ed62013-01-24 11:01:33 -0800122 * X: During normal operation, modification requires pool->lock and should
123 * be done only from local cpu. Either disabling preemption on local
124 * cpu or grabbing pool->lock is enough for read access. If
125 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200126 *
Tejun Heo822d8402013-03-19 13:45:21 -0700127 * MG: pool->manager_mutex and pool->lock protected. Writes require both
128 * locks. Reads can happen under either lock.
129 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700130 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700131 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700132 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700133 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700134 * WQ: wq->mutex protected.
135 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700136 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700137 *
138 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200139 */
140
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800141/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200142
Tejun Heobd7bdd42012-07-12 14:46:37 -0700143struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800144 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700145 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700146 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800147 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700148 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149
150 struct list_head worklist; /* L: list of pending works */
151 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700152
153 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700154 int nr_idle; /* L: currently idle ones */
155
156 struct list_head idle_list; /* X: list of idle workers */
157 struct timer_list idle_timer; /* L: worker idle timeout */
158 struct timer_list mayday_timer; /* L: SOS timer for workers */
159
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700160 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800161 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
162 /* L: hash of busy workers */
163
Tejun Heobc3a1af2013-03-13 19:47:39 -0700164 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700165 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700166 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700167 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800168
Tejun Heo7a4e3442013-03-12 11:30:00 -0700169 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700170 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
171 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700172
Tejun Heoe19e3972013-01-24 11:39:44 -0800173 /*
174 * The current concurrency level. As it's likely to be accessed
175 * from other CPUs during try_to_wake_up(), put it in a separate
176 * cacheline.
177 */
178 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700179
180 /*
181 * Destruction of pool is sched-RCU protected to allow dereferences
182 * from get_work_pool().
183 */
184 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200185} ____cacheline_aligned_in_smp;
186
187/*
Tejun Heo112202d2013-02-13 19:29:12 -0800188 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
189 * of work_struct->data are used for flags and the remaining high bits
190 * point to the pwq; thus, pwqs need to be aligned at two's power of the
191 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Tejun Heo112202d2013-02-13 19:29:12 -0800193struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700194 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200195 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200196 int work_color; /* L: current color */
197 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700198 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200199 int nr_in_flight[WORK_NR_COLORS];
200 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200201 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200202 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200203 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700204 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700205 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700206
207 /*
208 * Release of unbound pwq is punted to system_wq. See put_pwq()
209 * and pwq_unbound_release_workfn() for details. pool_workqueue
210 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700211 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700212 */
213 struct work_struct unbound_release_work;
214 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700215} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200218 * Structure used to wait for workqueue flush.
219 */
220struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700221 struct list_head list; /* WQ: list of flushers */
222 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200223 struct completion done; /* flush completion */
224};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo226223a2013-03-12 11:30:05 -0700226struct wq_device;
227
Tejun Heo73f53c42010-06-29 10:07:11 +0200228/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700229 * The externally visible workqueue. It relays the issued work items to
230 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700233 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700234 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200235
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700236 struct mutex mutex; /* protects this wq */
237 int work_color; /* WQ: current work color */
238 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800239 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700240 struct wq_flusher *first_flusher; /* WQ: first flusher */
241 struct list_head flusher_queue; /* WQ: flush waiters */
242 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200243
Tejun Heo2e109a22013-03-13 19:47:40 -0700244 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200245 struct worker *rescuer; /* I: rescue worker */
246
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700247 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700248 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700249
Tejun Heo6029a912013-04-01 11:23:34 -0700250 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
Tejun Heo4c16bd32013-04-01 11:23:36 -0700251 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700252
Tejun Heo226223a2013-03-12 11:30:05 -0700253#ifdef CONFIG_SYSFS
254 struct wq_device *wq_dev; /* I: for sysfs interface */
255#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700256#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200257 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700259 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700260
261 /* hot fields used during command issue, aligned to cacheline */
262 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
263 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700264 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Tejun Heoe904e6c2013-03-12 11:29:57 -0700267static struct kmem_cache *pwq_cache;
268
Tejun Heobce90382013-04-01 11:23:32 -0700269static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
270static cpumask_var_t *wq_numa_possible_cpumask;
271 /* possible CPUs of each node */
272
Tejun Heod55262c2013-04-01 11:23:38 -0700273static bool wq_disable_numa;
274module_param_named(disable_numa, wq_disable_numa, bool, 0444);
275
Viresh Kumarcee22a12013-04-08 16:45:40 +0530276/* see the comment above the definition of WQ_POWER_EFFICIENT */
277#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
278static bool wq_power_efficient = true;
279#else
280static bool wq_power_efficient;
281#endif
282
283module_param_named(power_efficient, wq_power_efficient, bool, 0444);
284
Tejun Heobce90382013-04-01 11:23:32 -0700285static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
286
Tejun Heo4c16bd32013-04-01 11:23:36 -0700287/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
288static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
289
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700290static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700291static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700292
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700293static LIST_HEAD(workqueues); /* PL: list of all workqueues */
294static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700295
296/* the per-cpu worker pools */
297static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
298 cpu_worker_pools);
299
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700300static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700301
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700302/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700303static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
304
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700305/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700306static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
307
Tejun Heod320c032010-06-29 10:07:14 +0200308struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400309EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300310struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900311EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300312struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200313EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300314struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200315EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300316struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100317EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530318struct workqueue_struct *system_power_efficient_wq __read_mostly;
319EXPORT_SYMBOL_GPL(system_power_efficient_wq);
320struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
321EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200322
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700323static int worker_thread(void *__worker);
324static void copy_workqueue_attrs(struct workqueue_attrs *to,
325 const struct workqueue_attrs *from);
326
Tejun Heo97bd2342010-10-05 10:41:14 +0200327#define CREATE_TRACE_POINTS
328#include <trace/events/workqueue.h>
329
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700330#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700331 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700332 lockdep_is_held(&wq_pool_mutex), \
333 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700334
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700335#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700336 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700337 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700338 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700339
Tejun Heo822d8402013-03-19 13:45:21 -0700340#ifdef CONFIG_LOCKDEP
341#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800342 WARN_ONCE(debug_locks && \
343 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700344 !lockdep_is_held(&(pool)->lock), \
345 "pool->manager_mutex or ->lock should be held")
346#else
347#define assert_manager_or_pool_lock(pool) do { } while (0)
348#endif
349
Tejun Heof02ae732013-03-12 11:30:03 -0700350#define for_each_cpu_worker_pool(pool, cpu) \
351 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
352 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700353 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700354
Tejun Heo49e3cf42013-03-12 11:29:58 -0700355/**
Tejun Heo17116962013-03-12 11:29:58 -0700356 * for_each_pool - iterate through all worker_pools in the system
357 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700358 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700359 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700360 * This must be called either with wq_pool_mutex held or sched RCU read
361 * locked. If the pool needs to be used beyond the locking in effect, the
362 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700363 *
364 * The if/else clause exists only for the lockdep assertion and can be
365 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700366 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700367#define for_each_pool(pool, pi) \
368 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700369 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700370 else
Tejun Heo17116962013-03-12 11:29:58 -0700371
372/**
Tejun Heo822d8402013-03-19 13:45:21 -0700373 * for_each_pool_worker - iterate through all workers of a worker_pool
374 * @worker: iteration cursor
375 * @wi: integer used for iteration
376 * @pool: worker_pool to iterate workers of
377 *
378 * This must be called with either @pool->manager_mutex or ->lock held.
379 *
380 * The if/else clause exists only for the lockdep assertion and can be
381 * ignored.
382 */
383#define for_each_pool_worker(worker, wi, pool) \
384 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
385 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
386 else
387
388/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700389 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
390 * @pwq: iteration cursor
391 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700392 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700393 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700394 * If the pwq needs to be used beyond the locking in effect, the caller is
395 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700396 *
397 * The if/else clause exists only for the lockdep assertion and can be
398 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700399 */
400#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700401 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700402 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700403 else
Tejun Heof3421792010-07-02 10:03:51 +0200404
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900405#ifdef CONFIG_DEBUG_OBJECTS_WORK
406
407static struct debug_obj_descr work_debug_descr;
408
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100409static void *work_debug_hint(void *addr)
410{
411 return ((struct work_struct *) addr)->func;
412}
413
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900414/*
415 * fixup_init is called when:
416 * - an active object is initialized
417 */
418static int work_fixup_init(void *addr, enum debug_obj_state state)
419{
420 struct work_struct *work = addr;
421
422 switch (state) {
423 case ODEBUG_STATE_ACTIVE:
424 cancel_work_sync(work);
425 debug_object_init(work, &work_debug_descr);
426 return 1;
427 default:
428 return 0;
429 }
430}
431
432/*
433 * fixup_activate is called when:
434 * - an active object is activated
435 * - an unknown object is activated (might be a statically initialized object)
436 */
437static int work_fixup_activate(void *addr, enum debug_obj_state state)
438{
439 struct work_struct *work = addr;
440
441 switch (state) {
442
443 case ODEBUG_STATE_NOTAVAILABLE:
444 /*
445 * This is not really a fixup. The work struct was
446 * statically initialized. We just make sure that it
447 * is tracked in the object tracker.
448 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200449 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900450 debug_object_init(work, &work_debug_descr);
451 debug_object_activate(work, &work_debug_descr);
452 return 0;
453 }
454 WARN_ON_ONCE(1);
455 return 0;
456
457 case ODEBUG_STATE_ACTIVE:
458 WARN_ON(1);
459
460 default:
461 return 0;
462 }
463}
464
465/*
466 * fixup_free is called when:
467 * - an active object is freed
468 */
469static int work_fixup_free(void *addr, enum debug_obj_state state)
470{
471 struct work_struct *work = addr;
472
473 switch (state) {
474 case ODEBUG_STATE_ACTIVE:
475 cancel_work_sync(work);
476 debug_object_free(work, &work_debug_descr);
477 return 1;
478 default:
479 return 0;
480 }
481}
482
483static struct debug_obj_descr work_debug_descr = {
484 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100485 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900486 .fixup_init = work_fixup_init,
487 .fixup_activate = work_fixup_activate,
488 .fixup_free = work_fixup_free,
489};
490
491static inline void debug_work_activate(struct work_struct *work)
492{
493 debug_object_activate(work, &work_debug_descr);
494}
495
496static inline void debug_work_deactivate(struct work_struct *work)
497{
498 debug_object_deactivate(work, &work_debug_descr);
499}
500
501void __init_work(struct work_struct *work, int onstack)
502{
503 if (onstack)
504 debug_object_init_on_stack(work, &work_debug_descr);
505 else
506 debug_object_init(work, &work_debug_descr);
507}
508EXPORT_SYMBOL_GPL(__init_work);
509
510void destroy_work_on_stack(struct work_struct *work)
511{
512 debug_object_free(work, &work_debug_descr);
513}
514EXPORT_SYMBOL_GPL(destroy_work_on_stack);
515
516#else
517static inline void debug_work_activate(struct work_struct *work) { }
518static inline void debug_work_deactivate(struct work_struct *work) { }
519#endif
520
Tejun Heo9daf9e62013-01-24 11:01:33 -0800521/* allocate ID and assign it to @pool */
522static int worker_pool_assign_id(struct worker_pool *pool)
523{
524 int ret;
525
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700526 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700527
Tejun Heoe68035f2013-03-13 14:59:38 -0700528 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700529 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700530 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700531 return 0;
532 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800533 return ret;
534}
535
Tejun Heo76af4d92013-03-12 11:30:00 -0700536/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700537 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
538 * @wq: the target workqueue
539 * @node: the node ID
540 *
541 * This must be called either with pwq_lock held or sched RCU read locked.
542 * If the pwq needs to be used beyond the locking in effect, the caller is
543 * responsible for guaranteeing that the pwq stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700544 *
545 * Return: The unbound pool_workqueue for @node.
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700546 */
547static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
548 int node)
549{
550 assert_rcu_or_wq_mutex(wq);
551 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
552}
553
Tejun Heo73f53c42010-06-29 10:07:11 +0200554static unsigned int work_color_to_flags(int color)
555{
556 return color << WORK_STRUCT_COLOR_SHIFT;
557}
558
559static int get_work_color(struct work_struct *work)
560{
561 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
562 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
563}
564
565static int work_next_color(int color)
566{
567 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700568}
569
David Howells4594bf12006-12-07 11:33:26 +0000570/*
Tejun Heo112202d2013-02-13 19:29:12 -0800571 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
572 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800573 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574 *
Tejun Heo112202d2013-02-13 19:29:12 -0800575 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
576 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700577 * work->data. These functions should only be called while the work is
578 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579 *
Tejun Heo112202d2013-02-13 19:29:12 -0800580 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800581 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800582 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800583 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700584 *
585 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
586 * canceled. While being canceled, a work item may have its PENDING set
587 * but stay off timer and worklist for arbitrarily long and nobody should
588 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000589 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590static inline void set_work_data(struct work_struct *work, unsigned long data,
591 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000592{
Tejun Heo6183c002013-03-12 11:29:57 -0700593 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000595}
David Howells365970a2006-11-22 14:54:49 +0000596
Tejun Heo112202d2013-02-13 19:29:12 -0800597static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200599{
Tejun Heo112202d2013-02-13 19:29:12 -0800600 set_work_data(work, (unsigned long)pwq,
601 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200602}
603
Lai Jiangshan4468a002013-02-06 18:04:53 -0800604static void set_work_pool_and_keep_pending(struct work_struct *work,
605 int pool_id)
606{
607 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
608 WORK_STRUCT_PENDING);
609}
610
Tejun Heo7c3eed52013-01-24 11:01:33 -0800611static void set_work_pool_and_clear_pending(struct work_struct *work,
612 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000613{
Tejun Heo23657bb2012-08-13 17:08:19 -0700614 /*
615 * The following wmb is paired with the implied mb in
616 * test_and_set_bit(PENDING) and ensures all updates to @work made
617 * here are visible to and precede any updates by the next PENDING
618 * owner.
619 */
620 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800621 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200622}
623
624static void clear_work_data(struct work_struct *work)
625{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800626 smp_wmb(); /* see set_work_pool_and_clear_pending() */
627 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200628}
629
Tejun Heo112202d2013-02-13 19:29:12 -0800630static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200631{
Tejun Heoe1201532010-07-22 14:14:25 +0200632 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200633
Tejun Heo112202d2013-02-13 19:29:12 -0800634 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200635 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
636 else
637 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200638}
639
Tejun Heo7c3eed52013-01-24 11:01:33 -0800640/**
641 * get_work_pool - return the worker_pool a given work was associated with
642 * @work: the work item of interest
643 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700644 * Pools are created and destroyed under wq_pool_mutex, and allows read
645 * access under sched-RCU read lock. As such, this function should be
646 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700647 *
648 * All fields of the returned pool are accessible as long as the above
649 * mentioned locking is in effect. If the returned pool needs to be used
650 * beyond the critical section, the caller is responsible for ensuring the
651 * returned pool is and stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700652 *
653 * Return: The worker_pool @work was last associated with. %NULL if none.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800654 */
655static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200656{
Tejun Heoe1201532010-07-22 14:14:25 +0200657 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800658 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200659
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700660 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700661
Tejun Heo112202d2013-02-13 19:29:12 -0800662 if (data & WORK_STRUCT_PWQ)
663 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800664 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200665
Tejun Heo7c3eed52013-01-24 11:01:33 -0800666 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
667 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200668 return NULL;
669
Tejun Heofa1b54e2013-03-12 11:30:00 -0700670 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800671}
672
673/**
674 * get_work_pool_id - return the worker pool ID a given work is associated with
675 * @work: the work item of interest
676 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700677 * Return: The worker_pool ID @work was last associated with.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800678 * %WORK_OFFQ_POOL_NONE if none.
679 */
680static int get_work_pool_id(struct work_struct *work)
681{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800682 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800683
Tejun Heo112202d2013-02-13 19:29:12 -0800684 if (data & WORK_STRUCT_PWQ)
685 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800686 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
687
688 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800689}
690
Tejun Heobbb68df2012-08-03 10:30:46 -0700691static void mark_work_canceling(struct work_struct *work)
692{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800693 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700694
Tejun Heo7c3eed52013-01-24 11:01:33 -0800695 pool_id <<= WORK_OFFQ_POOL_SHIFT;
696 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700697}
698
699static bool work_is_canceling(struct work_struct *work)
700{
701 unsigned long data = atomic_long_read(&work->data);
702
Tejun Heo112202d2013-02-13 19:29:12 -0800703 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700704}
705
David Howells365970a2006-11-22 14:54:49 +0000706/*
Tejun Heo32704762012-07-13 22:16:45 -0700707 * Policy functions. These define the policies on how the global worker
708 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800709 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000710 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200711
Tejun Heo63d95a92012-07-12 14:46:37 -0700712static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000713{
Tejun Heoe19e3972013-01-24 11:39:44 -0800714 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000715}
716
Tejun Heoe22bee72010-06-29 10:07:14 +0200717/*
718 * Need to wake up a worker? Called from anything but currently
719 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700720 *
721 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800722 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700723 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200724 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700725static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000726{
Tejun Heo63d95a92012-07-12 14:46:37 -0700727 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000728}
729
Tejun Heoe22bee72010-06-29 10:07:14 +0200730/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700731static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200732{
Tejun Heo63d95a92012-07-12 14:46:37 -0700733 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200734}
735
736/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700737static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200738{
Tejun Heoe19e3972013-01-24 11:39:44 -0800739 return !list_empty(&pool->worklist) &&
740 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200741}
742
743/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700744static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200745{
Tejun Heo63d95a92012-07-12 14:46:37 -0700746 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200747}
748
749/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700750static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200751{
Tejun Heo63d95a92012-07-12 14:46:37 -0700752 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700753 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200754}
755
756/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700757static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200758{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700759 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700760 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
761 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200762
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700763 /*
764 * nr_idle and idle_list may disagree if idle rebinding is in
765 * progress. Never return %true if idle_list is empty.
766 */
767 if (list_empty(&pool->idle_list))
768 return false;
769
Tejun Heoe22bee72010-06-29 10:07:14 +0200770 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
771}
772
773/*
774 * Wake up functions.
775 */
776
Tejun Heo7e116292010-06-29 10:07:13 +0200777/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700778static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200779{
Tejun Heo63d95a92012-07-12 14:46:37 -0700780 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200781 return NULL;
782
Tejun Heo63d95a92012-07-12 14:46:37 -0700783 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200784}
785
786/**
787 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700788 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200789 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700790 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200791 *
792 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800793 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200794 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700795static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200796{
Tejun Heo63d95a92012-07-12 14:46:37 -0700797 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200798
799 if (likely(worker))
800 wake_up_process(worker->task);
801}
802
Tejun Heo4690c4a2010-06-29 10:07:10 +0200803/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200804 * wq_worker_waking_up - a worker is waking up
805 * @task: task waking up
806 * @cpu: CPU @task is waking up to
807 *
808 * This function is called during try_to_wake_up() when a worker is
809 * being awoken.
810 *
811 * CONTEXT:
812 * spin_lock_irq(rq->lock)
813 */
Tejun Heod84ff052013-03-12 11:29:59 -0700814void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200815{
816 struct worker *worker = kthread_data(task);
817
Joonsoo Kim36576002012-10-26 23:03:49 +0900818 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800819 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800820 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900821 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200822}
823
824/**
825 * wq_worker_sleeping - a worker is going to sleep
826 * @task: task going to sleep
827 * @cpu: CPU in question, must be the current CPU number
828 *
829 * This function is called during schedule() when a busy worker is
830 * going to sleep. Worker on the same cpu can be woken up by
831 * returning pointer to its task.
832 *
833 * CONTEXT:
834 * spin_lock_irq(rq->lock)
835 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700836 * Return:
Tejun Heoe22bee72010-06-29 10:07:14 +0200837 * Worker task on @cpu to wake up, %NULL if none.
838 */
Tejun Heod84ff052013-03-12 11:29:59 -0700839struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200840{
841 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800842 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200843
Tejun Heo111c2252013-01-17 17:16:24 -0800844 /*
845 * Rescuers, which may not have all the fields set up like normal
846 * workers, also reach here, let's not access anything before
847 * checking NOT_RUNNING.
848 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500849 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 return NULL;
851
Tejun Heo111c2252013-01-17 17:16:24 -0800852 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800853
Tejun Heoe22bee72010-06-29 10:07:14 +0200854 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700855 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
856 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200857
858 /*
859 * The counterpart of the following dec_and_test, implied mb,
860 * worklist not empty test sequence is in insert_work().
861 * Please read comment there.
862 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700863 * NOT_RUNNING is clear. This means that we're bound to and
864 * running on the local cpu w/ rq lock held and preemption
865 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800866 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700867 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200868 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800869 if (atomic_dec_and_test(&pool->nr_running) &&
870 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700871 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200872 return to_wakeup ? to_wakeup->task : NULL;
873}
874
875/**
876 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200877 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200878 * @flags: flags to set
879 * @wakeup: wakeup an idle worker if necessary
880 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200881 * Set @flags in @worker->flags and adjust nr_running accordingly. If
882 * nr_running becomes zero and @wakeup is %true, an idle worker is
883 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200884 *
Tejun Heocb444762010-07-02 10:03:50 +0200885 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800886 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200887 */
888static inline void worker_set_flags(struct worker *worker, unsigned int flags,
889 bool wakeup)
890{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700891 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200892
Tejun Heocb444762010-07-02 10:03:50 +0200893 WARN_ON_ONCE(worker->task != current);
894
Tejun Heoe22bee72010-06-29 10:07:14 +0200895 /*
896 * If transitioning into NOT_RUNNING, adjust nr_running and
897 * wake up an idle worker as necessary if requested by
898 * @wakeup.
899 */
900 if ((flags & WORKER_NOT_RUNNING) &&
901 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200902 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800903 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700904 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700905 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200906 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800907 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200908 }
909
Tejun Heod302f012010-06-29 10:07:13 +0200910 worker->flags |= flags;
911}
912
913/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200914 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200915 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200916 * @flags: flags to clear
917 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200918 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200919 *
Tejun Heocb444762010-07-02 10:03:50 +0200920 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800921 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200922 */
923static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
924{
Tejun Heo63d95a92012-07-12 14:46:37 -0700925 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200926 unsigned int oflags = worker->flags;
927
Tejun Heocb444762010-07-02 10:03:50 +0200928 WARN_ON_ONCE(worker->task != current);
929
Tejun Heod302f012010-06-29 10:07:13 +0200930 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200931
Tejun Heo42c025f2011-01-11 15:58:49 +0100932 /*
933 * If transitioning out of NOT_RUNNING, increment nr_running. Note
934 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
935 * of multiple flags, not a single flag.
936 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200937 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
938 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800939 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200940}
941
942/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200943 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800944 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200945 * @work: work to find worker for
946 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800947 * Find a worker which is executing @work on @pool by searching
948 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800949 * to match, its current execution should match the address of @work and
950 * its work function. This is to avoid unwanted dependency between
951 * unrelated work executions through a work item being recycled while still
952 * being executed.
953 *
954 * This is a bit tricky. A work item may be freed once its execution
955 * starts and nothing prevents the freed area from being recycled for
956 * another work item. If the same work item address ends up being reused
957 * before the original execution finishes, workqueue will identify the
958 * recycled work item as currently executing and make it wait until the
959 * current execution finishes, introducing an unwanted dependency.
960 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700961 * This function checks the work item address and work function to avoid
962 * false positives. Note that this isn't complete as one may construct a
963 * work function which can introduce dependency onto itself through a
964 * recycled work item. Well, if somebody wants to shoot oneself in the
965 * foot that badly, there's only so much we can do, and if such deadlock
966 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200967 *
968 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800969 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200970 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700971 * Return:
972 * Pointer to worker which is executing @work if found, %NULL
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200973 * otherwise.
974 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800975static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200976 struct work_struct *work)
977{
Sasha Levin42f85702012-12-17 10:01:23 -0500978 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500979
Sasha Levinb67bfe02013-02-27 17:06:00 -0800980 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800981 (unsigned long)work)
982 if (worker->current_work == work &&
983 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500984 return worker;
985
986 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200987}
988
989/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700990 * move_linked_works - move linked works to a list
991 * @work: start of series of works to be scheduled
992 * @head: target list to append @work to
993 * @nextp: out paramter for nested worklist walking
994 *
995 * Schedule linked works starting from @work to @head. Work series to
996 * be scheduled starts at @work and includes any consecutive work with
997 * WORK_STRUCT_LINKED set in its predecessor.
998 *
999 * If @nextp is not NULL, it's updated to point to the next work of
1000 * the last scheduled work. This allows move_linked_works() to be
1001 * nested inside outer list_for_each_entry_safe().
1002 *
1003 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001004 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001005 */
1006static void move_linked_works(struct work_struct *work, struct list_head *head,
1007 struct work_struct **nextp)
1008{
1009 struct work_struct *n;
1010
1011 /*
1012 * Linked worklist will always end before the end of the list,
1013 * use NULL for list head.
1014 */
1015 list_for_each_entry_safe_from(work, n, NULL, entry) {
1016 list_move_tail(&work->entry, head);
1017 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1018 break;
1019 }
1020
1021 /*
1022 * If we're already inside safe list traversal and have moved
1023 * multiple works to the scheduled queue, the next position
1024 * needs to be updated.
1025 */
1026 if (nextp)
1027 *nextp = n;
1028}
1029
Tejun Heo8864b4e2013-03-12 11:30:04 -07001030/**
1031 * get_pwq - get an extra reference on the specified pool_workqueue
1032 * @pwq: pool_workqueue to get
1033 *
1034 * Obtain an extra reference on @pwq. The caller should guarantee that
1035 * @pwq has positive refcnt and be holding the matching pool->lock.
1036 */
1037static void get_pwq(struct pool_workqueue *pwq)
1038{
1039 lockdep_assert_held(&pwq->pool->lock);
1040 WARN_ON_ONCE(pwq->refcnt <= 0);
1041 pwq->refcnt++;
1042}
1043
1044/**
1045 * put_pwq - put a pool_workqueue reference
1046 * @pwq: pool_workqueue to put
1047 *
1048 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1049 * destruction. The caller should be holding the matching pool->lock.
1050 */
1051static void put_pwq(struct pool_workqueue *pwq)
1052{
1053 lockdep_assert_held(&pwq->pool->lock);
1054 if (likely(--pwq->refcnt))
1055 return;
1056 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1057 return;
1058 /*
1059 * @pwq can't be released under pool->lock, bounce to
1060 * pwq_unbound_release_workfn(). This never recurses on the same
1061 * pool->lock as this path is taken only for unbound workqueues and
1062 * the release work item is scheduled on a per-cpu workqueue. To
1063 * avoid lockdep warning, unbound pool->locks are given lockdep
1064 * subclass of 1 in get_unbound_pool().
1065 */
1066 schedule_work(&pwq->unbound_release_work);
1067}
1068
Tejun Heodce90d42013-04-01 11:23:35 -07001069/**
1070 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1071 * @pwq: pool_workqueue to put (can be %NULL)
1072 *
1073 * put_pwq() with locking. This function also allows %NULL @pwq.
1074 */
1075static void put_pwq_unlocked(struct pool_workqueue *pwq)
1076{
1077 if (pwq) {
1078 /*
1079 * As both pwqs and pools are sched-RCU protected, the
1080 * following lock operations are safe.
1081 */
1082 spin_lock_irq(&pwq->pool->lock);
1083 put_pwq(pwq);
1084 spin_unlock_irq(&pwq->pool->lock);
1085 }
1086}
1087
Tejun Heo112202d2013-02-13 19:29:12 -08001088static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001089{
Tejun Heo112202d2013-02-13 19:29:12 -08001090 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001091
1092 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001093 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001094 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001095 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001096}
1097
Tejun Heo112202d2013-02-13 19:29:12 -08001098static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001099{
Tejun Heo112202d2013-02-13 19:29:12 -08001100 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001101 struct work_struct, entry);
1102
Tejun Heo112202d2013-02-13 19:29:12 -08001103 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001104}
1105
Tejun Heobf4ede02012-08-03 10:30:46 -07001106/**
Tejun Heo112202d2013-02-13 19:29:12 -08001107 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1108 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001109 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001110 *
1111 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001112 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001113 *
1114 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001115 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001116 */
Tejun Heo112202d2013-02-13 19:29:12 -08001117static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001118{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001119 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001120 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001121 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001122
Tejun Heo112202d2013-02-13 19:29:12 -08001123 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001124
Tejun Heo112202d2013-02-13 19:29:12 -08001125 pwq->nr_active--;
1126 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001127 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001128 if (pwq->nr_active < pwq->max_active)
1129 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001130 }
1131
1132 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001133 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001134 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001135
1136 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001137 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001138 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001139
Tejun Heo112202d2013-02-13 19:29:12 -08001140 /* this pwq is done, clear flush_color */
1141 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001142
1143 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001144 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001145 * will handle the rest.
1146 */
Tejun Heo112202d2013-02-13 19:29:12 -08001147 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1148 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001149out_put:
1150 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001151}
1152
Tejun Heo36e227d2012-08-03 10:30:46 -07001153/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001154 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001155 * @work: work item to steal
1156 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001157 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001158 *
1159 * Try to grab PENDING bit of @work. This function can handle @work in any
Yacine Belkadid185af32013-07-31 14:59:24 -07001160 * stable state - idle, on timer or on worklist.
Tejun Heo36e227d2012-08-03 10:30:46 -07001161 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001162 * Return:
Tejun Heo36e227d2012-08-03 10:30:46 -07001163 * 1 if @work was pending and we successfully stole PENDING
1164 * 0 if @work was idle and we claimed PENDING
1165 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001166 * -ENOENT if someone else is canceling @work, this state may persist
1167 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001168 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001169 * Note:
Tejun Heobbb68df2012-08-03 10:30:46 -07001170 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001171 * interrupted while holding PENDING and @work off queue, irq must be
1172 * disabled on entry. This, combined with delayed_work->timer being
1173 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001174 *
1175 * On successful return, >= 0, irq is disabled and the caller is
1176 * responsible for releasing it using local_irq_restore(*@flags).
1177 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001178 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001179 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001180static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1181 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001182{
Tejun Heod565ed62013-01-24 11:01:33 -08001183 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001184 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001185
Tejun Heobbb68df2012-08-03 10:30:46 -07001186 local_irq_save(*flags);
1187
Tejun Heo36e227d2012-08-03 10:30:46 -07001188 /* try to steal the timer if it exists */
1189 if (is_dwork) {
1190 struct delayed_work *dwork = to_delayed_work(work);
1191
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001192 /*
1193 * dwork->timer is irqsafe. If del_timer() fails, it's
1194 * guaranteed that the timer is not queued anywhere and not
1195 * running on the local CPU.
1196 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001197 if (likely(del_timer(&dwork->timer)))
1198 return 1;
1199 }
1200
1201 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001202 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1203 return 0;
1204
1205 /*
1206 * The queueing is in progress, or it is already queued. Try to
1207 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1208 */
Tejun Heod565ed62013-01-24 11:01:33 -08001209 pool = get_work_pool(work);
1210 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001211 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001212
Tejun Heod565ed62013-01-24 11:01:33 -08001213 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001214 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001215 * work->data is guaranteed to point to pwq only while the work
1216 * item is queued on pwq->wq, and both updating work->data to point
1217 * to pwq on queueing and to pool on dequeueing are done under
1218 * pwq->pool->lock. This in turn guarantees that, if work->data
1219 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001220 * item is currently queued on that pool.
1221 */
Tejun Heo112202d2013-02-13 19:29:12 -08001222 pwq = get_work_pwq(work);
1223 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001224 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001225
Tejun Heo16062832013-02-06 18:04:53 -08001226 /*
1227 * A delayed work item cannot be grabbed directly because
1228 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001229 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001230 * management later on and cause stall. Make sure the work
1231 * item is activated before grabbing.
1232 */
1233 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001234 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001235
Tejun Heo16062832013-02-06 18:04:53 -08001236 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001237 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001238
Tejun Heo112202d2013-02-13 19:29:12 -08001239 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001240 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001241
Tejun Heo16062832013-02-06 18:04:53 -08001242 spin_unlock(&pool->lock);
1243 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001244 }
Tejun Heod565ed62013-01-24 11:01:33 -08001245 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001246fail:
1247 local_irq_restore(*flags);
1248 if (work_is_canceling(work))
1249 return -ENOENT;
1250 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001251 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001252}
1253
1254/**
Tejun Heo706026c2013-01-24 11:01:34 -08001255 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001256 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001257 * @work: work to insert
1258 * @head: insertion point
1259 * @extra_flags: extra WORK_STRUCT_* flags to set
1260 *
Tejun Heo112202d2013-02-13 19:29:12 -08001261 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001262 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001263 *
1264 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001265 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001266 */
Tejun Heo112202d2013-02-13 19:29:12 -08001267static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1268 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001269{
Tejun Heo112202d2013-02-13 19:29:12 -08001270 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001271
Tejun Heo4690c4a2010-06-29 10:07:10 +02001272 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001273 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001274 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001275 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001276
1277 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001278 * Ensure either wq_worker_sleeping() sees the above
1279 * list_add_tail() or we see zero nr_running to avoid workers lying
1280 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001281 */
1282 smp_mb();
1283
Tejun Heo63d95a92012-07-12 14:46:37 -07001284 if (__need_more_worker(pool))
1285 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001286}
1287
Tejun Heoc8efcc22010-12-20 19:32:04 +01001288/*
1289 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001290 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001291 */
1292static bool is_chained_work(struct workqueue_struct *wq)
1293{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001294 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001295
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001296 worker = current_wq_worker();
1297 /*
1298 * Return %true iff I'm a worker execuing a work item on @wq. If
1299 * I'm @worker, it's safe to dereference it without locking.
1300 */
Tejun Heo112202d2013-02-13 19:29:12 -08001301 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001302}
1303
Tejun Heod84ff052013-03-12 11:29:59 -07001304static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct work_struct *work)
1306{
Tejun Heo112202d2013-02-13 19:29:12 -08001307 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001308 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001309 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001310 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001311 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001312
1313 /*
1314 * While a work item is PENDING && off queue, a task trying to
1315 * steal the PENDING will busy-loop waiting for it to either get
1316 * queued or lose PENDING. Grabbing PENDING and queueing should
1317 * happen with IRQ disabled.
1318 */
1319 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001321 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001322
Tejun Heoc8efcc22010-12-20 19:32:04 +01001323 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001324 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001325 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001326 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001327retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001328 if (req_cpu == WORK_CPU_UNBOUND)
1329 cpu = raw_smp_processor_id();
1330
Tejun Heoc9178082013-03-12 11:30:04 -07001331 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001332 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001333 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001334 else
1335 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001336
Tejun Heoc9178082013-03-12 11:30:04 -07001337 /*
1338 * If @work was previously on a different pool, it might still be
1339 * running there, in which case the work needs to be queued on that
1340 * pool to guarantee non-reentrancy.
1341 */
1342 last_pool = get_work_pool(work);
1343 if (last_pool && last_pool != pwq->pool) {
1344 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001345
Tejun Heoc9178082013-03-12 11:30:04 -07001346 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001347
Tejun Heoc9178082013-03-12 11:30:04 -07001348 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001349
Tejun Heoc9178082013-03-12 11:30:04 -07001350 if (worker && worker->current_pwq->wq == wq) {
1351 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001352 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001353 /* meh... not running there, queue here */
1354 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001355 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001356 }
Tejun Heof3421792010-07-02 10:03:51 +02001357 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001358 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001359 }
1360
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001361 /*
1362 * pwq is determined and locked. For unbound pools, we could have
1363 * raced with pwq release and it could already be dead. If its
1364 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001365 * without another pwq replacing it in the numa_pwq_tbl or while
1366 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001367 * make forward-progress.
1368 */
1369 if (unlikely(!pwq->refcnt)) {
1370 if (wq->flags & WQ_UNBOUND) {
1371 spin_unlock(&pwq->pool->lock);
1372 cpu_relax();
1373 goto retry;
1374 }
1375 /* oops */
1376 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1377 wq->name, cpu);
1378 }
1379
Tejun Heo112202d2013-02-13 19:29:12 -08001380 /* pwq determined, queue */
1381 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001382
Dan Carpenterf5b25522012-04-13 22:06:58 +03001383 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001384 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001385 return;
1386 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001387
Tejun Heo112202d2013-02-13 19:29:12 -08001388 pwq->nr_in_flight[pwq->work_color]++;
1389 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001390
Tejun Heo112202d2013-02-13 19:29:12 -08001391 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001392 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001393 pwq->nr_active++;
1394 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001395 } else {
1396 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001397 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001398 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001399
Tejun Heo112202d2013-02-13 19:29:12 -08001400 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001401
Tejun Heo112202d2013-02-13 19:29:12 -08001402 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001405/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001406 * queue_work_on - queue work on specific cpu
1407 * @cpu: CPU number to execute work on
1408 * @wq: workqueue to use
1409 * @work: work to queue
1410 *
Zhang Ruic1a220e2008-07-23 21:28:39 -07001411 * We queue the work to a specific CPU, the caller must ensure it
1412 * can't go away.
Yacine Belkadid185af32013-07-31 14:59:24 -07001413 *
1414 * Return: %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001415 */
Tejun Heod4283e92012-08-03 10:30:44 -07001416bool queue_work_on(int cpu, struct workqueue_struct *wq,
1417 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001418{
Tejun Heod4283e92012-08-03 10:30:44 -07001419 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001420 unsigned long flags;
1421
1422 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001423
Tejun Heo22df02b2010-06-29 10:07:10 +02001424 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001425 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001426 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001427 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001428
1429 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001430 return ret;
1431}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001432EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001433
Tejun Heod8e794d2012-08-03 10:30:45 -07001434void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
David Howells52bad642006-11-22 14:54:01 +00001436 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001438 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001439 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001441EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001443static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1444 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001446 struct timer_list *timer = &dwork->timer;
1447 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001449 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1450 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001451 WARN_ON_ONCE(timer_pending(timer));
1452 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001453
Tejun Heo8852aac2012-12-01 16:23:42 -08001454 /*
1455 * If @delay is 0, queue @dwork->work immediately. This is for
1456 * both optimization and correctness. The earliest @timer can
1457 * expire is on the closest next tick and delayed_work users depend
1458 * on that there's no such delay when @delay is 0.
1459 */
1460 if (!delay) {
1461 __queue_work(cpu, wq, &dwork->work);
1462 return;
1463 }
1464
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001465 timer_stats_timer_set_start_info(&dwork->timer);
1466
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001467 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001468 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001469 timer->expires = jiffies + delay;
1470
1471 if (unlikely(cpu != WORK_CPU_UNBOUND))
1472 add_timer_on(timer, cpu);
1473 else
1474 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001477/**
1478 * queue_delayed_work_on - queue work on specific CPU after delay
1479 * @cpu: CPU number to execute work on
1480 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001481 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001482 * @delay: number of jiffies to wait before queueing
1483 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001484 * Return: %false if @work was already on a queue, %true otherwise. If
Tejun Heo715f1302012-08-03 10:30:46 -07001485 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1486 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001487 */
Tejun Heod4283e92012-08-03 10:30:44 -07001488bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1489 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001490{
David Howells52bad642006-11-22 14:54:01 +00001491 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001492 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001493 unsigned long flags;
1494
1495 /* read the comment in __queue_work() */
1496 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001497
Tejun Heo22df02b2010-06-29 10:07:10 +02001498 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001499 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001500 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001501 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001502
1503 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001504 return ret;
1505}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001506EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Tejun Heoc8e55f32010-06-29 10:07:12 +02001508/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001509 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1510 * @cpu: CPU number to execute work on
1511 * @wq: workqueue to use
1512 * @dwork: work to queue
1513 * @delay: number of jiffies to wait before queueing
1514 *
1515 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1516 * modify @dwork's timer so that it expires after @delay. If @delay is
1517 * zero, @work is guaranteed to be scheduled immediately regardless of its
1518 * current state.
1519 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001520 * Return: %false if @dwork was idle and queued, %true if @dwork was
Tejun Heo8376fe22012-08-03 10:30:47 -07001521 * pending and its timer was modified.
1522 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001523 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001524 * See try_to_grab_pending() for details.
1525 */
1526bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1527 struct delayed_work *dwork, unsigned long delay)
1528{
1529 unsigned long flags;
1530 int ret;
1531
1532 do {
1533 ret = try_to_grab_pending(&dwork->work, true, &flags);
1534 } while (unlikely(ret == -EAGAIN));
1535
1536 if (likely(ret >= 0)) {
1537 __queue_delayed_work(cpu, wq, dwork, delay);
1538 local_irq_restore(flags);
1539 }
1540
1541 /* -ENOENT from try_to_grab_pending() becomes %true */
1542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1545
1546/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001547 * worker_enter_idle - enter idle state
1548 * @worker: worker which is entering idle state
1549 *
1550 * @worker is entering idle state. Update stats and idle timer if
1551 * necessary.
1552 *
1553 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001554 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001555 */
1556static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001558 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Tejun Heo6183c002013-03-12 11:29:57 -07001560 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1561 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1562 (worker->hentry.next || worker->hentry.pprev)))
1563 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Tejun Heocb444762010-07-02 10:03:50 +02001565 /* can't use worker_set_flags(), also called from start_worker() */
1566 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001567 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001569
Tejun Heoc8e55f32010-06-29 10:07:12 +02001570 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001571 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001572
Tejun Heo628c78e2012-07-17 12:39:27 -07001573 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1574 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001575
Tejun Heo544ecf32012-05-14 15:04:50 -07001576 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001577 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001578 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001579 * nr_running, the warning may trigger spuriously. Check iff
1580 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001581 */
Tejun Heo24647572013-01-24 11:01:33 -08001582 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001583 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001584 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Tejun Heoc8e55f32010-06-29 10:07:12 +02001587/**
1588 * worker_leave_idle - leave idle state
1589 * @worker: worker which is leaving idle state
1590 *
1591 * @worker is leaving idle state. Update stats.
1592 *
1593 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001594 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001595 */
1596static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001598 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Tejun Heo6183c002013-03-12 11:29:57 -07001600 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1601 return;
Tejun Heod302f012010-06-29 10:07:13 +02001602 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001603 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001604 list_del_init(&worker->entry);
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Tejun Heoe22bee72010-06-29 10:07:14 +02001607/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001608 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1609 * @pool: target worker_pool
1610 *
1611 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 *
1613 * Works which are scheduled while the cpu is online must at least be
1614 * scheduled to a worker which is bound to the cpu so that if they are
1615 * flushed from cpu callbacks while cpu is going down, they are
1616 * guaranteed to execute on the cpu.
1617 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001618 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 * themselves to the target cpu and may race with cpu going down or
1620 * coming online. kthread_bind() can't be used because it may put the
1621 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001622 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 * [dis]associated in the meantime.
1624 *
Tejun Heo706026c2013-01-24 11:01:34 -08001625 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001626 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001627 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1628 * enters idle state or fetches works without dropping lock, it can
1629 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001630 *
1631 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001632 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 * held.
1634 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001635 * Return:
Tejun Heo706026c2013-01-24 11:01:34 -08001636 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001637 * bound), %false if offline.
1638 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001639static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001640__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001641{
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 while (true) {
1643 /*
1644 * The following call may fail, succeed or succeed
1645 * without actually migrating the task to the cpu if
1646 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001647 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 */
Tejun Heo24647572013-01-24 11:01:33 -08001649 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001650 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001651
Tejun Heod565ed62013-01-24 11:01:33 -08001652 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001653 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001654 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001655 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001656 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001657 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001658 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001659
Tejun Heo5035b202011-04-29 18:08:37 +02001660 /*
1661 * We've raced with CPU hot[un]plug. Give it a breather
1662 * and retry migration. cond_resched() is required here;
1663 * otherwise, we might deadlock against cpu_stop trying to
1664 * bring down the CPU on non-preemptive kernel.
1665 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001666 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001667 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001668 }
1669}
1670
Tejun Heoc34056a2010-06-29 10:07:11 +02001671static struct worker *alloc_worker(void)
1672{
1673 struct worker *worker;
1674
1675 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001676 if (worker) {
1677 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001678 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001679 /* on creation a worker is in !idle && prep state */
1680 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001681 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001682 return worker;
1683}
1684
1685/**
1686 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001687 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001688 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001689 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001690 * can be started by calling start_worker() or destroyed using
1691 * destroy_worker().
1692 *
1693 * CONTEXT:
1694 * Might sleep. Does GFP_KERNEL allocations.
1695 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001696 * Return:
Tejun Heoc34056a2010-06-29 10:07:11 +02001697 * Pointer to the newly created worker.
1698 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001699static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001700{
Tejun Heoc34056a2010-06-29 10:07:11 +02001701 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001702 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001703 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001704
Tejun Heocd549682013-03-13 19:47:39 -07001705 lockdep_assert_held(&pool->manager_mutex);
1706
Tejun Heo822d8402013-03-19 13:45:21 -07001707 /*
1708 * ID is needed to determine kthread name. Allocate ID first
1709 * without installing the pointer.
1710 */
1711 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001712 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001713
1714 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1715
Tejun Heod565ed62013-01-24 11:01:33 -08001716 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001717 idr_preload_end();
1718 if (id < 0)
1719 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001720
1721 worker = alloc_worker();
1722 if (!worker)
1723 goto fail;
1724
Tejun Heobd7bdd42012-07-12 14:46:37 -07001725 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001726 worker->id = id;
1727
Tejun Heo29c91e92013-03-12 11:30:03 -07001728 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001729 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1730 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001731 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001732 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1733
Tejun Heof3f90ad2013-04-01 11:23:34 -07001734 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001735 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001736 if (IS_ERR(worker->task))
1737 goto fail;
1738
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001739 /*
1740 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1741 * online CPUs. It'll be re-applied when any of the CPUs come up.
1742 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001743 set_user_nice(worker->task, pool->attrs->nice);
1744 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001745
Tejun Heo14a40ff2013-03-19 13:45:20 -07001746 /* prevent userland from meddling with cpumask of workqueue workers */
1747 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001748
1749 /*
1750 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1751 * remains stable across this function. See the comments above the
1752 * flag definition for details.
1753 */
1754 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001755 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001756
Tejun Heo822d8402013-03-19 13:45:21 -07001757 /* successful, commit the pointer to idr */
1758 spin_lock_irq(&pool->lock);
1759 idr_replace(&pool->worker_idr, worker, worker->id);
1760 spin_unlock_irq(&pool->lock);
1761
Tejun Heoc34056a2010-06-29 10:07:11 +02001762 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001763
Tejun Heoc34056a2010-06-29 10:07:11 +02001764fail:
1765 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001766 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001767 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001768 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001769 }
1770 kfree(worker);
1771 return NULL;
1772}
1773
1774/**
1775 * start_worker - start a newly created worker
1776 * @worker: worker to start
1777 *
Tejun Heo706026c2013-01-24 11:01:34 -08001778 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001779 *
1780 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001781 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001782 */
1783static void start_worker(struct worker *worker)
1784{
Tejun Heocb444762010-07-02 10:03:50 +02001785 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001786 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001787 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 wake_up_process(worker->task);
1789}
1790
1791/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001792 * create_and_start_worker - create and start a worker for a pool
1793 * @pool: the target pool
1794 *
Tejun Heocd549682013-03-13 19:47:39 -07001795 * Grab the managership of @pool and create and start a new worker for it.
Yacine Belkadid185af32013-07-31 14:59:24 -07001796 *
1797 * Return: 0 on success. A negative error code otherwise.
Tejun Heoebf44d12013-03-13 19:47:39 -07001798 */
1799static int create_and_start_worker(struct worker_pool *pool)
1800{
1801 struct worker *worker;
1802
Tejun Heocd549682013-03-13 19:47:39 -07001803 mutex_lock(&pool->manager_mutex);
1804
Tejun Heoebf44d12013-03-13 19:47:39 -07001805 worker = create_worker(pool);
1806 if (worker) {
1807 spin_lock_irq(&pool->lock);
1808 start_worker(worker);
1809 spin_unlock_irq(&pool->lock);
1810 }
1811
Tejun Heocd549682013-03-13 19:47:39 -07001812 mutex_unlock(&pool->manager_mutex);
1813
Tejun Heoebf44d12013-03-13 19:47:39 -07001814 return worker ? 0 : -ENOMEM;
1815}
1816
1817/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001818 * destroy_worker - destroy a workqueue worker
1819 * @worker: worker to be destroyed
1820 *
Tejun Heo706026c2013-01-24 11:01:34 -08001821 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001822 *
1823 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001824 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001825 */
1826static void destroy_worker(struct worker *worker)
1827{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001828 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001829
Tejun Heocd549682013-03-13 19:47:39 -07001830 lockdep_assert_held(&pool->manager_mutex);
1831 lockdep_assert_held(&pool->lock);
1832
Tejun Heoc34056a2010-06-29 10:07:11 +02001833 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001834 if (WARN_ON(worker->current_work) ||
1835 WARN_ON(!list_empty(&worker->scheduled)))
1836 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001837
Tejun Heoc8e55f32010-06-29 10:07:12 +02001838 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001839 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001840 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001841 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001842
1843 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001844 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001845
Tejun Heo822d8402013-03-19 13:45:21 -07001846 idr_remove(&pool->worker_idr, worker->id);
1847
Tejun Heod565ed62013-01-24 11:01:33 -08001848 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001849
Tejun Heoc34056a2010-06-29 10:07:11 +02001850 kthread_stop(worker->task);
1851 kfree(worker);
1852
Tejun Heod565ed62013-01-24 11:01:33 -08001853 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001854}
1855
Tejun Heo63d95a92012-07-12 14:46:37 -07001856static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001857{
Tejun Heo63d95a92012-07-12 14:46:37 -07001858 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001859
Tejun Heod565ed62013-01-24 11:01:33 -08001860 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001861
Tejun Heo63d95a92012-07-12 14:46:37 -07001862 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001863 struct worker *worker;
1864 unsigned long expires;
1865
1866 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001867 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001868 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1869
1870 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001871 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001872 else {
1873 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001874 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001875 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 }
1877 }
1878
Tejun Heod565ed62013-01-24 11:01:33 -08001879 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001880}
1881
Tejun Heo493a1722013-03-12 11:29:59 -07001882static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001883{
Tejun Heo112202d2013-02-13 19:29:12 -08001884 struct pool_workqueue *pwq = get_work_pwq(work);
1885 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001886
Tejun Heo2e109a22013-03-13 19:47:40 -07001887 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001888
Tejun Heo493008a2013-03-12 11:30:03 -07001889 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001890 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001891
1892 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001893 if (list_empty(&pwq->mayday_node)) {
1894 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001896 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001897}
1898
Tejun Heo706026c2013-01-24 11:01:34 -08001899static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001900{
Tejun Heo63d95a92012-07-12 14:46:37 -07001901 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 struct work_struct *work;
1903
Tejun Heo2e109a22013-03-13 19:47:40 -07001904 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001905 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001906
Tejun Heo63d95a92012-07-12 14:46:37 -07001907 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 /*
1909 * We've been trying to create a new worker but
1910 * haven't been successful. We might be hitting an
1911 * allocation deadlock. Send distress signals to
1912 * rescuers.
1913 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 send_mayday(work);
1916 }
1917
Tejun Heo493a1722013-03-12 11:29:59 -07001918 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001919 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001920
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001922}
1923
1924/**
1925 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001926 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001927 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001928 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 * have at least one idle worker on return from this function. If
1930 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001931 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 * possible allocation deadlock.
1933 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001934 * On return, need_to_create_worker() is guaranteed to be %false and
1935 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 *
1937 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001938 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 * multiple times. Does GFP_KERNEL allocations. Called only from
1940 * manager.
1941 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001942 * Return:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001943 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 * otherwise.
1945 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001946static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001947__releases(&pool->lock)
1948__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001949{
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 return false;
1952restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001953 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001954
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957
1958 while (true) {
1959 struct worker *worker;
1960
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001961 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001962 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001964 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001966 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1967 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 return true;
1969 }
1970
Tejun Heo63d95a92012-07-12 14:46:37 -07001971 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 break;
1973
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 __set_current_state(TASK_INTERRUPTIBLE);
1975 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001976
Tejun Heo63d95a92012-07-12 14:46:37 -07001977 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 break;
1979 }
1980
Tejun Heo63d95a92012-07-12 14:46:37 -07001981 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001982 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001983 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 goto restart;
1985 return true;
1986}
1987
1988/**
1989 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001992 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 * IDLE_WORKER_TIMEOUT.
1994 *
1995 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001996 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001997 * multiple times. Called only from manager.
1998 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001999 * Return:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002000 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 * otherwise.
2002 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002003static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002004{
2005 bool ret = false;
2006
Tejun Heo63d95a92012-07-12 14:46:37 -07002007 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 struct worker *worker;
2009 unsigned long expires;
2010
Tejun Heo63d95a92012-07-12 14:46:37 -07002011 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2013
2014 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002015 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 break;
2017 }
2018
2019 destroy_worker(worker);
2020 ret = true;
2021 }
2022
2023 return ret;
2024}
2025
2026/**
2027 * manage_workers - manage worker pool
2028 * @worker: self
2029 *
Tejun Heo706026c2013-01-24 11:01:34 -08002030 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002031 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002032 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 *
2034 * The caller can safely start processing works on false return. On
2035 * true return, it's guaranteed that need_to_create_worker() is false
2036 * and may_start_working() is true.
2037 *
2038 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002039 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 * multiple times. Does GFP_KERNEL allocations.
2041 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002042 * Return:
Libin2d498db2013-08-21 08:50:40 +08002043 * %false if the pool don't need management and the caller can safely start
2044 * processing works, %true indicates that the function released pool->lock
2045 * and reacquired it to perform some management function and that the
2046 * conditions that the caller verified while holding the lock before
2047 * calling the function might no longer be true.
Tejun Heoe22bee72010-06-29 10:07:14 +02002048 */
2049static bool manage_workers(struct worker *worker)
2050{
Tejun Heo63d95a92012-07-12 14:46:37 -07002051 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002052 bool ret = false;
2053
Tejun Heobc3a1af2013-03-13 19:47:39 -07002054 /*
2055 * Managership is governed by two mutexes - manager_arb and
2056 * manager_mutex. manager_arb handles arbitration of manager role.
2057 * Anyone who successfully grabs manager_arb wins the arbitration
2058 * and becomes the manager. mutex_trylock() on pool->manager_arb
2059 * failure while holding pool->lock reliably indicates that someone
2060 * else is managing the pool and the worker which failed trylock
2061 * can proceed to executing work items. This means that anyone
2062 * grabbing manager_arb is responsible for actually performing
2063 * manager duties. If manager_arb is grabbed and released without
2064 * actual management, the pool may stall indefinitely.
2065 *
2066 * manager_mutex is used for exclusion of actual management
2067 * operations. The holder of manager_mutex can be sure that none
2068 * of management operations, including creation and destruction of
2069 * workers, won't take place until the mutex is released. Because
2070 * manager_mutex doesn't interfere with manager role arbitration,
2071 * it is guaranteed that the pool's management, while may be
2072 * delayed, won't be disturbed by someone else grabbing
2073 * manager_mutex.
2074 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002075 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002076 return ret;
2077
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002078 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002079 * With manager arbitration won, manager_mutex would be free in
2080 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002081 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002082 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002083 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002084 mutex_lock(&pool->manager_mutex);
Joonsoo Kim8f174b12013-05-01 00:07:00 +09002085 spin_lock_irq(&pool->lock);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002086 ret = true;
2087 }
2088
Tejun Heo11ebea52012-07-12 14:46:37 -07002089 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002090
2091 /*
2092 * Destroy and then create so that may_start_working() is true
2093 * on return.
2094 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002095 ret |= maybe_destroy_workers(pool);
2096 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002097
Tejun Heobc3a1af2013-03-13 19:47:39 -07002098 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002099 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002100 return ret;
2101}
2102
Tejun Heoa62428c2010-06-29 10:07:10 +02002103/**
2104 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002105 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002106 * @work: work to process
2107 *
2108 * Process @work. This function contains all the logics necessary to
2109 * process a single work including synchronization against and
2110 * interaction with other workers on the same cpu, queueing and
2111 * flushing. As long as context requirement is met, any worker can
2112 * call this function to process a work.
2113 *
2114 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002115 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002116 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002117static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002118__releases(&pool->lock)
2119__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002120{
Tejun Heo112202d2013-02-13 19:29:12 -08002121 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002122 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002123 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002124 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002125 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002126#ifdef CONFIG_LOCKDEP
2127 /*
2128 * It is permissible to free the struct work_struct from
2129 * inside the function that is called from it, this we need to
2130 * take into account for lockdep too. To avoid bogus "held
2131 * lock freed" warnings as well as problems when looking into
2132 * work->lockdep_map, make a copy and use that here.
2133 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002134 struct lockdep_map lockdep_map;
2135
2136 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002137#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002138 /*
2139 * Ensure we're on the correct CPU. DISASSOCIATED test is
2140 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002141 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002142 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002143 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002144 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002145 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002146
Tejun Heo7e116292010-06-29 10:07:13 +02002147 /*
2148 * A single work shouldn't be executed concurrently by
2149 * multiple workers on a single cpu. Check whether anyone is
2150 * already processing the work. If so, defer the work to the
2151 * currently executing one.
2152 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002153 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002154 if (unlikely(collision)) {
2155 move_linked_works(work, &collision->scheduled, NULL);
2156 return;
2157 }
2158
Tejun Heo8930cab2012-08-03 10:30:45 -07002159 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002160 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002161 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002162 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002163 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002164 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002165 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002166
Tejun Heoa62428c2010-06-29 10:07:10 +02002167 list_del_init(&work->entry);
2168
Tejun Heo649027d2010-06-29 10:07:14 +02002169 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002170 * CPU intensive works don't participate in concurrency
2171 * management. They're the scheduler's responsibility.
2172 */
2173 if (unlikely(cpu_intensive))
2174 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2175
Tejun Heo974271c2012-07-12 14:46:37 -07002176 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002177 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002178 * executed ASAP. Wake up another worker if necessary.
2179 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002180 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2181 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002182
Tejun Heo8930cab2012-08-03 10:30:45 -07002183 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002184 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002185 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002186 * PENDING and queued state changes happen together while IRQ is
2187 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002188 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002189 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002190
Tejun Heod565ed62013-01-24 11:01:33 -08002191 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002192
Tejun Heo112202d2013-02-13 19:29:12 -08002193 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002194 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002195 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002196 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002197 /*
2198 * While we must be careful to not use "work" after this, the trace
2199 * point will only record its address.
2200 */
2201 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002202 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002203 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002204
2205 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002206 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2207 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002208 current->comm, preempt_count(), task_pid_nr(current),
2209 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002210 debug_show_held_locks(current);
2211 dump_stack();
2212 }
2213
Tejun Heob22ce272013-08-28 17:33:37 -04002214 /*
2215 * The following prevents a kworker from hogging CPU on !PREEMPT
2216 * kernels, where a requeueing work item waiting for something to
2217 * happen could deadlock with stop_machine as such work item could
2218 * indefinitely requeue itself while all other CPUs are trapped in
2219 * stop_machine.
2220 */
2221 cond_resched();
2222
Tejun Heod565ed62013-01-24 11:01:33 -08002223 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002224
Tejun Heofb0e7be2010-06-29 10:07:15 +02002225 /* clear cpu intensive status */
2226 if (unlikely(cpu_intensive))
2227 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2228
Tejun Heoa62428c2010-06-29 10:07:10 +02002229 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002230 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002231 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002232 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002233 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002234 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002235 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002236}
2237
Tejun Heoaffee4b2010-06-29 10:07:12 +02002238/**
2239 * process_scheduled_works - process scheduled works
2240 * @worker: self
2241 *
2242 * Process all scheduled works. Please note that the scheduled list
2243 * may change while processing a work, so this function repeatedly
2244 * fetches a work from the top and executes it.
2245 *
2246 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002247 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002248 * multiple times.
2249 */
2250static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002252 while (!list_empty(&worker->scheduled)) {
2253 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002255 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Tejun Heo4690c4a2010-06-29 10:07:10 +02002259/**
2260 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002261 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002262 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002263 * The worker thread function. All workers belong to a worker_pool -
2264 * either a per-cpu one or dynamic unbound one. These workers process all
2265 * work items regardless of their specific target workqueue. The only
2266 * exception is work items which belong to workqueues with a rescuer which
2267 * will be explained in rescuer_thread().
Yacine Belkadid185af32013-07-31 14:59:24 -07002268 *
2269 * Return: 0
Tejun Heo4690c4a2010-06-29 10:07:10 +02002270 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002271static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Tejun Heoc34056a2010-06-29 10:07:11 +02002273 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002274 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Tejun Heoe22bee72010-06-29 10:07:14 +02002276 /* tell the scheduler that this is a workqueue worker */
2277 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002278woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002279 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Tejun Heoa9ab7752013-03-19 13:45:21 -07002281 /* am I supposed to die? */
2282 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002283 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002284 WARN_ON_ONCE(!list_empty(&worker->entry));
2285 worker->task->flags &= ~PF_WQ_WORKER;
2286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
Tejun Heoc8e55f32010-06-29 10:07:12 +02002289 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002290recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002291 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002292 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002293 goto sleep;
2294
2295 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002296 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002297 goto recheck;
2298
Tejun Heoc8e55f32010-06-29 10:07:12 +02002299 /*
2300 * ->scheduled list can only be filled while a worker is
2301 * preparing to process a work or actually processing it.
2302 * Make sure nobody diddled with it while I was sleeping.
2303 */
Tejun Heo6183c002013-03-12 11:29:57 -07002304 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002305
Tejun Heoe22bee72010-06-29 10:07:14 +02002306 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002307 * Finish PREP stage. We're guaranteed to have at least one idle
2308 * worker or that someone else has already assumed the manager
2309 * role. This is where @worker starts participating in concurrency
2310 * management if applicable and concurrency management is restored
2311 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002313 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002314
2315 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002316 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002317 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002318 struct work_struct, entry);
2319
2320 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2321 /* optimization path, not strictly necessary */
2322 process_one_work(worker, work);
2323 if (unlikely(!list_empty(&worker->scheduled)))
2324 process_scheduled_works(worker);
2325 } else {
2326 move_linked_works(work, &worker->scheduled, NULL);
2327 process_scheduled_works(worker);
2328 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002329 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330
Tejun Heoe22bee72010-06-29 10:07:14 +02002331 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002332sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002333 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002334 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002335
Tejun Heoc8e55f32010-06-29 10:07:12 +02002336 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002337 * pool->lock is held and there's no work to process and no need to
2338 * manage, sleep. Workers are woken up only while holding
2339 * pool->lock or from local cpu, so setting the current state
2340 * before releasing pool->lock is enough to prevent losing any
2341 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002342 */
2343 worker_enter_idle(worker);
2344 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002345 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002346 schedule();
2347 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Tejun Heoe22bee72010-06-29 10:07:14 +02002350/**
2351 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002352 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002353 *
2354 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002355 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002356 *
Tejun Heo706026c2013-01-24 11:01:34 -08002357 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 * worker which uses GFP_KERNEL allocation which has slight chance of
2359 * developing into deadlock if some works currently on the same queue
2360 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2361 * the problem rescuer solves.
2362 *
Tejun Heo706026c2013-01-24 11:01:34 -08002363 * When such condition is possible, the pool summons rescuers of all
2364 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002365 * those works so that forward progress can be guaranteed.
2366 *
2367 * This should happen rarely.
Yacine Belkadid185af32013-07-31 14:59:24 -07002368 *
2369 * Return: 0
Tejun Heoe22bee72010-06-29 10:07:14 +02002370 */
Tejun Heo111c2252013-01-17 17:16:24 -08002371static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002372{
Tejun Heo111c2252013-01-17 17:16:24 -08002373 struct worker *rescuer = __rescuer;
2374 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002375 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002376
2377 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002378
2379 /*
2380 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2381 * doesn't participate in concurrency management.
2382 */
2383 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002384repeat:
2385 set_current_state(TASK_INTERRUPTIBLE);
2386
Mike Galbraith412d32e2012-11-28 07:17:18 +01002387 if (kthread_should_stop()) {
2388 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002389 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002390 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002391 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002392
Tejun Heo493a1722013-03-12 11:29:59 -07002393 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002394 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002395
2396 while (!list_empty(&wq->maydays)) {
2397 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2398 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002399 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002400 struct work_struct *work, *n;
2401
2402 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002403 list_del_init(&pwq->mayday_node);
2404
Tejun Heo2e109a22013-03-13 19:47:40 -07002405 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002406
2407 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002408 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002409 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002410
2411 /*
2412 * Slurp in all works issued via this workqueue and
2413 * process'em.
2414 */
Tejun Heo6183c002013-03-12 11:29:57 -07002415 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002416 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002417 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002418 move_linked_works(work, scheduled, &n);
2419
2420 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002421
2422 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002423 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002424 * regular worker; otherwise, we end up with 0 concurrency
2425 * and stalling the execution.
2426 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002427 if (keep_working(pool))
2428 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002429
Lai Jiangshanb3104102013-02-19 12:17:02 -08002430 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002431 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002432 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002433 }
2434
Tejun Heo2e109a22013-03-13 19:47:40 -07002435 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002436
Tejun Heo111c2252013-01-17 17:16:24 -08002437 /* rescuers should never participate in concurrency management */
2438 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002439 schedule();
2440 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002443struct wq_barrier {
2444 struct work_struct work;
2445 struct completion done;
2446};
2447
2448static void wq_barrier_func(struct work_struct *work)
2449{
2450 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2451 complete(&barr->done);
2452}
2453
Tejun Heo4690c4a2010-06-29 10:07:10 +02002454/**
2455 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002456 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002457 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002458 * @target: target work to attach @barr to
2459 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002460 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002461 * @barr is linked to @target such that @barr is completed only after
2462 * @target finishes execution. Please note that the ordering
2463 * guarantee is observed only with respect to @target and on the local
2464 * cpu.
2465 *
2466 * Currently, a queued barrier can't be canceled. This is because
2467 * try_to_grab_pending() can't determine whether the work to be
2468 * grabbed is at the head of the queue and thus can't clear LINKED
2469 * flag of the previous work while there must be a valid next work
2470 * after a work with LINKED flag set.
2471 *
2472 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002473 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002474 *
2475 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002476 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002477 */
Tejun Heo112202d2013-02-13 19:29:12 -08002478static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002479 struct wq_barrier *barr,
2480 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002481{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002482 struct list_head *head;
2483 unsigned int linked = 0;
2484
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002485 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002486 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002487 * as we know for sure that this will not trigger any of the
2488 * checks and call back into the fixup functions where we
2489 * might deadlock.
2490 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002491 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002492 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002493 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002494
Tejun Heoaffee4b2010-06-29 10:07:12 +02002495 /*
2496 * If @target is currently being executed, schedule the
2497 * barrier to the worker; otherwise, put it after @target.
2498 */
2499 if (worker)
2500 head = worker->scheduled.next;
2501 else {
2502 unsigned long *bits = work_data_bits(target);
2503
2504 head = target->entry.next;
2505 /* there can already be other linked works, inherit and set */
2506 linked = *bits & WORK_STRUCT_LINKED;
2507 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2508 }
2509
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002510 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002511 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002512 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002513}
2514
Tejun Heo73f53c42010-06-29 10:07:11 +02002515/**
Tejun Heo112202d2013-02-13 19:29:12 -08002516 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002517 * @wq: workqueue being flushed
2518 * @flush_color: new flush color, < 0 for no-op
2519 * @work_color: new work color, < 0 for no-op
2520 *
Tejun Heo112202d2013-02-13 19:29:12 -08002521 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 *
Tejun Heo112202d2013-02-13 19:29:12 -08002523 * If @flush_color is non-negative, flush_color on all pwqs should be
2524 * -1. If no pwq has in-flight commands at the specified color, all
2525 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2526 * has in flight commands, its pwq->flush_color is set to
2527 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002528 * wakeup logic is armed and %true is returned.
2529 *
2530 * The caller should have initialized @wq->first_flusher prior to
2531 * calling this function with non-negative @flush_color. If
2532 * @flush_color is negative, no flush color update is done and %false
2533 * is returned.
2534 *
Tejun Heo112202d2013-02-13 19:29:12 -08002535 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002536 * work_color which is previous to @work_color and all will be
2537 * advanced to @work_color.
2538 *
2539 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002540 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002541 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002542 * Return:
Tejun Heo73f53c42010-06-29 10:07:11 +02002543 * %true if @flush_color >= 0 and there's something to flush. %false
2544 * otherwise.
2545 */
Tejun Heo112202d2013-02-13 19:29:12 -08002546static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002547 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Tejun Heo73f53c42010-06-29 10:07:11 +02002549 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002550 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002551
Tejun Heo73f53c42010-06-29 10:07:11 +02002552 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002553 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002554 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002555 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002556
Tejun Heo49e3cf42013-03-12 11:29:58 -07002557 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002558 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002559
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002560 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002561
2562 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002563 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002564
Tejun Heo112202d2013-02-13 19:29:12 -08002565 if (pwq->nr_in_flight[flush_color]) {
2566 pwq->flush_color = flush_color;
2567 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002568 wait = true;
2569 }
2570 }
2571
2572 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002573 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002574 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002575 }
2576
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002577 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578 }
2579
Tejun Heo112202d2013-02-13 19:29:12 -08002580 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002581 complete(&wq->first_flusher->done);
2582
2583 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002588 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002590 * This function sleeps until all work items which were queued on entry
2591 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002593void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Tejun Heo73f53c42010-06-29 10:07:11 +02002595 struct wq_flusher this_flusher = {
2596 .list = LIST_HEAD_INIT(this_flusher.list),
2597 .flush_color = -1,
2598 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2599 };
2600 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002601
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002602 lock_map_acquire(&wq->lockdep_map);
2603 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002604
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002605 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606
2607 /*
2608 * Start-to-wait phase
2609 */
2610 next_color = work_next_color(wq->work_color);
2611
2612 if (next_color != wq->flush_color) {
2613 /*
2614 * Color space is not full. The current work_color
2615 * becomes our flush_color and work_color is advanced
2616 * by one.
2617 */
Tejun Heo6183c002013-03-12 11:29:57 -07002618 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002619 this_flusher.flush_color = wq->work_color;
2620 wq->work_color = next_color;
2621
2622 if (!wq->first_flusher) {
2623 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002624 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002625
2626 wq->first_flusher = &this_flusher;
2627
Tejun Heo112202d2013-02-13 19:29:12 -08002628 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002629 wq->work_color)) {
2630 /* nothing to flush, done */
2631 wq->flush_color = next_color;
2632 wq->first_flusher = NULL;
2633 goto out_unlock;
2634 }
2635 } else {
2636 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002637 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002638 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002639 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002640 }
2641 } else {
2642 /*
2643 * Oops, color space is full, wait on overflow queue.
2644 * The next flush completion will assign us
2645 * flush_color and transfer to flusher_queue.
2646 */
2647 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2648 }
2649
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002650 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002651
2652 wait_for_completion(&this_flusher.done);
2653
2654 /*
2655 * Wake-up-and-cascade phase
2656 *
2657 * First flushers are responsible for cascading flushes and
2658 * handling overflow. Non-first flushers can simply return.
2659 */
2660 if (wq->first_flusher != &this_flusher)
2661 return;
2662
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002663 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002664
Tejun Heo4ce48b32010-07-02 10:03:51 +02002665 /* we might have raced, check again with mutex held */
2666 if (wq->first_flusher != &this_flusher)
2667 goto out_unlock;
2668
Tejun Heo73f53c42010-06-29 10:07:11 +02002669 wq->first_flusher = NULL;
2670
Tejun Heo6183c002013-03-12 11:29:57 -07002671 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2672 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002673
2674 while (true) {
2675 struct wq_flusher *next, *tmp;
2676
2677 /* complete all the flushers sharing the current flush color */
2678 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2679 if (next->flush_color != wq->flush_color)
2680 break;
2681 list_del_init(&next->list);
2682 complete(&next->done);
2683 }
2684
Tejun Heo6183c002013-03-12 11:29:57 -07002685 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2686 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002687
2688 /* this flush_color is finished, advance by one */
2689 wq->flush_color = work_next_color(wq->flush_color);
2690
2691 /* one color has been freed, handle overflow queue */
2692 if (!list_empty(&wq->flusher_overflow)) {
2693 /*
2694 * Assign the same color to all overflowed
2695 * flushers, advance work_color and append to
2696 * flusher_queue. This is the start-to-wait
2697 * phase for these overflowed flushers.
2698 */
2699 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2700 tmp->flush_color = wq->work_color;
2701
2702 wq->work_color = work_next_color(wq->work_color);
2703
2704 list_splice_tail_init(&wq->flusher_overflow,
2705 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002706 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002707 }
2708
2709 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002710 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002711 break;
2712 }
2713
2714 /*
2715 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002716 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002717 */
Tejun Heo6183c002013-03-12 11:29:57 -07002718 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2719 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002720
2721 list_del_init(&next->list);
2722 wq->first_flusher = next;
2723
Tejun Heo112202d2013-02-13 19:29:12 -08002724 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002725 break;
2726
2727 /*
2728 * Meh... this color is already done, clear first
2729 * flusher and repeat cascading.
2730 */
2731 wq->first_flusher = NULL;
2732 }
2733
2734out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002735 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
Dave Jonesae90dd52006-06-30 01:40:45 -04002737EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002739/**
2740 * drain_workqueue - drain a workqueue
2741 * @wq: workqueue to drain
2742 *
2743 * Wait until the workqueue becomes empty. While draining is in progress,
2744 * only chain queueing is allowed. IOW, only currently pending or running
2745 * work items on @wq can queue further work items on it. @wq is flushed
2746 * repeatedly until it becomes empty. The number of flushing is detemined
2747 * by the depth of chaining and should be relatively short. Whine if it
2748 * takes too long.
2749 */
2750void drain_workqueue(struct workqueue_struct *wq)
2751{
2752 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002753 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002754
2755 /*
2756 * __queue_work() needs to test whether there are drainers, is much
2757 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002758 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002759 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002760 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002761 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002762 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002763 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002764reflush:
2765 flush_workqueue(wq);
2766
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002767 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002768
Tejun Heo49e3cf42013-03-12 11:29:58 -07002769 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002770 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002771
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002772 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002773 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002774 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002775
2776 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002777 continue;
2778
2779 if (++flush_cnt == 10 ||
2780 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002781 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002782 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002783
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002784 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002785 goto reflush;
2786 }
2787
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002788 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002789 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002790 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002791}
2792EXPORT_SYMBOL_GPL(drain_workqueue);
2793
Tejun Heo606a5022012-08-20 14:51:23 -07002794static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002795{
2796 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002797 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002798 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002799
2800 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002801
Tejun Heofa1b54e2013-03-12 11:30:00 -07002802 local_irq_disable();
2803 pool = get_work_pool(work);
2804 if (!pool) {
2805 local_irq_enable();
2806 return false;
2807 }
2808
2809 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002810 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002811 pwq = get_work_pwq(work);
2812 if (pwq) {
2813 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002814 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002815 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002816 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002817 if (!worker)
2818 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002819 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002820 }
Tejun Heobaf59022010-09-16 10:42:16 +02002821
Tejun Heo112202d2013-02-13 19:29:12 -08002822 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002823 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002824
Tejun Heoe1594892011-01-09 23:32:15 +01002825 /*
2826 * If @max_active is 1 or rescuer is in use, flushing another work
2827 * item on the same workqueue may lead to deadlock. Make sure the
2828 * flusher is not running on the same workqueue by verifying write
2829 * access.
2830 */
Tejun Heo493008a2013-03-12 11:30:03 -07002831 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002832 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002833 else
Tejun Heo112202d2013-02-13 19:29:12 -08002834 lock_map_acquire_read(&pwq->wq->lockdep_map);
2835 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002836
Tejun Heobaf59022010-09-16 10:42:16 +02002837 return true;
2838already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002839 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002840 return false;
2841}
2842
Lai Jiangshanc2fda502013-07-24 18:31:42 +08002843static bool __flush_work(struct work_struct *work)
2844{
2845 struct wq_barrier barr;
2846
2847 if (start_flush_work(work, &barr)) {
2848 wait_for_completion(&barr.done);
2849 destroy_work_on_stack(&barr.work);
2850 return true;
2851 } else {
2852 return false;
2853 }
2854}
2855
Oleg Nesterovdb700892008-07-25 01:47:49 -07002856/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002857 * flush_work - wait for a work to finish executing the last queueing instance
2858 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002859 *
Tejun Heo606a5022012-08-20 14:51:23 -07002860 * Wait until @work has finished execution. @work is guaranteed to be idle
2861 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002862 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002863 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 * %true if flush_work() waited for the work to finish execution,
2865 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002866 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002867bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002868{
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002869 lock_map_acquire(&work->lockdep_map);
2870 lock_map_release(&work->lockdep_map);
2871
Lai Jiangshanc2fda502013-07-24 18:31:42 +08002872 return __flush_work(work);
Oleg Nesterovdb700892008-07-25 01:47:49 -07002873}
2874EXPORT_SYMBOL_GPL(flush_work);
2875
Tejun Heo36e227d2012-08-03 10:30:46 -07002876static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002877{
Tejun Heobbb68df2012-08-03 10:30:46 -07002878 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002879 int ret;
2880
2881 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002882 ret = try_to_grab_pending(work, is_dwork, &flags);
2883 /*
2884 * If someone else is canceling, wait for the same event it
2885 * would be waiting for before retrying.
2886 */
2887 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002888 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002889 } while (unlikely(ret < 0));
2890
Tejun Heobbb68df2012-08-03 10:30:46 -07002891 /* tell other tasks trying to grab @work to back off */
2892 mark_work_canceling(work);
2893 local_irq_restore(flags);
2894
Tejun Heo606a5022012-08-20 14:51:23 -07002895 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002896 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002897 return ret;
2898}
2899
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * cancel_work_sync - cancel a work and wait for it to finish
2902 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 * Cancel @work and wait for its execution to finish. This function
2905 * can be used even if the work re-queues itself or migrates to
2906 * another workqueue. On return from this function, @work is
2907 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002908 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002909 * cancel_work_sync(&delayed_work->work) must not be used for
2910 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002912 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002913 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002914 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002915 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002917 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002918bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002919{
Tejun Heo36e227d2012-08-03 10:30:46 -07002920 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002921}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002922EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002923
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002924/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002925 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2926 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002927 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002928 * Delayed timer is cancelled and the pending work is queued for
2929 * immediate execution. Like flush_work(), this function only
2930 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002931 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002932 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002933 * %true if flush_work() waited for the work to finish execution,
2934 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002935 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002936bool flush_delayed_work(struct delayed_work *dwork)
2937{
Tejun Heo8930cab2012-08-03 10:30:45 -07002938 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002939 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002940 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002941 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002942 return flush_work(&dwork->work);
2943}
2944EXPORT_SYMBOL(flush_delayed_work);
2945
2946/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947 * cancel_delayed_work - cancel a delayed work
2948 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002949 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002950 * Kill off a pending delayed_work.
2951 *
2952 * Return: %true if @dwork was pending and canceled; %false if it wasn't
2953 * pending.
2954 *
2955 * Note:
2956 * The work callback function may still be running on return, unless
2957 * it returns %true and the work doesn't re-arm itself. Explicitly flush or
2958 * use cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002959 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002960 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002961 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002962bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002963{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002964 unsigned long flags;
2965 int ret;
2966
2967 do {
2968 ret = try_to_grab_pending(&dwork->work, true, &flags);
2969 } while (unlikely(ret == -EAGAIN));
2970
2971 if (unlikely(ret < 0))
2972 return false;
2973
Tejun Heo7c3eed52013-01-24 11:01:33 -08002974 set_work_pool_and_clear_pending(&dwork->work,
2975 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002976 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002977 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002978}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002979EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002980
2981/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002982 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2983 * @dwork: the delayed work cancel
2984 *
2985 * This is cancel_work_sync() for delayed works.
2986 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002987 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002988 * %true if @dwork was pending, %false otherwise.
2989 */
2990bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002991{
Tejun Heo36e227d2012-08-03 10:30:46 -07002992 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002993}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002994EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002996/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002997 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002998 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002999 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003000 * schedule_on_each_cpu() executes @func on each online CPU using the
3001 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003002 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003003 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003004 * Return:
Tejun Heo31ddd872010-10-19 11:14:49 +02003005 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003006 */
David Howells65f27f32006-11-22 14:55:48 +00003007int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003008{
3009 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003010 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003011
Andrew Mortonb6136772006-06-25 05:47:49 -07003012 works = alloc_percpu(struct work_struct);
3013 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003014 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003015
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003016 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003017
Christoph Lameter15316ba2006-01-08 01:00:43 -08003018 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003019 struct work_struct *work = per_cpu_ptr(works, cpu);
3020
3021 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003022 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003023 }
Tejun Heo93981802009-11-17 14:06:20 -08003024
3025 for_each_online_cpu(cpu)
3026 flush_work(per_cpu_ptr(works, cpu));
3027
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003028 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003029 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003030 return 0;
3031}
3032
Alan Sterneef6a7d2010-02-12 17:39:21 +09003033/**
3034 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3035 *
3036 * Forces execution of the kernel-global workqueue and blocks until its
3037 * completion.
3038 *
3039 * Think twice before calling this function! It's very easy to get into
3040 * trouble if you don't take great care. Either of the following situations
3041 * will lead to deadlock:
3042 *
3043 * One of the work items currently on the workqueue needs to acquire
3044 * a lock held by your code or its caller.
3045 *
3046 * Your code is running in the context of a work routine.
3047 *
3048 * They will be detected by lockdep when they occur, but the first might not
3049 * occur very often. It depends on what work items are on the workqueue and
3050 * what locks they need, which you have no control over.
3051 *
3052 * In most situations flushing the entire workqueue is overkill; you merely
3053 * need to know that a particular work item isn't queued and isn't running.
3054 * In such cases you should use cancel_delayed_work_sync() or
3055 * cancel_work_sync() instead.
3056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057void flush_scheduled_work(void)
3058{
Tejun Heod320c032010-06-29 10:07:14 +02003059 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
Dave Jonesae90dd52006-06-30 01:40:45 -04003061EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003064 * execute_in_process_context - reliably execute the routine with user context
3065 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003066 * @ew: guaranteed storage for the execute work structure (must
3067 * be available when the work executes)
3068 *
3069 * Executes the function immediately if process context is available,
3070 * otherwise schedules the function for delayed execution.
3071 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003072 * Return: 0 - function was executed
James Bottomley1fa44ec2006-02-23 12:43:43 -06003073 * 1 - function was scheduled for execution
3074 */
David Howells65f27f32006-11-22 14:55:48 +00003075int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003076{
3077 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003078 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003079 return 0;
3080 }
3081
David Howells65f27f32006-11-22 14:55:48 +00003082 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003083 schedule_work(&ew->work);
3084
3085 return 1;
3086}
3087EXPORT_SYMBOL_GPL(execute_in_process_context);
3088
Tejun Heo226223a2013-03-12 11:30:05 -07003089#ifdef CONFIG_SYSFS
3090/*
3091 * Workqueues with WQ_SYSFS flag set is visible to userland via
3092 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3093 * following attributes.
3094 *
3095 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3096 * max_active RW int : maximum number of in-flight work items
3097 *
3098 * Unbound workqueues have the following extra attributes.
3099 *
3100 * id RO int : the associated pool ID
3101 * nice RW int : nice value of the workers
3102 * cpumask RW mask : bitmask of allowed CPUs for the workers
3103 */
3104struct wq_device {
3105 struct workqueue_struct *wq;
3106 struct device dev;
3107};
3108
3109static struct workqueue_struct *dev_to_wq(struct device *dev)
3110{
3111 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3112
3113 return wq_dev->wq;
3114}
3115
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003116static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
3117 char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003118{
3119 struct workqueue_struct *wq = dev_to_wq(dev);
3120
3121 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3122}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003123static DEVICE_ATTR_RO(per_cpu);
Tejun Heo226223a2013-03-12 11:30:05 -07003124
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003125static ssize_t max_active_show(struct device *dev,
3126 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003127{
3128 struct workqueue_struct *wq = dev_to_wq(dev);
3129
3130 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3131}
3132
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003133static ssize_t max_active_store(struct device *dev,
3134 struct device_attribute *attr, const char *buf,
3135 size_t count)
Tejun Heo226223a2013-03-12 11:30:05 -07003136{
3137 struct workqueue_struct *wq = dev_to_wq(dev);
3138 int val;
3139
3140 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3141 return -EINVAL;
3142
3143 workqueue_set_max_active(wq, val);
3144 return count;
3145}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003146static DEVICE_ATTR_RW(max_active);
Tejun Heo226223a2013-03-12 11:30:05 -07003147
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003148static struct attribute *wq_sysfs_attrs[] = {
3149 &dev_attr_per_cpu.attr,
3150 &dev_attr_max_active.attr,
3151 NULL,
Tejun Heo226223a2013-03-12 11:30:05 -07003152};
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003153ATTRIBUTE_GROUPS(wq_sysfs);
Tejun Heo226223a2013-03-12 11:30:05 -07003154
Tejun Heod55262c2013-04-01 11:23:38 -07003155static ssize_t wq_pool_ids_show(struct device *dev,
3156 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003157{
3158 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003159 const char *delim = "";
3160 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003161
3162 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003163 for_each_node(node) {
3164 written += scnprintf(buf + written, PAGE_SIZE - written,
3165 "%s%d:%d", delim, node,
3166 unbound_pwq_by_node(wq, node)->pool->id);
3167 delim = " ";
3168 }
3169 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003170 rcu_read_unlock_sched();
3171
3172 return written;
3173}
3174
3175static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3176 char *buf)
3177{
3178 struct workqueue_struct *wq = dev_to_wq(dev);
3179 int written;
3180
Tejun Heo6029a912013-04-01 11:23:34 -07003181 mutex_lock(&wq->mutex);
3182 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3183 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003184
3185 return written;
3186}
3187
3188/* prepare workqueue_attrs for sysfs store operations */
3189static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3190{
3191 struct workqueue_attrs *attrs;
3192
3193 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3194 if (!attrs)
3195 return NULL;
3196
Tejun Heo6029a912013-04-01 11:23:34 -07003197 mutex_lock(&wq->mutex);
3198 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3199 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003200 return attrs;
3201}
3202
3203static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3204 const char *buf, size_t count)
3205{
3206 struct workqueue_struct *wq = dev_to_wq(dev);
3207 struct workqueue_attrs *attrs;
3208 int ret;
3209
3210 attrs = wq_sysfs_prep_attrs(wq);
3211 if (!attrs)
3212 return -ENOMEM;
3213
3214 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3215 attrs->nice >= -20 && attrs->nice <= 19)
3216 ret = apply_workqueue_attrs(wq, attrs);
3217 else
3218 ret = -EINVAL;
3219
3220 free_workqueue_attrs(attrs);
3221 return ret ?: count;
3222}
3223
3224static ssize_t wq_cpumask_show(struct device *dev,
3225 struct device_attribute *attr, char *buf)
3226{
3227 struct workqueue_struct *wq = dev_to_wq(dev);
3228 int written;
3229
Tejun Heo6029a912013-04-01 11:23:34 -07003230 mutex_lock(&wq->mutex);
3231 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3232 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003233
3234 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3235 return written;
3236}
3237
3238static ssize_t wq_cpumask_store(struct device *dev,
3239 struct device_attribute *attr,
3240 const char *buf, size_t count)
3241{
3242 struct workqueue_struct *wq = dev_to_wq(dev);
3243 struct workqueue_attrs *attrs;
3244 int ret;
3245
3246 attrs = wq_sysfs_prep_attrs(wq);
3247 if (!attrs)
3248 return -ENOMEM;
3249
3250 ret = cpumask_parse(buf, attrs->cpumask);
3251 if (!ret)
3252 ret = apply_workqueue_attrs(wq, attrs);
3253
3254 free_workqueue_attrs(attrs);
3255 return ret ?: count;
3256}
3257
Tejun Heod55262c2013-04-01 11:23:38 -07003258static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3259 char *buf)
3260{
3261 struct workqueue_struct *wq = dev_to_wq(dev);
3262 int written;
3263
3264 mutex_lock(&wq->mutex);
3265 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3266 !wq->unbound_attrs->no_numa);
3267 mutex_unlock(&wq->mutex);
3268
3269 return written;
3270}
3271
3272static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3273 const char *buf, size_t count)
3274{
3275 struct workqueue_struct *wq = dev_to_wq(dev);
3276 struct workqueue_attrs *attrs;
3277 int v, ret;
3278
3279 attrs = wq_sysfs_prep_attrs(wq);
3280 if (!attrs)
3281 return -ENOMEM;
3282
3283 ret = -EINVAL;
3284 if (sscanf(buf, "%d", &v) == 1) {
3285 attrs->no_numa = !v;
3286 ret = apply_workqueue_attrs(wq, attrs);
3287 }
3288
3289 free_workqueue_attrs(attrs);
3290 return ret ?: count;
3291}
3292
Tejun Heo226223a2013-03-12 11:30:05 -07003293static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003294 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003295 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3296 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003297 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003298 __ATTR_NULL,
3299};
3300
3301static struct bus_type wq_subsys = {
3302 .name = "workqueue",
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003303 .dev_groups = wq_sysfs_groups,
Tejun Heo226223a2013-03-12 11:30:05 -07003304};
3305
3306static int __init wq_sysfs_init(void)
3307{
3308 return subsys_virtual_register(&wq_subsys, NULL);
3309}
3310core_initcall(wq_sysfs_init);
3311
3312static void wq_device_release(struct device *dev)
3313{
3314 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3315
3316 kfree(wq_dev);
3317}
3318
3319/**
3320 * workqueue_sysfs_register - make a workqueue visible in sysfs
3321 * @wq: the workqueue to register
3322 *
3323 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3324 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3325 * which is the preferred method.
3326 *
3327 * Workqueue user should use this function directly iff it wants to apply
3328 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3329 * apply_workqueue_attrs() may race against userland updating the
3330 * attributes.
3331 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003332 * Return: 0 on success, -errno on failure.
Tejun Heo226223a2013-03-12 11:30:05 -07003333 */
3334int workqueue_sysfs_register(struct workqueue_struct *wq)
3335{
3336 struct wq_device *wq_dev;
3337 int ret;
3338
3339 /*
3340 * Adjusting max_active or creating new pwqs by applyting
3341 * attributes breaks ordering guarantee. Disallow exposing ordered
3342 * workqueues.
3343 */
3344 if (WARN_ON(wq->flags & __WQ_ORDERED))
3345 return -EINVAL;
3346
3347 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3348 if (!wq_dev)
3349 return -ENOMEM;
3350
3351 wq_dev->wq = wq;
3352 wq_dev->dev.bus = &wq_subsys;
3353 wq_dev->dev.init_name = wq->name;
3354 wq_dev->dev.release = wq_device_release;
3355
3356 /*
3357 * unbound_attrs are created separately. Suppress uevent until
3358 * everything is ready.
3359 */
3360 dev_set_uevent_suppress(&wq_dev->dev, true);
3361
3362 ret = device_register(&wq_dev->dev);
3363 if (ret) {
3364 kfree(wq_dev);
3365 wq->wq_dev = NULL;
3366 return ret;
3367 }
3368
3369 if (wq->flags & WQ_UNBOUND) {
3370 struct device_attribute *attr;
3371
3372 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3373 ret = device_create_file(&wq_dev->dev, attr);
3374 if (ret) {
3375 device_unregister(&wq_dev->dev);
3376 wq->wq_dev = NULL;
3377 return ret;
3378 }
3379 }
3380 }
3381
3382 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3383 return 0;
3384}
3385
3386/**
3387 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3388 * @wq: the workqueue to unregister
3389 *
3390 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3391 */
3392static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3393{
3394 struct wq_device *wq_dev = wq->wq_dev;
3395
3396 if (!wq->wq_dev)
3397 return;
3398
3399 wq->wq_dev = NULL;
3400 device_unregister(&wq_dev->dev);
3401}
3402#else /* CONFIG_SYSFS */
3403static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3404#endif /* CONFIG_SYSFS */
3405
Tejun Heo7a4e3442013-03-12 11:30:00 -07003406/**
3407 * free_workqueue_attrs - free a workqueue_attrs
3408 * @attrs: workqueue_attrs to free
3409 *
3410 * Undo alloc_workqueue_attrs().
3411 */
3412void free_workqueue_attrs(struct workqueue_attrs *attrs)
3413{
3414 if (attrs) {
3415 free_cpumask_var(attrs->cpumask);
3416 kfree(attrs);
3417 }
3418}
3419
3420/**
3421 * alloc_workqueue_attrs - allocate a workqueue_attrs
3422 * @gfp_mask: allocation mask to use
3423 *
3424 * Allocate a new workqueue_attrs, initialize with default settings and
Yacine Belkadid185af32013-07-31 14:59:24 -07003425 * return it.
3426 *
3427 * Return: The allocated new workqueue_attr on success. %NULL on failure.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003428 */
3429struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3430{
3431 struct workqueue_attrs *attrs;
3432
3433 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3434 if (!attrs)
3435 goto fail;
3436 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3437 goto fail;
3438
Tejun Heo13e2e552013-04-01 11:23:31 -07003439 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003440 return attrs;
3441fail:
3442 free_workqueue_attrs(attrs);
3443 return NULL;
3444}
3445
Tejun Heo29c91e92013-03-12 11:30:03 -07003446static void copy_workqueue_attrs(struct workqueue_attrs *to,
3447 const struct workqueue_attrs *from)
3448{
3449 to->nice = from->nice;
3450 cpumask_copy(to->cpumask, from->cpumask);
Shaohua Li2865a8f2013-08-01 09:56:36 +08003451 /*
3452 * Unlike hash and equality test, this function doesn't ignore
3453 * ->no_numa as it is used for both pool and wq attrs. Instead,
3454 * get_unbound_pool() explicitly clears ->no_numa after copying.
3455 */
3456 to->no_numa = from->no_numa;
Tejun Heo29c91e92013-03-12 11:30:03 -07003457}
3458
Tejun Heo29c91e92013-03-12 11:30:03 -07003459/* hash value of the content of @attr */
3460static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3461{
3462 u32 hash = 0;
3463
3464 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003465 hash = jhash(cpumask_bits(attrs->cpumask),
3466 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003467 return hash;
3468}
3469
3470/* content equality test */
3471static bool wqattrs_equal(const struct workqueue_attrs *a,
3472 const struct workqueue_attrs *b)
3473{
3474 if (a->nice != b->nice)
3475 return false;
3476 if (!cpumask_equal(a->cpumask, b->cpumask))
3477 return false;
3478 return true;
3479}
3480
Tejun Heo7a4e3442013-03-12 11:30:00 -07003481/**
3482 * init_worker_pool - initialize a newly zalloc'd worker_pool
3483 * @pool: worker_pool to initialize
3484 *
3485 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Yacine Belkadid185af32013-07-31 14:59:24 -07003486 *
3487 * Return: 0 on success, -errno on failure. Even on failure, all fields
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 * inside @pool proper are initialized and put_unbound_pool() can be called
3489 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003490 */
3491static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003492{
3493 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003494 pool->id = -1;
3495 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003496 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003497 pool->flags |= POOL_DISASSOCIATED;
3498 INIT_LIST_HEAD(&pool->worklist);
3499 INIT_LIST_HEAD(&pool->idle_list);
3500 hash_init(pool->busy_hash);
3501
3502 init_timer_deferrable(&pool->idle_timer);
3503 pool->idle_timer.function = idle_worker_timeout;
3504 pool->idle_timer.data = (unsigned long)pool;
3505
3506 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3507 (unsigned long)pool);
3508
3509 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003510 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003511 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003512
Tejun Heo29c91e92013-03-12 11:30:03 -07003513 INIT_HLIST_NODE(&pool->hash_node);
3514 pool->refcnt = 1;
3515
3516 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003517 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3518 if (!pool->attrs)
3519 return -ENOMEM;
3520 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003521}
3522
Tejun Heo29c91e92013-03-12 11:30:03 -07003523static void rcu_free_pool(struct rcu_head *rcu)
3524{
3525 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3526
Tejun Heo822d8402013-03-19 13:45:21 -07003527 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003528 free_workqueue_attrs(pool->attrs);
3529 kfree(pool);
3530}
3531
3532/**
3533 * put_unbound_pool - put a worker_pool
3534 * @pool: worker_pool to put
3535 *
3536 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003537 * safe manner. get_unbound_pool() calls this function on its failure path
3538 * and this function should be able to release pools which went through,
3539 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003540 *
3541 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003542 */
3543static void put_unbound_pool(struct worker_pool *pool)
3544{
3545 struct worker *worker;
3546
Tejun Heoa892cac2013-04-01 11:23:32 -07003547 lockdep_assert_held(&wq_pool_mutex);
3548
3549 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003550 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003551
3552 /* sanity checks */
3553 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003554 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003555 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003556
3557 /* release id and unhash */
3558 if (pool->id >= 0)
3559 idr_remove(&worker_pool_idr, pool->id);
3560 hash_del(&pool->hash_node);
3561
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003562 /*
3563 * Become the manager and destroy all workers. Grabbing
3564 * manager_arb prevents @pool's workers from blocking on
3565 * manager_mutex.
3566 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003567 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003568 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003569 spin_lock_irq(&pool->lock);
3570
3571 while ((worker = first_worker(pool)))
3572 destroy_worker(worker);
3573 WARN_ON(pool->nr_workers || pool->nr_idle);
3574
3575 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003576 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003577 mutex_unlock(&pool->manager_arb);
3578
3579 /* shut down the timers */
3580 del_timer_sync(&pool->idle_timer);
3581 del_timer_sync(&pool->mayday_timer);
3582
3583 /* sched-RCU protected to allow dereferences from get_work_pool() */
3584 call_rcu_sched(&pool->rcu, rcu_free_pool);
3585}
3586
3587/**
3588 * get_unbound_pool - get a worker_pool with the specified attributes
3589 * @attrs: the attributes of the worker_pool to get
3590 *
3591 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3592 * reference count and return it. If there already is a matching
3593 * worker_pool, it will be used; otherwise, this function attempts to
Yacine Belkadid185af32013-07-31 14:59:24 -07003594 * create a new one.
Tejun Heoa892cac2013-04-01 11:23:32 -07003595 *
3596 * Should be called with wq_pool_mutex held.
Yacine Belkadid185af32013-07-31 14:59:24 -07003597 *
3598 * Return: On success, a worker_pool with the same attributes as @attrs.
3599 * On failure, %NULL.
Tejun Heo29c91e92013-03-12 11:30:03 -07003600 */
3601static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3602{
Tejun Heo29c91e92013-03-12 11:30:03 -07003603 u32 hash = wqattrs_hash(attrs);
3604 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003605 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003606
Tejun Heoa892cac2013-04-01 11:23:32 -07003607 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003608
3609 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003610 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3611 if (wqattrs_equal(pool->attrs, attrs)) {
3612 pool->refcnt++;
3613 goto out_unlock;
3614 }
3615 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003616
3617 /* nope, create a new one */
3618 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3619 if (!pool || init_worker_pool(pool) < 0)
3620 goto fail;
3621
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003622 if (workqueue_freezing)
3623 pool->flags |= POOL_FREEZING;
3624
Tejun Heo8864b4e2013-03-12 11:30:04 -07003625 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003626 copy_workqueue_attrs(pool->attrs, attrs);
3627
Shaohua Li2865a8f2013-08-01 09:56:36 +08003628 /*
3629 * no_numa isn't a worker_pool attribute, always clear it. See
3630 * 'struct workqueue_attrs' comments for detail.
3631 */
3632 pool->attrs->no_numa = false;
3633
Tejun Heof3f90ad2013-04-01 11:23:34 -07003634 /* if cpumask is contained inside a NUMA node, we belong to that node */
3635 if (wq_numa_enabled) {
3636 for_each_node(node) {
3637 if (cpumask_subset(pool->attrs->cpumask,
3638 wq_numa_possible_cpumask[node])) {
3639 pool->node = node;
3640 break;
3641 }
3642 }
3643 }
3644
Tejun Heo29c91e92013-03-12 11:30:03 -07003645 if (worker_pool_assign_id(pool) < 0)
3646 goto fail;
3647
3648 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003649 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003650 goto fail;
3651
Tejun Heo29c91e92013-03-12 11:30:03 -07003652 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003653 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3654out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003655 return pool;
3656fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003657 if (pool)
3658 put_unbound_pool(pool);
3659 return NULL;
3660}
3661
Tejun Heo8864b4e2013-03-12 11:30:04 -07003662static void rcu_free_pwq(struct rcu_head *rcu)
3663{
3664 kmem_cache_free(pwq_cache,
3665 container_of(rcu, struct pool_workqueue, rcu));
3666}
3667
3668/*
3669 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3670 * and needs to be destroyed.
3671 */
3672static void pwq_unbound_release_workfn(struct work_struct *work)
3673{
3674 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3675 unbound_release_work);
3676 struct workqueue_struct *wq = pwq->wq;
3677 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003678 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003679
3680 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3681 return;
3682
Tejun Heo75ccf592013-03-12 11:30:04 -07003683 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003684 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003685 * necessary on release but do it anyway. It's easier to verify
3686 * and consistent with the linking path.
3687 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003688 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003689 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003690 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003691 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003692
Tejun Heoa892cac2013-04-01 11:23:32 -07003693 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003694 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003695 mutex_unlock(&wq_pool_mutex);
3696
Tejun Heo8864b4e2013-03-12 11:30:04 -07003697 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3698
3699 /*
3700 * If we're the last pwq going away, @wq is already dead and no one
3701 * is gonna access it anymore. Free it.
3702 */
Tejun Heo6029a912013-04-01 11:23:34 -07003703 if (is_last) {
3704 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003705 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003706 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003707}
3708
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003709/**
Tejun Heo699ce092013-03-13 16:51:35 -07003710 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003711 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003712 *
Tejun Heo699ce092013-03-13 16:51:35 -07003713 * If @pwq isn't freezing, set @pwq->max_active to the associated
3714 * workqueue's saved_max_active and activate delayed work items
3715 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003716 */
Tejun Heo699ce092013-03-13 16:51:35 -07003717static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003718{
Tejun Heo699ce092013-03-13 16:51:35 -07003719 struct workqueue_struct *wq = pwq->wq;
3720 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003721
Tejun Heo699ce092013-03-13 16:51:35 -07003722 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003723 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003724
3725 /* fast exit for non-freezable wqs */
3726 if (!freezable && pwq->max_active == wq->saved_max_active)
3727 return;
3728
Lai Jiangshana357fc02013-03-25 16:57:19 -07003729 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003730
3731 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3732 pwq->max_active = wq->saved_max_active;
3733
3734 while (!list_empty(&pwq->delayed_works) &&
3735 pwq->nr_active < pwq->max_active)
3736 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003737
3738 /*
3739 * Need to kick a worker after thawed or an unbound wq's
3740 * max_active is bumped. It's a slow path. Do it always.
3741 */
3742 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003743 } else {
3744 pwq->max_active = 0;
3745 }
3746
Lai Jiangshana357fc02013-03-25 16:57:19 -07003747 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003748}
3749
Tejun Heoe50aba92013-04-01 11:23:35 -07003750/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003751static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3752 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003753{
3754 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3755
Tejun Heoe50aba92013-04-01 11:23:35 -07003756 memset(pwq, 0, sizeof(*pwq));
3757
Tejun Heod2c1d402013-03-12 11:30:04 -07003758 pwq->pool = pool;
3759 pwq->wq = wq;
3760 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003761 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003762 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003763 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003764 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003765 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003766}
Tejun Heod2c1d402013-03-12 11:30:04 -07003767
Tejun Heof147f292013-04-01 11:23:35 -07003768/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003769static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003770{
3771 struct workqueue_struct *wq = pwq->wq;
3772
3773 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003774
Tejun Heo1befcf32013-04-01 11:23:35 -07003775 /* may be called multiple times, ignore if already linked */
3776 if (!list_empty(&pwq->pwqs_node))
3777 return;
3778
Tejun Heo983ca252013-03-13 16:51:35 -07003779 /*
3780 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003781 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003782 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003783 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003784
3785 /* sync max_active to the current setting */
3786 pwq_adjust_max_active(pwq);
3787
3788 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003789 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003790}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003791
Tejun Heof147f292013-04-01 11:23:35 -07003792/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3793static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3794 const struct workqueue_attrs *attrs)
3795{
3796 struct worker_pool *pool;
3797 struct pool_workqueue *pwq;
3798
3799 lockdep_assert_held(&wq_pool_mutex);
3800
3801 pool = get_unbound_pool(attrs);
3802 if (!pool)
3803 return NULL;
3804
Tejun Heoe50aba92013-04-01 11:23:35 -07003805 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003806 if (!pwq) {
3807 put_unbound_pool(pool);
3808 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003809 }
Tejun Heo6029a912013-04-01 11:23:34 -07003810
Tejun Heof147f292013-04-01 11:23:35 -07003811 init_pwq(pwq, wq, pool);
3812 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003813}
3814
Tejun Heo4c16bd32013-04-01 11:23:36 -07003815/* undo alloc_unbound_pwq(), used only in the error path */
3816static void free_unbound_pwq(struct pool_workqueue *pwq)
3817{
3818 lockdep_assert_held(&wq_pool_mutex);
3819
3820 if (pwq) {
3821 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003822 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003823 }
3824}
3825
3826/**
3827 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3828 * @attrs: the wq_attrs of interest
3829 * @node: the target NUMA node
3830 * @cpu_going_down: if >= 0, the CPU to consider as offline
3831 * @cpumask: outarg, the resulting cpumask
3832 *
3833 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3834 * @cpu_going_down is >= 0, that cpu is considered offline during
Yacine Belkadid185af32013-07-31 14:59:24 -07003835 * calculation. The result is stored in @cpumask.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003836 *
3837 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3838 * enabled and @node has online CPUs requested by @attrs, the returned
3839 * cpumask is the intersection of the possible CPUs of @node and
3840 * @attrs->cpumask.
3841 *
3842 * The caller is responsible for ensuring that the cpumask of @node stays
3843 * stable.
Yacine Belkadid185af32013-07-31 14:59:24 -07003844 *
3845 * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
3846 * %false if equal.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003847 */
3848static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3849 int cpu_going_down, cpumask_t *cpumask)
3850{
Tejun Heod55262c2013-04-01 11:23:38 -07003851 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003852 goto use_dfl;
3853
3854 /* does @node have any online CPUs @attrs wants? */
3855 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3856 if (cpu_going_down >= 0)
3857 cpumask_clear_cpu(cpu_going_down, cpumask);
3858
3859 if (cpumask_empty(cpumask))
3860 goto use_dfl;
3861
3862 /* yeap, return possible CPUs in @node that @attrs wants */
3863 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3864 return !cpumask_equal(cpumask, attrs->cpumask);
3865
3866use_dfl:
3867 cpumask_copy(cpumask, attrs->cpumask);
3868 return false;
3869}
3870
Tejun Heo1befcf32013-04-01 11:23:35 -07003871/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3872static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3873 int node,
3874 struct pool_workqueue *pwq)
3875{
3876 struct pool_workqueue *old_pwq;
3877
3878 lockdep_assert_held(&wq->mutex);
3879
3880 /* link_pwq() can handle duplicate calls */
3881 link_pwq(pwq);
3882
3883 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3884 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3885 return old_pwq;
3886}
3887
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003888/**
3889 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3890 * @wq: the target workqueue
3891 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3892 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003893 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3894 * machines, this function maps a separate pwq to each NUMA node with
3895 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3896 * NUMA node it was issued on. Older pwqs are released as in-flight work
3897 * items finish. Note that a work item which repeatedly requeues itself
3898 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003899 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003900 * Performs GFP_KERNEL allocations.
3901 *
3902 * Return: 0 on success and -errno on failure.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003903 */
3904int apply_workqueue_attrs(struct workqueue_struct *wq,
3905 const struct workqueue_attrs *attrs)
3906{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003907 struct workqueue_attrs *new_attrs, *tmp_attrs;
3908 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003909 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003910
Tejun Heo8719dce2013-03-12 11:30:04 -07003911 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003912 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3913 return -EINVAL;
3914
Tejun Heo8719dce2013-03-12 11:30:04 -07003915 /* creating multiple pwqs breaks ordering guarantee */
3916 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3917 return -EINVAL;
3918
Tejun Heo4c16bd32013-04-01 11:23:36 -07003919 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003920 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003921 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3922 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003923 goto enomem;
3924
Tejun Heo4c16bd32013-04-01 11:23:36 -07003925 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003926 copy_workqueue_attrs(new_attrs, attrs);
3927 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3928
Tejun Heo4c16bd32013-04-01 11:23:36 -07003929 /*
3930 * We may create multiple pwqs with differing cpumasks. Make a
3931 * copy of @new_attrs which will be modified and used to obtain
3932 * pools.
3933 */
3934 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003935
Tejun Heo4c16bd32013-04-01 11:23:36 -07003936 /*
3937 * CPUs should stay stable across pwq creations and installations.
3938 * Pin CPUs, determine the target cpumask for each node and create
3939 * pwqs accordingly.
3940 */
3941 get_online_cpus();
3942
3943 mutex_lock(&wq_pool_mutex);
3944
3945 /*
3946 * If something goes wrong during CPU up/down, we'll fall back to
3947 * the default pwq covering whole @attrs->cpumask. Always create
3948 * it even if we don't use it immediately.
3949 */
3950 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3951 if (!dfl_pwq)
3952 goto enomem_pwq;
3953
3954 for_each_node(node) {
3955 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3956 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3957 if (!pwq_tbl[node])
3958 goto enomem_pwq;
3959 } else {
3960 dfl_pwq->refcnt++;
3961 pwq_tbl[node] = dfl_pwq;
3962 }
3963 }
3964
3965 mutex_unlock(&wq_pool_mutex);
3966
3967 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003968 mutex_lock(&wq->mutex);
3969
Tejun Heof147f292013-04-01 11:23:35 -07003970 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003971
3972 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003973 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003974 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3975
3976 /* @dfl_pwq might not have been used, ensure it's linked */
3977 link_pwq(dfl_pwq);
3978 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003979
3980 mutex_unlock(&wq->mutex);
3981
Tejun Heo4c16bd32013-04-01 11:23:36 -07003982 /* put the old pwqs */
3983 for_each_node(node)
3984 put_pwq_unlocked(pwq_tbl[node]);
3985 put_pwq_unlocked(dfl_pwq);
3986
3987 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003988 ret = 0;
3989 /* fall through */
3990out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003991 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003992 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003993 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003994 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003995
Tejun Heo4c16bd32013-04-01 11:23:36 -07003996enomem_pwq:
3997 free_unbound_pwq(dfl_pwq);
3998 for_each_node(node)
3999 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
4000 free_unbound_pwq(pwq_tbl[node]);
4001 mutex_unlock(&wq_pool_mutex);
4002 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07004003enomem:
Tejun Heo48621252013-04-01 11:23:31 -07004004 ret = -ENOMEM;
4005 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004006}
4007
Tejun Heo4c16bd32013-04-01 11:23:36 -07004008/**
4009 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
4010 * @wq: the target workqueue
4011 * @cpu: the CPU coming up or going down
4012 * @online: whether @cpu is coming up or going down
4013 *
4014 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
4015 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
4016 * @wq accordingly.
4017 *
4018 * If NUMA affinity can't be adjusted due to memory allocation failure, it
4019 * falls back to @wq->dfl_pwq which may not be optimal but is always
4020 * correct.
4021 *
4022 * Note that when the last allowed CPU of a NUMA node goes offline for a
4023 * workqueue with a cpumask spanning multiple nodes, the workers which were
4024 * already executing the work items for the workqueue will lose their CPU
4025 * affinity and may execute on any CPU. This is similar to how per-cpu
4026 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
4027 * affinity, it's the user's responsibility to flush the work item from
4028 * CPU_DOWN_PREPARE.
4029 */
4030static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
4031 bool online)
4032{
4033 int node = cpu_to_node(cpu);
4034 int cpu_off = online ? -1 : cpu;
4035 struct pool_workqueue *old_pwq = NULL, *pwq;
4036 struct workqueue_attrs *target_attrs;
4037 cpumask_t *cpumask;
4038
4039 lockdep_assert_held(&wq_pool_mutex);
4040
4041 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
4042 return;
4043
4044 /*
4045 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
4046 * Let's use a preallocated one. The following buf is protected by
4047 * CPU hotplug exclusion.
4048 */
4049 target_attrs = wq_update_unbound_numa_attrs_buf;
4050 cpumask = target_attrs->cpumask;
4051
4052 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07004053 if (wq->unbound_attrs->no_numa)
4054 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004055
4056 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
4057 pwq = unbound_pwq_by_node(wq, node);
4058
4059 /*
4060 * Let's determine what needs to be done. If the target cpumask is
4061 * different from wq's, we need to compare it to @pwq's and create
4062 * a new one if they don't match. If the target cpumask equals
4063 * wq's, the default pwq should be used. If @pwq is already the
4064 * default one, nothing to do; otherwise, install the default one.
4065 */
4066 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
4067 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4068 goto out_unlock;
4069 } else {
4070 if (pwq == wq->dfl_pwq)
4071 goto out_unlock;
4072 else
4073 goto use_dfl_pwq;
4074 }
4075
4076 mutex_unlock(&wq->mutex);
4077
4078 /* create a new pwq */
4079 pwq = alloc_unbound_pwq(wq, target_attrs);
4080 if (!pwq) {
4081 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4082 wq->name);
4083 goto out_unlock;
4084 }
4085
4086 /*
4087 * Install the new pwq. As this function is called only from CPU
4088 * hotplug callbacks and applying a new attrs is wrapped with
4089 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4090 * inbetween.
4091 */
4092 mutex_lock(&wq->mutex);
4093 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4094 goto out_unlock;
4095
4096use_dfl_pwq:
4097 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4098 get_pwq(wq->dfl_pwq);
4099 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4100 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4101out_unlock:
4102 mutex_unlock(&wq->mutex);
4103 put_pwq_unlocked(old_pwq);
4104}
4105
Tejun Heo30cdf242013-03-12 11:29:57 -07004106static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004108 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07004109 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004110
Tejun Heo30cdf242013-03-12 11:29:57 -07004111 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004112 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4113 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004114 return -ENOMEM;
4115
4116 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004117 struct pool_workqueue *pwq =
4118 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004119 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004120 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004121
Tejun Heof147f292013-04-01 11:23:35 -07004122 init_pwq(pwq, wq, &cpu_pools[highpri]);
4123
4124 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004125 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004126 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004127 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004128 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07004129 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004130 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004131 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004132}
4133
Tejun Heof3421792010-07-02 10:03:51 +02004134static int wq_clamp_max_active(int max_active, unsigned int flags,
4135 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004136{
Tejun Heof3421792010-07-02 10:03:51 +02004137 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4138
4139 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004140 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4141 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004142
Tejun Heof3421792010-07-02 10:03:51 +02004143 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004144}
4145
Tejun Heob196be82012-01-10 15:11:35 -08004146struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004147 unsigned int flags,
4148 int max_active,
4149 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004150 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004151{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004152 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004153 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004154 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004155 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004156
Viresh Kumarcee22a12013-04-08 16:45:40 +05304157 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4158 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4159 flags |= WQ_UNBOUND;
4160
Tejun Heoecf68812013-04-01 11:23:34 -07004161 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004162 if (flags & WQ_UNBOUND)
4163 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4164
4165 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004166 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004167 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004168
Tejun Heo6029a912013-04-01 11:23:34 -07004169 if (flags & WQ_UNBOUND) {
4170 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4171 if (!wq->unbound_attrs)
4172 goto err_free_wq;
4173 }
4174
Tejun Heoecf68812013-04-01 11:23:34 -07004175 va_start(args, lock_name);
4176 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004177 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004178
Tejun Heod320c032010-06-29 10:07:14 +02004179 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004180 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004181
Tejun Heob196be82012-01-10 15:11:35 -08004182 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004183 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004184 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004185 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004186 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004187 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004188 INIT_LIST_HEAD(&wq->flusher_queue);
4189 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004190 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004191
Johannes Bergeb13ba82008-01-16 09:51:58 +01004192 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004193 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004194
Tejun Heo30cdf242013-03-12 11:29:57 -07004195 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004196 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004197
Tejun Heo493008a2013-03-12 11:30:03 -07004198 /*
4199 * Workqueues which may be used during memory reclaim should
4200 * have a rescuer to guarantee forward progress.
4201 */
4202 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004203 struct worker *rescuer;
4204
Tejun Heod2c1d402013-03-12 11:30:04 -07004205 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004206 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004207 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004208
Tejun Heo111c2252013-01-17 17:16:24 -08004209 rescuer->rescue_wq = wq;
4210 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004211 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004212 if (IS_ERR(rescuer->task)) {
4213 kfree(rescuer);
4214 goto err_destroy;
4215 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004216
Tejun Heod2c1d402013-03-12 11:30:04 -07004217 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004218 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004219 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004220 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004221
Tejun Heo226223a2013-03-12 11:30:05 -07004222 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4223 goto err_destroy;
4224
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004225 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004226 * wq_pool_mutex protects global freeze state and workqueues list.
4227 * Grab it, adjust max_active and add the new @wq to workqueues
4228 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004229 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004230 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004231
Lai Jiangshana357fc02013-03-25 16:57:19 -07004232 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004233 for_each_pwq(pwq, wq)
4234 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004235 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004236
Tejun Heo15376632010-06-29 10:07:11 +02004237 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004238
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004239 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004240
Oleg Nesterov3af244332007-05-09 02:34:09 -07004241 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004242
4243err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004244 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004245 kfree(wq);
4246 return NULL;
4247err_destroy:
4248 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004249 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004250}
Tejun Heod320c032010-06-29 10:07:14 +02004251EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004252
4253/**
4254 * destroy_workqueue - safely terminate a workqueue
4255 * @wq: target workqueue
4256 *
4257 * Safely destroy a workqueue. All work currently pending will be done first.
4258 */
4259void destroy_workqueue(struct workqueue_struct *wq)
4260{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004261 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004262 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004263
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004264 /* drain it before proceeding with destruction */
4265 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004266
Tejun Heo6183c002013-03-12 11:29:57 -07004267 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004268 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004269 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004270 int i;
4271
Tejun Heo76af4d92013-03-12 11:30:00 -07004272 for (i = 0; i < WORK_NR_COLORS; i++) {
4273 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004274 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004275 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004276 }
4277 }
4278
Lai Jiangshan5c529592013-04-04 10:05:38 +08004279 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004280 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004281 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004282 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004283 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004284 }
Tejun Heo6183c002013-03-12 11:29:57 -07004285 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004286 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004287
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004288 /*
4289 * wq list is used to freeze wq, remove from list after
4290 * flushing is complete in case freeze races us.
4291 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004292 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004293 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004294 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004295
Tejun Heo226223a2013-03-12 11:30:05 -07004296 workqueue_sysfs_unregister(wq);
4297
Tejun Heo493008a2013-03-12 11:30:03 -07004298 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004299 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004300 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004301 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004302 }
4303
Tejun Heo8864b4e2013-03-12 11:30:04 -07004304 if (!(wq->flags & WQ_UNBOUND)) {
4305 /*
4306 * The base ref is never dropped on per-cpu pwqs. Directly
4307 * free the pwqs and wq.
4308 */
4309 free_percpu(wq->cpu_pwqs);
4310 kfree(wq);
4311 } else {
4312 /*
4313 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004314 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4315 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004316 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004317 for_each_node(node) {
4318 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4319 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4320 put_pwq_unlocked(pwq);
4321 }
4322
4323 /*
4324 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4325 * put. Don't access it afterwards.
4326 */
4327 pwq = wq->dfl_pwq;
4328 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004329 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004330 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004331}
4332EXPORT_SYMBOL_GPL(destroy_workqueue);
4333
Tejun Heodcd989c2010-06-29 10:07:14 +02004334/**
4335 * workqueue_set_max_active - adjust max_active of a workqueue
4336 * @wq: target workqueue
4337 * @max_active: new max_active value.
4338 *
4339 * Set max_active of @wq to @max_active.
4340 *
4341 * CONTEXT:
4342 * Don't call from IRQ context.
4343 */
4344void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4345{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004346 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004347
Tejun Heo8719dce2013-03-12 11:30:04 -07004348 /* disallow meddling with max_active for ordered workqueues */
4349 if (WARN_ON(wq->flags & __WQ_ORDERED))
4350 return;
4351
Tejun Heof3421792010-07-02 10:03:51 +02004352 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004353
Lai Jiangshana357fc02013-03-25 16:57:19 -07004354 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004355
4356 wq->saved_max_active = max_active;
4357
Tejun Heo699ce092013-03-13 16:51:35 -07004358 for_each_pwq(pwq, wq)
4359 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004360
Lai Jiangshana357fc02013-03-25 16:57:19 -07004361 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004362}
4363EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4364
4365/**
Tejun Heoe62676162013-03-12 17:41:37 -07004366 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4367 *
4368 * Determine whether %current is a workqueue rescuer. Can be used from
4369 * work functions to determine whether it's being run off the rescuer task.
Yacine Belkadid185af32013-07-31 14:59:24 -07004370 *
4371 * Return: %true if %current is a workqueue rescuer. %false otherwise.
Tejun Heoe62676162013-03-12 17:41:37 -07004372 */
4373bool current_is_workqueue_rescuer(void)
4374{
4375 struct worker *worker = current_wq_worker();
4376
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004377 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07004378}
4379
4380/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004381 * workqueue_congested - test whether a workqueue is congested
4382 * @cpu: CPU in question
4383 * @wq: target workqueue
4384 *
4385 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4386 * no synchronization around this function and the test result is
4387 * unreliable and only useful as advisory hints or for debugging.
4388 *
Tejun Heod3251852013-05-10 11:10:17 -07004389 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4390 * Note that both per-cpu and unbound workqueues may be associated with
4391 * multiple pool_workqueues which have separate congested states. A
4392 * workqueue being congested on one CPU doesn't mean the workqueue is also
4393 * contested on other CPUs / NUMA nodes.
4394 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004395 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004396 * %true if congested, %false otherwise.
4397 */
Tejun Heod84ff052013-03-12 11:29:59 -07004398bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004399{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004400 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004401 bool ret;
4402
Lai Jiangshan88109452013-03-20 03:28:10 +08004403 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004404
Tejun Heod3251852013-05-10 11:10:17 -07004405 if (cpu == WORK_CPU_UNBOUND)
4406 cpu = smp_processor_id();
4407
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004408 if (!(wq->flags & WQ_UNBOUND))
4409 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4410 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004411 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004412
Tejun Heo76af4d92013-03-12 11:30:00 -07004413 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004414 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004415
4416 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004417}
4418EXPORT_SYMBOL_GPL(workqueue_congested);
4419
4420/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004421 * work_busy - test whether a work is currently pending or running
4422 * @work: the work to be tested
4423 *
4424 * Test whether @work is currently pending or running. There is no
4425 * synchronization around this function and the test result is
4426 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004427 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004428 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004429 * OR'd bitmask of WORK_BUSY_* bits.
4430 */
4431unsigned int work_busy(struct work_struct *work)
4432{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004433 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004434 unsigned long flags;
4435 unsigned int ret = 0;
4436
Tejun Heodcd989c2010-06-29 10:07:14 +02004437 if (work_pending(work))
4438 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004439
Tejun Heofa1b54e2013-03-12 11:30:00 -07004440 local_irq_save(flags);
4441 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004442 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004443 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004444 if (find_worker_executing_work(pool, work))
4445 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004446 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004447 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004448 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004449
4450 return ret;
4451}
4452EXPORT_SYMBOL_GPL(work_busy);
4453
Tejun Heo3d1cb202013-04-30 15:27:22 -07004454/**
4455 * set_worker_desc - set description for the current work item
4456 * @fmt: printf-style format string
4457 * @...: arguments for the format string
4458 *
4459 * This function can be called by a running work function to describe what
4460 * the work item is about. If the worker task gets dumped, this
4461 * information will be printed out together to help debugging. The
4462 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4463 */
4464void set_worker_desc(const char *fmt, ...)
4465{
4466 struct worker *worker = current_wq_worker();
4467 va_list args;
4468
4469 if (worker) {
4470 va_start(args, fmt);
4471 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4472 va_end(args);
4473 worker->desc_valid = true;
4474 }
4475}
4476
4477/**
4478 * print_worker_info - print out worker information and description
4479 * @log_lvl: the log level to use when printing
4480 * @task: target task
4481 *
4482 * If @task is a worker and currently executing a work item, print out the
4483 * name of the workqueue being serviced and worker description set with
4484 * set_worker_desc() by the currently executing work item.
4485 *
4486 * This function can be safely called on any task as long as the
4487 * task_struct itself is accessible. While safe, this function isn't
4488 * synchronized and may print out mixups or garbages of limited length.
4489 */
4490void print_worker_info(const char *log_lvl, struct task_struct *task)
4491{
4492 work_func_t *fn = NULL;
4493 char name[WQ_NAME_LEN] = { };
4494 char desc[WORKER_DESC_LEN] = { };
4495 struct pool_workqueue *pwq = NULL;
4496 struct workqueue_struct *wq = NULL;
4497 bool desc_valid = false;
4498 struct worker *worker;
4499
4500 if (!(task->flags & PF_WQ_WORKER))
4501 return;
4502
4503 /*
4504 * This function is called without any synchronization and @task
4505 * could be in any state. Be careful with dereferences.
4506 */
4507 worker = probe_kthread_data(task);
4508
4509 /*
4510 * Carefully copy the associated workqueue's workfn and name. Keep
4511 * the original last '\0' in case the original contains garbage.
4512 */
4513 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4514 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4515 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4516 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4517
4518 /* copy worker description */
4519 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4520 if (desc_valid)
4521 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4522
4523 if (fn || name[0] || desc[0]) {
4524 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4525 if (desc[0])
4526 pr_cont(" (%s)", desc);
4527 pr_cont("\n");
4528 }
4529}
4530
Tejun Heodb7bccf2010-06-29 10:07:12 +02004531/*
4532 * CPU hotplug.
4533 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004534 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004535 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004536 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004537 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004538 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004539 * blocked draining impractical.
4540 *
Tejun Heo24647572013-01-24 11:01:33 -08004541 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004542 * running as an unbound one and allowing it to be reattached later if the
4543 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004544 */
4545
Tejun Heo706026c2013-01-24 11:01:34 -08004546static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004547{
Tejun Heo38db41d2013-01-24 11:01:34 -08004548 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004549 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004550 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004551 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004552
Tejun Heof02ae732013-03-12 11:30:03 -07004553 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004554 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004555
Tejun Heobc3a1af2013-03-13 19:47:39 -07004556 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004557 spin_lock_irq(&pool->lock);
4558
4559 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004560 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004561 * unbound and set DISASSOCIATED. Before this, all workers
4562 * except for the ones which are still executing works from
4563 * before the last CPU down must be on the cpu. After
4564 * this, they may become diasporas.
4565 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004566 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004567 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004568
Tejun Heo24647572013-01-24 11:01:33 -08004569 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004570
Tejun Heo94cf58b2013-01-24 11:01:33 -08004571 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004572 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004573
Lai Jiangshaneb283422013-03-08 15:18:28 -08004574 /*
4575 * Call schedule() so that we cross rq->lock and thus can
4576 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4577 * This is necessary as scheduler callbacks may be invoked
4578 * from other cpus.
4579 */
4580 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004581
Lai Jiangshaneb283422013-03-08 15:18:28 -08004582 /*
4583 * Sched callbacks are disabled now. Zap nr_running.
4584 * After this, nr_running stays zero and need_more_worker()
4585 * and keep_working() are always true as long as the
4586 * worklist is not empty. This pool now behaves as an
4587 * unbound (in terms of concurrency management) pool which
4588 * are served by workers tied to the pool.
4589 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004590 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004591
4592 /*
4593 * With concurrency management just turned off, a busy
4594 * worker blocking could lead to lengthy stalls. Kick off
4595 * unbound chain execution of currently pending work items.
4596 */
4597 spin_lock_irq(&pool->lock);
4598 wake_up_worker(pool);
4599 spin_unlock_irq(&pool->lock);
4600 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004601}
4602
Tejun Heobd7c0892013-03-19 13:45:21 -07004603/**
4604 * rebind_workers - rebind all workers of a pool to the associated CPU
4605 * @pool: pool of interest
4606 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004607 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004608 */
4609static void rebind_workers(struct worker_pool *pool)
4610{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004611 struct worker *worker;
4612 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004613
4614 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004615
Tejun Heoa9ab7752013-03-19 13:45:21 -07004616 /*
4617 * Restore CPU affinity of all workers. As all idle workers should
4618 * be on the run-queue of the associated CPU before any local
4619 * wake-ups for concurrency management happen, restore CPU affinty
4620 * of all workers first and then clear UNBOUND. As we're called
4621 * from CPU_ONLINE, the following shouldn't fail.
4622 */
4623 for_each_pool_worker(worker, wi, pool)
4624 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4625 pool->attrs->cpumask) < 0);
4626
4627 spin_lock_irq(&pool->lock);
4628
4629 for_each_pool_worker(worker, wi, pool) {
4630 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004631
4632 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004633 * A bound idle worker should actually be on the runqueue
4634 * of the associated CPU for local wake-ups targeting it to
4635 * work. Kick all idle workers so that they migrate to the
4636 * associated CPU. Doing this in the same loop as
4637 * replacing UNBOUND with REBOUND is safe as no worker will
4638 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004639 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004640 if (worker_flags & WORKER_IDLE)
4641 wake_up_process(worker->task);
4642
4643 /*
4644 * We want to clear UNBOUND but can't directly call
4645 * worker_clr_flags() or adjust nr_running. Atomically
4646 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4647 * @worker will clear REBOUND using worker_clr_flags() when
4648 * it initiates the next execution cycle thus restoring
4649 * concurrency management. Note that when or whether
4650 * @worker clears REBOUND doesn't affect correctness.
4651 *
4652 * ACCESS_ONCE() is necessary because @worker->flags may be
4653 * tested without holding any lock in
4654 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4655 * fail incorrectly leading to premature concurrency
4656 * management operations.
4657 */
4658 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4659 worker_flags |= WORKER_REBOUND;
4660 worker_flags &= ~WORKER_UNBOUND;
4661 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004662 }
4663
Tejun Heoa9ab7752013-03-19 13:45:21 -07004664 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004665}
4666
Tejun Heo7dbc7252013-03-19 13:45:21 -07004667/**
4668 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4669 * @pool: unbound pool of interest
4670 * @cpu: the CPU which is coming up
4671 *
4672 * An unbound pool may end up with a cpumask which doesn't have any online
4673 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4674 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4675 * online CPU before, cpus_allowed of all its workers should be restored.
4676 */
4677static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4678{
4679 static cpumask_t cpumask;
4680 struct worker *worker;
4681 int wi;
4682
4683 lockdep_assert_held(&pool->manager_mutex);
4684
4685 /* is @cpu allowed for @pool? */
4686 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4687 return;
4688
4689 /* is @cpu the only online CPU? */
4690 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4691 if (cpumask_weight(&cpumask) != 1)
4692 return;
4693
4694 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4695 for_each_pool_worker(worker, wi, pool)
4696 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4697 pool->attrs->cpumask) < 0);
4698}
4699
Tejun Heo8db25e72012-07-17 12:39:28 -07004700/*
4701 * Workqueues should be brought up before normal priority CPU notifiers.
4702 * This will be registered high priority CPU notifier.
4703 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004704static int workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004705 unsigned long action,
4706 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004707{
Tejun Heod84ff052013-03-12 11:29:59 -07004708 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004709 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004710 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004711 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Tejun Heo8db25e72012-07-17 12:39:28 -07004713 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004715 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004716 if (pool->nr_workers)
4717 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004718 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004719 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004721 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004722
Tejun Heo65758202012-07-17 12:39:26 -07004723 case CPU_DOWN_FAILED:
4724 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004725 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004726
4727 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004728 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004729
Tejun Heo7dbc7252013-03-19 13:45:21 -07004730 if (pool->cpu == cpu) {
4731 spin_lock_irq(&pool->lock);
4732 pool->flags &= ~POOL_DISASSOCIATED;
4733 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004734
Tejun Heo7dbc7252013-03-19 13:45:21 -07004735 rebind_workers(pool);
4736 } else if (pool->cpu < 0) {
4737 restore_unbound_workers_cpumask(pool, cpu);
4738 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004739
Tejun Heobc3a1af2013-03-13 19:47:39 -07004740 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004741 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004742
Tejun Heo4c16bd32013-04-01 11:23:36 -07004743 /* update NUMA affinity of unbound workqueues */
4744 list_for_each_entry(wq, &workqueues, list)
4745 wq_update_unbound_numa(wq, cpu, true);
4746
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004747 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004748 break;
Tejun Heo65758202012-07-17 12:39:26 -07004749 }
4750 return NOTIFY_OK;
4751}
4752
4753/*
4754 * Workqueues should be brought down after normal priority CPU notifiers.
4755 * This will be registered as low priority CPU notifier.
4756 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004757static int workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004758 unsigned long action,
4759 void *hcpu)
4760{
Tejun Heod84ff052013-03-12 11:29:59 -07004761 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004762 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004763 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004764
Tejun Heo65758202012-07-17 12:39:26 -07004765 switch (action & ~CPU_TASKS_FROZEN) {
4766 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004767 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004768 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004769 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004770
4771 /* update NUMA affinity of unbound workqueues */
4772 mutex_lock(&wq_pool_mutex);
4773 list_for_each_entry(wq, &workqueues, list)
4774 wq_update_unbound_numa(wq, cpu, false);
4775 mutex_unlock(&wq_pool_mutex);
4776
4777 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004778 flush_work(&unbind_work);
4779 break;
Tejun Heo65758202012-07-17 12:39:26 -07004780 }
4781 return NOTIFY_OK;
4782}
4783
Rusty Russell2d3854a2008-11-05 13:39:10 +11004784#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004785
Rusty Russell2d3854a2008-11-05 13:39:10 +11004786struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004787 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004788 long (*fn)(void *);
4789 void *arg;
4790 long ret;
4791};
4792
Tejun Heoed48ece2012-09-18 12:48:43 -07004793static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004794{
Tejun Heoed48ece2012-09-18 12:48:43 -07004795 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4796
Rusty Russell2d3854a2008-11-05 13:39:10 +11004797 wfc->ret = wfc->fn(wfc->arg);
4798}
4799
4800/**
4801 * work_on_cpu - run a function in user context on a particular cpu
4802 * @cpu: the cpu to run on
4803 * @fn: the function to run
4804 * @arg: the function arg
4805 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004806 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06004807 * The caller must not hold any locks which would prevent @fn from completing.
Yacine Belkadid185af32013-07-31 14:59:24 -07004808 *
4809 * Return: The value @fn returns.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004810 */
Tejun Heod84ff052013-03-12 11:29:59 -07004811long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004812{
Tejun Heoed48ece2012-09-18 12:48:43 -07004813 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004814
Tejun Heoed48ece2012-09-18 12:48:43 -07004815 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4816 schedule_work_on(cpu, &wfc.work);
Lai Jiangshanc2fda502013-07-24 18:31:42 +08004817
4818 /*
4819 * The work item is on-stack and can't lead to deadlock through
4820 * flushing. Use __flush_work() to avoid spurious lockdep warnings
4821 * when work_on_cpu()s are nested.
4822 */
4823 __flush_work(&wfc.work);
4824
Rusty Russell2d3854a2008-11-05 13:39:10 +11004825 return wfc.ret;
4826}
4827EXPORT_SYMBOL_GPL(work_on_cpu);
4828#endif /* CONFIG_SMP */
4829
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004830#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304831
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004832/**
4833 * freeze_workqueues_begin - begin freezing workqueues
4834 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004835 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004836 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004837 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004838 *
4839 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004840 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004841 */
4842void freeze_workqueues_begin(void)
4843{
Tejun Heo17116962013-03-12 11:29:58 -07004844 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004845 struct workqueue_struct *wq;
4846 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004847 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004848
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004849 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004850
Tejun Heo6183c002013-03-12 11:29:57 -07004851 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004852 workqueue_freezing = true;
4853
Tejun Heo24b8a842013-03-12 11:29:58 -07004854 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004855 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004856 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004857 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4858 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004859 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004860 }
4861
Tejun Heo24b8a842013-03-12 11:29:58 -07004862 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004863 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004864 for_each_pwq(pwq, wq)
4865 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004866 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004867 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004868
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004869 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004871
4872/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004873 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004874 *
4875 * Check whether freezing is complete. This function must be called
4876 * between freeze_workqueues_begin() and thaw_workqueues().
4877 *
4878 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004879 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004880 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004881 * Return:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004882 * %true if some freezable workqueues are still busy. %false if freezing
4883 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004884 */
4885bool freeze_workqueues_busy(void)
4886{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004887 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004888 struct workqueue_struct *wq;
4889 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004890
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004891 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004892
Tejun Heo6183c002013-03-12 11:29:57 -07004893 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004894
Tejun Heo24b8a842013-03-12 11:29:58 -07004895 list_for_each_entry(wq, &workqueues, list) {
4896 if (!(wq->flags & WQ_FREEZABLE))
4897 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004898 /*
4899 * nr_active is monotonically decreasing. It's safe
4900 * to peek without lock.
4901 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004902 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004903 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004904 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004905 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004906 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004907 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004908 goto out_unlock;
4909 }
4910 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004911 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004912 }
4913out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004914 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004915 return busy;
4916}
4917
4918/**
4919 * thaw_workqueues - thaw workqueues
4920 *
4921 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004922 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004923 *
4924 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004925 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004926 */
4927void thaw_workqueues(void)
4928{
Tejun Heo24b8a842013-03-12 11:29:58 -07004929 struct workqueue_struct *wq;
4930 struct pool_workqueue *pwq;
4931 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004932 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004933
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004934 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004935
4936 if (!workqueue_freezing)
4937 goto out_unlock;
4938
Tejun Heo24b8a842013-03-12 11:29:58 -07004939 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004940 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004941 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004942 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4943 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004944 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004945 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004946
Tejun Heo24b8a842013-03-12 11:29:58 -07004947 /* restore max_active and repopulate worklist */
4948 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004949 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004950 for_each_pwq(pwq, wq)
4951 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004952 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004953 }
4954
4955 workqueue_freezing = false;
4956out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004957 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004958}
4959#endif /* CONFIG_FREEZER */
4960
Tejun Heobce90382013-04-01 11:23:32 -07004961static void __init wq_numa_init(void)
4962{
4963 cpumask_var_t *tbl;
4964 int node, cpu;
4965
4966 /* determine NUMA pwq table len - highest node id + 1 */
4967 for_each_node(node)
4968 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4969
4970 if (num_possible_nodes() <= 1)
4971 return;
4972
Tejun Heod55262c2013-04-01 11:23:38 -07004973 if (wq_disable_numa) {
4974 pr_info("workqueue: NUMA affinity support disabled\n");
4975 return;
4976 }
4977
Tejun Heo4c16bd32013-04-01 11:23:36 -07004978 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4979 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4980
Tejun Heobce90382013-04-01 11:23:32 -07004981 /*
4982 * We want masks of possible CPUs of each node which isn't readily
4983 * available. Build one from cpu_to_node() which should have been
4984 * fully initialized by now.
4985 */
4986 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4987 BUG_ON(!tbl);
4988
4989 for_each_node(node)
Tejun Heo1be0c252013-05-15 14:24:24 -07004990 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
4991 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07004992
4993 for_each_possible_cpu(cpu) {
4994 node = cpu_to_node(cpu);
4995 if (WARN_ON(node == NUMA_NO_NODE)) {
4996 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4997 /* happens iff arch is bonkers, let's just proceed */
4998 return;
4999 }
5000 cpumask_set_cpu(cpu, tbl[node]);
5001 }
5002
5003 wq_numa_possible_cpumask = tbl;
5004 wq_numa_enabled = true;
5005}
5006
Suresh Siddha6ee05782010-07-30 14:57:37 -07005007static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008{
Tejun Heo7a4e3442013-03-12 11:30:00 -07005009 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
5010 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02005011
Tejun Heo7c3eed52013-01-24 11:01:33 -08005012 /* make sure we have enough bits for OFFQ pool ID */
5013 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08005014 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07005015
Tejun Heoe904e6c2013-03-12 11:29:57 -07005016 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
5017
5018 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
5019
Tejun Heo65758202012-07-17 12:39:26 -07005020 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07005021 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02005022
Tejun Heobce90382013-04-01 11:23:32 -07005023 wq_numa_init();
5024
Tejun Heo706026c2013-01-24 11:01:34 -08005025 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07005026 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005027 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02005028
Tejun Heo7a4e3442013-03-12 11:30:00 -07005029 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07005030 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07005031 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08005032 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07005033 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07005034 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07005035 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07005036
Tejun Heo9daf9e62013-01-24 11:01:33 -08005037 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005038 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08005039 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005040 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07005041 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02005042 }
5043
Tejun Heoe22bee72010-06-29 10:07:14 +02005044 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07005045 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005046 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02005047
Tejun Heof02ae732013-03-12 11:30:03 -07005048 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07005049 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07005050 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07005051 }
Tejun Heoe22bee72010-06-29 10:07:14 +02005052 }
5053
Tejun Heo29c91e92013-03-12 11:30:03 -07005054 /* create default unbound wq attrs */
5055 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
5056 struct workqueue_attrs *attrs;
5057
5058 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07005059 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07005060 unbound_std_wq_attrs[i] = attrs;
5061 }
5062
Tejun Heod320c032010-06-29 10:07:14 +02005063 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005064 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005065 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005066 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5067 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005068 system_freezable_wq = alloc_workqueue("events_freezable",
5069 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305070 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5071 WQ_POWER_EFFICIENT, 0);
5072 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5073 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5074 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005075 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305076 !system_unbound_wq || !system_freezable_wq ||
5077 !system_power_efficient_wq ||
5078 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005081early_initcall(init_workqueues);