blob: 59bc24918655ba36b7df23390e91b8f1bd7a6287 [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
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +080068 * attach_mutex to avoid changing binding state while
Lai Jiangshan4736cbf2014-05-20 17:46:35 +080069 * worker_attach_to_pool() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070070 */
Tejun Heo24647572013-01-24 11:01:33 -080071 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020072
Tejun Heoc8e55f32010-06-29 10:07:12 +020073 /* worker flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074 WORKER_DIE = 1 << 1, /* die die die */
75 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020076 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020077 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020078 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070079 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020080
Tejun Heoa9ab7752013-03-19 13:45:21 -070081 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
82 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020083
Tejun Heoe34cdddb2013-01-24 11:01:33 -080084 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070085
Tejun Heo29c91e92013-03-12 11:30:03 -070086 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020087 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020088
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
90 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
91
Tejun Heo3233cdb2011-02-16 18:10:19 +010092 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
93 /* call for help after 10ms
94 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
96 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020097
98 /*
99 * Rescue workers are used only on emergencies and shared by
Dongsheng Yang8698a742014-03-11 18:09:12 +0800100 * all cpus. Give MIN_NICE.
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 */
Dongsheng Yang8698a742014-03-11 18:09:12 +0800102 RESCUER_NICE_LEVEL = MIN_NICE,
103 HIGHPRI_NICE_LEVEL = MIN_NICE,
Tejun Heoecf68812013-04-01 11:23:34 -0700104
105 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 * Structure fields follow one of the following exclusion rules.
110 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200111 * I: Modifiable by initialization/destruction paths and read-only for
112 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200113 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200114 * P: Preemption protected. Disabling preemption is enough and should
115 * only be modified and accessed from the local cpu.
116 *
Tejun Heod565ed62013-01-24 11:01:33 -0800117 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heod565ed62013-01-24 11:01:33 -0800119 * X: During normal operation, modification requires pool->lock and should
120 * be done only from local cpu. Either disabling preemption on local
121 * cpu or grabbing pool->lock is enough for read access. If
122 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200123 *
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800124 * A: pool->attach_mutex protected.
Tejun Heo822d8402013-03-19 13:45:21 -0700125 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700126 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700127 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700128 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700129 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700130 * WQ: wq->mutex protected.
131 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700132 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700133 *
134 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200135 */
136
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800137/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800140 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700141 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700142 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800143 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700144 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700145
146 struct list_head worklist; /* L: list of pending works */
147 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700148
149 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700150 int nr_idle; /* L: currently idle ones */
151
152 struct list_head idle_list; /* X: list of idle workers */
153 struct timer_list idle_timer; /* L: worker idle timeout */
154 struct timer_list mayday_timer; /* L: SOS timer for workers */
155
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700156 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800157 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
158 /* L: hash of busy workers */
159
Tejun Heobc3a1af2013-03-13 19:47:39 -0700160 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700161 struct mutex manager_arb; /* manager arbitration */
Tejun Heo2607d7a2015-03-09 09:22:28 -0400162 struct worker *manager; /* L: purely informational */
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800163 struct mutex attach_mutex; /* attach/detach exclusion */
164 struct list_head workers; /* A: attached workers */
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +0800165 struct completion *detach_completion; /* all workers detached */
Tejun Heoe19e3972013-01-24 11:39:44 -0800166
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +0800167 struct ida worker_ida; /* worker IDs for task name */
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 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400234 struct list_head list; /* PR: 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
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400261 /*
262 * Destruction of workqueue_struct is sched-RCU protected to allow
263 * walking the workqueues list without grabbing wq_pool_mutex.
264 * This is used to dump all workqueues from sysrq.
265 */
266 struct rcu_head rcu;
267
Tejun Heo2728fd22013-04-01 11:23:35 -0700268 /* hot fields used during command issue, aligned to cacheline */
269 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
270 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700271 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
Tejun Heoe904e6c2013-03-12 11:29:57 -0700274static struct kmem_cache *pwq_cache;
275
Tejun Heobce90382013-04-01 11:23:32 -0700276static cpumask_var_t *wq_numa_possible_cpumask;
277 /* possible CPUs of each node */
278
Tejun Heod55262c2013-04-01 11:23:38 -0700279static bool wq_disable_numa;
280module_param_named(disable_numa, wq_disable_numa, bool, 0444);
281
Viresh Kumarcee22a12013-04-08 16:45:40 +0530282/* see the comment above the definition of WQ_POWER_EFFICIENT */
Luis R. Rodriguez552f5302015-05-27 11:09:39 +0930283static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT);
Viresh Kumarcee22a12013-04-08 16:45:40 +0530284module_param_named(power_efficient, wq_power_efficient, bool, 0444);
285
Tejun Heobce90382013-04-01 11:23:32 -0700286static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
287
Tejun Heo4c16bd32013-04-01 11:23:36 -0700288/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
289static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
290
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700291static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700292static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700293
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400294static LIST_HEAD(workqueues); /* PR: list of all workqueues */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700295static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700296
297/* the per-cpu worker pools */
298static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
299 cpu_worker_pools);
300
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700301static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700302
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700303/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700304static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
305
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700306/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700307static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
308
Tejun Heo8a2b7532013-09-05 12:30:04 -0400309/* I: attributes used when instantiating ordered pools on demand */
310static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS];
311
Tejun Heod320c032010-06-29 10:07:14 +0200312struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400313EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300314struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900315EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300316struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200317EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300318struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200319EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300320struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100321EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530322struct workqueue_struct *system_power_efficient_wq __read_mostly;
323EXPORT_SYMBOL_GPL(system_power_efficient_wq);
324struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
325EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200326
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700327static int worker_thread(void *__worker);
328static void copy_workqueue_attrs(struct workqueue_attrs *to,
329 const struct workqueue_attrs *from);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +0800330static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700331
Tejun Heo97bd2342010-10-05 10:41:14 +0200332#define CREATE_TRACE_POINTS
333#include <trace/events/workqueue.h>
334
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700335#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700336 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700337 lockdep_is_held(&wq_pool_mutex), \
338 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700339
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700340#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700341 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700342 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700343 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700344
Tejun Heof02ae732013-03-12 11:30:03 -0700345#define for_each_cpu_worker_pool(pool, cpu) \
346 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
347 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700348 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700349
Tejun Heo49e3cf42013-03-12 11:29:58 -0700350/**
Tejun Heo17116962013-03-12 11:29:58 -0700351 * for_each_pool - iterate through all worker_pools in the system
352 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700353 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700354 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700355 * This must be called either with wq_pool_mutex held or sched RCU read
356 * locked. If the pool needs to be used beyond the locking in effect, the
357 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700358 *
359 * The if/else clause exists only for the lockdep assertion and can be
360 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700361 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700362#define for_each_pool(pool, pi) \
363 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700364 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700365 else
Tejun Heo17116962013-03-12 11:29:58 -0700366
367/**
Tejun Heo822d8402013-03-19 13:45:21 -0700368 * for_each_pool_worker - iterate through all workers of a worker_pool
369 * @worker: iteration cursor
Tejun Heo822d8402013-03-19 13:45:21 -0700370 * @pool: worker_pool to iterate workers of
371 *
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800372 * This must be called with @pool->attach_mutex.
Tejun Heo822d8402013-03-19 13:45:21 -0700373 *
374 * The if/else clause exists only for the lockdep assertion and can be
375 * ignored.
376 */
Lai Jiangshanda028462014-05-20 17:46:31 +0800377#define for_each_pool_worker(worker, pool) \
378 list_for_each_entry((worker), &(pool)->workers, node) \
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800379 if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \
Tejun Heo822d8402013-03-19 13:45:21 -0700380 else
381
382/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700383 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
384 * @pwq: iteration cursor
385 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700386 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700387 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700388 * If the pwq needs to be used beyond the locking in effect, the caller is
389 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700390 *
391 * The if/else clause exists only for the lockdep assertion and can be
392 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700393 */
394#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700395 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700396 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700397 else
Tejun Heof3421792010-07-02 10:03:51 +0200398
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900399#ifdef CONFIG_DEBUG_OBJECTS_WORK
400
401static struct debug_obj_descr work_debug_descr;
402
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100403static void *work_debug_hint(void *addr)
404{
405 return ((struct work_struct *) addr)->func;
406}
407
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900408/*
409 * fixup_init is called when:
410 * - an active object is initialized
411 */
412static int work_fixup_init(void *addr, enum debug_obj_state state)
413{
414 struct work_struct *work = addr;
415
416 switch (state) {
417 case ODEBUG_STATE_ACTIVE:
418 cancel_work_sync(work);
419 debug_object_init(work, &work_debug_descr);
420 return 1;
421 default:
422 return 0;
423 }
424}
425
426/*
427 * fixup_activate is called when:
428 * - an active object is activated
429 * - an unknown object is activated (might be a statically initialized object)
430 */
431static int work_fixup_activate(void *addr, enum debug_obj_state state)
432{
433 struct work_struct *work = addr;
434
435 switch (state) {
436
437 case ODEBUG_STATE_NOTAVAILABLE:
438 /*
439 * This is not really a fixup. The work struct was
440 * statically initialized. We just make sure that it
441 * is tracked in the object tracker.
442 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200443 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900444 debug_object_init(work, &work_debug_descr);
445 debug_object_activate(work, &work_debug_descr);
446 return 0;
447 }
448 WARN_ON_ONCE(1);
449 return 0;
450
451 case ODEBUG_STATE_ACTIVE:
452 WARN_ON(1);
453
454 default:
455 return 0;
456 }
457}
458
459/*
460 * fixup_free is called when:
461 * - an active object is freed
462 */
463static int work_fixup_free(void *addr, enum debug_obj_state state)
464{
465 struct work_struct *work = addr;
466
467 switch (state) {
468 case ODEBUG_STATE_ACTIVE:
469 cancel_work_sync(work);
470 debug_object_free(work, &work_debug_descr);
471 return 1;
472 default:
473 return 0;
474 }
475}
476
477static struct debug_obj_descr work_debug_descr = {
478 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100479 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900480 .fixup_init = work_fixup_init,
481 .fixup_activate = work_fixup_activate,
482 .fixup_free = work_fixup_free,
483};
484
485static inline void debug_work_activate(struct work_struct *work)
486{
487 debug_object_activate(work, &work_debug_descr);
488}
489
490static inline void debug_work_deactivate(struct work_struct *work)
491{
492 debug_object_deactivate(work, &work_debug_descr);
493}
494
495void __init_work(struct work_struct *work, int onstack)
496{
497 if (onstack)
498 debug_object_init_on_stack(work, &work_debug_descr);
499 else
500 debug_object_init(work, &work_debug_descr);
501}
502EXPORT_SYMBOL_GPL(__init_work);
503
504void destroy_work_on_stack(struct work_struct *work)
505{
506 debug_object_free(work, &work_debug_descr);
507}
508EXPORT_SYMBOL_GPL(destroy_work_on_stack);
509
Thomas Gleixnerea2e64f2014-03-23 14:20:44 +0000510void destroy_delayed_work_on_stack(struct delayed_work *work)
511{
512 destroy_timer_on_stack(&work->timer);
513 debug_object_free(&work->work, &work_debug_descr);
514}
515EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack);
516
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900517#else
518static inline void debug_work_activate(struct work_struct *work) { }
519static inline void debug_work_deactivate(struct work_struct *work) { }
520#endif
521
Li Bin4e8b22b2013-09-10 09:52:35 +0800522/**
523 * worker_pool_assign_id - allocate ID and assing it to @pool
524 * @pool: the pool pointer of interest
525 *
526 * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned
527 * successfully, -errno on failure.
528 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800529static int worker_pool_assign_id(struct worker_pool *pool)
530{
531 int ret;
532
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700533 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700534
Li Bin4e8b22b2013-09-10 09:52:35 +0800535 ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,
536 GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700537 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700538 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700539 return 0;
540 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800541 return ret;
542}
543
Tejun Heo76af4d92013-03-12 11:30:00 -0700544/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700545 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
546 * @wq: the target workqueue
547 * @node: the node ID
548 *
549 * This must be called either with pwq_lock held or sched RCU read locked.
550 * If the pwq needs to be used beyond the locking in effect, the caller is
551 * responsible for guaranteeing that the pwq stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700552 *
553 * Return: The unbound pool_workqueue for @node.
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700554 */
555static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
556 int node)
557{
558 assert_rcu_or_wq_mutex(wq);
559 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
560}
561
Tejun Heo73f53c42010-06-29 10:07:11 +0200562static unsigned int work_color_to_flags(int color)
563{
564 return color << WORK_STRUCT_COLOR_SHIFT;
565}
566
567static int get_work_color(struct work_struct *work)
568{
569 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
570 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
571}
572
573static int work_next_color(int color)
574{
575 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700576}
577
David Howells4594bf12006-12-07 11:33:26 +0000578/*
Tejun Heo112202d2013-02-13 19:29:12 -0800579 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
580 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800581 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582 *
Tejun Heo112202d2013-02-13 19:29:12 -0800583 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
584 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700585 * work->data. These functions should only be called while the work is
586 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587 *
Tejun Heo112202d2013-02-13 19:29:12 -0800588 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800589 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800590 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800591 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700592 *
593 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
594 * canceled. While being canceled, a work item may have its PENDING set
595 * but stay off timer and worklist for arbitrarily long and nobody should
596 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000597 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598static inline void set_work_data(struct work_struct *work, unsigned long data,
599 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000600{
Tejun Heo6183c002013-03-12 11:29:57 -0700601 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000603}
David Howells365970a2006-11-22 14:54:49 +0000604
Tejun Heo112202d2013-02-13 19:29:12 -0800605static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200606 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200607{
Tejun Heo112202d2013-02-13 19:29:12 -0800608 set_work_data(work, (unsigned long)pwq,
609 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200610}
611
Lai Jiangshan4468a002013-02-06 18:04:53 -0800612static void set_work_pool_and_keep_pending(struct work_struct *work,
613 int pool_id)
614{
615 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
616 WORK_STRUCT_PENDING);
617}
618
Tejun Heo7c3eed52013-01-24 11:01:33 -0800619static void set_work_pool_and_clear_pending(struct work_struct *work,
620 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000621{
Tejun Heo23657bb2012-08-13 17:08:19 -0700622 /*
623 * The following wmb is paired with the implied mb in
624 * test_and_set_bit(PENDING) and ensures all updates to @work made
625 * here are visible to and precede any updates by the next PENDING
626 * owner.
627 */
628 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800629 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200630}
631
632static void clear_work_data(struct work_struct *work)
633{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800634 smp_wmb(); /* see set_work_pool_and_clear_pending() */
635 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200636}
637
Tejun Heo112202d2013-02-13 19:29:12 -0800638static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200639{
Tejun Heoe1201532010-07-22 14:14:25 +0200640 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200641
Tejun Heo112202d2013-02-13 19:29:12 -0800642 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200643 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
644 else
645 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200646}
647
Tejun Heo7c3eed52013-01-24 11:01:33 -0800648/**
649 * get_work_pool - return the worker_pool a given work was associated with
650 * @work: the work item of interest
651 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700652 * Pools are created and destroyed under wq_pool_mutex, and allows read
653 * access under sched-RCU read lock. As such, this function should be
654 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700655 *
656 * All fields of the returned pool are accessible as long as the above
657 * mentioned locking is in effect. If the returned pool needs to be used
658 * beyond the critical section, the caller is responsible for ensuring the
659 * returned pool is and stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700660 *
661 * Return: The worker_pool @work was last associated with. %NULL if none.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662 */
663static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200664{
Tejun Heoe1201532010-07-22 14:14:25 +0200665 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800666 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200667
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700668 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700669
Tejun Heo112202d2013-02-13 19:29:12 -0800670 if (data & WORK_STRUCT_PWQ)
671 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800672 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200673
Tejun Heo7c3eed52013-01-24 11:01:33 -0800674 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
675 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200676 return NULL;
677
Tejun Heofa1b54e2013-03-12 11:30:00 -0700678 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800679}
680
681/**
682 * get_work_pool_id - return the worker pool ID a given work is associated with
683 * @work: the work item of interest
684 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700685 * Return: The worker_pool ID @work was last associated with.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800686 * %WORK_OFFQ_POOL_NONE if none.
687 */
688static int get_work_pool_id(struct work_struct *work)
689{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800690 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800691
Tejun Heo112202d2013-02-13 19:29:12 -0800692 if (data & WORK_STRUCT_PWQ)
693 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800694 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
695
696 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800697}
698
Tejun Heobbb68df2012-08-03 10:30:46 -0700699static void mark_work_canceling(struct work_struct *work)
700{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800701 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700702
Tejun Heo7c3eed52013-01-24 11:01:33 -0800703 pool_id <<= WORK_OFFQ_POOL_SHIFT;
704 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700705}
706
707static bool work_is_canceling(struct work_struct *work)
708{
709 unsigned long data = atomic_long_read(&work->data);
710
Tejun Heo112202d2013-02-13 19:29:12 -0800711 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700712}
713
David Howells365970a2006-11-22 14:54:49 +0000714/*
Tejun Heo32704762012-07-13 22:16:45 -0700715 * Policy functions. These define the policies on how the global worker
716 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800717 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000718 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200719
Tejun Heo63d95a92012-07-12 14:46:37 -0700720static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000721{
Tejun Heoe19e3972013-01-24 11:39:44 -0800722 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000723}
724
Tejun Heoe22bee72010-06-29 10:07:14 +0200725/*
726 * Need to wake up a worker? Called from anything but currently
727 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700728 *
729 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800730 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700731 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200732 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700733static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000734{
Tejun Heo63d95a92012-07-12 14:46:37 -0700735 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000736}
737
Tejun Heoe22bee72010-06-29 10:07:14 +0200738/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700739static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200740{
Tejun Heo63d95a92012-07-12 14:46:37 -0700741 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200742}
743
744/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700745static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200746{
Tejun Heoe19e3972013-01-24 11:39:44 -0800747 return !list_empty(&pool->worklist) &&
748 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200749}
750
751/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700752static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200753{
Tejun Heo63d95a92012-07-12 14:46:37 -0700754 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200755}
756
Tejun Heoe22bee72010-06-29 10:07:14 +0200757/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700758static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200759{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700760 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700761 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
762 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200763
764 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
765}
766
767/*
768 * Wake up functions.
769 */
770
Lai Jiangshan1037de32014-05-22 16:44:07 +0800771/* Return the first idle worker. Safe with preemption disabled */
772static struct worker *first_idle_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200773{
Tejun Heo63d95a92012-07-12 14:46:37 -0700774 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200775 return NULL;
776
Tejun Heo63d95a92012-07-12 14:46:37 -0700777 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200778}
779
780/**
781 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700782 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200783 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700784 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200785 *
786 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800787 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200788 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700789static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200790{
Lai Jiangshan1037de32014-05-22 16:44:07 +0800791 struct worker *worker = first_idle_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200792
793 if (likely(worker))
794 wake_up_process(worker->task);
795}
796
Tejun Heo4690c4a2010-06-29 10:07:10 +0200797/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200798 * wq_worker_waking_up - a worker is waking up
799 * @task: task waking up
800 * @cpu: CPU @task is waking up to
801 *
802 * This function is called during try_to_wake_up() when a worker is
803 * being awoken.
804 *
805 * CONTEXT:
806 * spin_lock_irq(rq->lock)
807 */
Tejun Heod84ff052013-03-12 11:29:59 -0700808void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200809{
810 struct worker *worker = kthread_data(task);
811
Joonsoo Kim36576002012-10-26 23:03:49 +0900812 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800813 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800814 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900815 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200816}
817
818/**
819 * wq_worker_sleeping - a worker is going to sleep
820 * @task: task going to sleep
821 * @cpu: CPU in question, must be the current CPU number
822 *
823 * This function is called during schedule() when a busy worker is
824 * going to sleep. Worker on the same cpu can be woken up by
825 * returning pointer to its task.
826 *
827 * CONTEXT:
828 * spin_lock_irq(rq->lock)
829 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700830 * Return:
Tejun Heoe22bee72010-06-29 10:07:14 +0200831 * Worker task on @cpu to wake up, %NULL if none.
832 */
Tejun Heod84ff052013-03-12 11:29:59 -0700833struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200834{
835 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800836 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200837
Tejun Heo111c2252013-01-17 17:16:24 -0800838 /*
839 * Rescuers, which may not have all the fields set up like normal
840 * workers, also reach here, let's not access anything before
841 * checking NOT_RUNNING.
842 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500843 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 return NULL;
845
Tejun Heo111c2252013-01-17 17:16:24 -0800846 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800847
Tejun Heoe22bee72010-06-29 10:07:14 +0200848 /* this can only happen on the local cpu */
Lai Jiangshan92b69f52014-06-03 15:33:08 +0800849 if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
Tejun Heo6183c002013-03-12 11:29:57 -0700850 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200851
852 /*
853 * The counterpart of the following dec_and_test, implied mb,
854 * worklist not empty test sequence is in insert_work().
855 * Please read comment there.
856 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700857 * NOT_RUNNING is clear. This means that we're bound to and
858 * running on the local cpu w/ rq lock held and preemption
859 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800860 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700861 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200862 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800863 if (atomic_dec_and_test(&pool->nr_running) &&
864 !list_empty(&pool->worklist))
Lai Jiangshan1037de32014-05-22 16:44:07 +0800865 to_wakeup = first_idle_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200866 return to_wakeup ? to_wakeup->task : NULL;
867}
868
869/**
870 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200871 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200872 * @flags: flags to set
Tejun Heod302f012010-06-29 10:07:13 +0200873 *
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800874 * Set @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200875 *
Tejun Heocb444762010-07-02 10:03:50 +0200876 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800877 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200878 */
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800879static inline void worker_set_flags(struct worker *worker, unsigned int flags)
Tejun Heod302f012010-06-29 10:07:13 +0200880{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700881 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200882
Tejun Heocb444762010-07-02 10:03:50 +0200883 WARN_ON_ONCE(worker->task != current);
884
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800885 /* If transitioning into NOT_RUNNING, adjust nr_running. */
Tejun Heoe22bee72010-06-29 10:07:14 +0200886 if ((flags & WORKER_NOT_RUNNING) &&
887 !(worker->flags & WORKER_NOT_RUNNING)) {
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800888 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200889 }
890
Tejun Heod302f012010-06-29 10:07:13 +0200891 worker->flags |= flags;
892}
893
894/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200895 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200896 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200897 * @flags: flags to clear
898 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200899 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200900 *
Tejun Heocb444762010-07-02 10:03:50 +0200901 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800902 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200903 */
904static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
905{
Tejun Heo63d95a92012-07-12 14:46:37 -0700906 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200907 unsigned int oflags = worker->flags;
908
Tejun Heocb444762010-07-02 10:03:50 +0200909 WARN_ON_ONCE(worker->task != current);
910
Tejun Heod302f012010-06-29 10:07:13 +0200911 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200912
Tejun Heo42c025f2011-01-11 15:58:49 +0100913 /*
914 * If transitioning out of NOT_RUNNING, increment nr_running. Note
915 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
916 * of multiple flags, not a single flag.
917 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200918 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
919 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800920 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200921}
922
923/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200924 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800925 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200926 * @work: work to find worker for
927 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800928 * Find a worker which is executing @work on @pool by searching
929 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800930 * to match, its current execution should match the address of @work and
931 * its work function. This is to avoid unwanted dependency between
932 * unrelated work executions through a work item being recycled while still
933 * being executed.
934 *
935 * This is a bit tricky. A work item may be freed once its execution
936 * starts and nothing prevents the freed area from being recycled for
937 * another work item. If the same work item address ends up being reused
938 * before the original execution finishes, workqueue will identify the
939 * recycled work item as currently executing and make it wait until the
940 * current execution finishes, introducing an unwanted dependency.
941 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700942 * This function checks the work item address and work function to avoid
943 * false positives. Note that this isn't complete as one may construct a
944 * work function which can introduce dependency onto itself through a
945 * recycled work item. Well, if somebody wants to shoot oneself in the
946 * foot that badly, there's only so much we can do, and if such deadlock
947 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200948 *
949 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800950 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200951 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700952 * Return:
953 * Pointer to worker which is executing @work if found, %NULL
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200954 * otherwise.
955 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800956static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200957 struct work_struct *work)
958{
Sasha Levin42f85702012-12-17 10:01:23 -0500959 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500960
Sasha Levinb67bfe02013-02-27 17:06:00 -0800961 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800962 (unsigned long)work)
963 if (worker->current_work == work &&
964 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500965 return worker;
966
967 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200968}
969
970/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700971 * move_linked_works - move linked works to a list
972 * @work: start of series of works to be scheduled
973 * @head: target list to append @work to
974 * @nextp: out paramter for nested worklist walking
975 *
976 * Schedule linked works starting from @work to @head. Work series to
977 * be scheduled starts at @work and includes any consecutive work with
978 * WORK_STRUCT_LINKED set in its predecessor.
979 *
980 * If @nextp is not NULL, it's updated to point to the next work of
981 * the last scheduled work. This allows move_linked_works() to be
982 * nested inside outer list_for_each_entry_safe().
983 *
984 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800985 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700986 */
987static void move_linked_works(struct work_struct *work, struct list_head *head,
988 struct work_struct **nextp)
989{
990 struct work_struct *n;
991
992 /*
993 * Linked worklist will always end before the end of the list,
994 * use NULL for list head.
995 */
996 list_for_each_entry_safe_from(work, n, NULL, entry) {
997 list_move_tail(&work->entry, head);
998 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
999 break;
1000 }
1001
1002 /*
1003 * If we're already inside safe list traversal and have moved
1004 * multiple works to the scheduled queue, the next position
1005 * needs to be updated.
1006 */
1007 if (nextp)
1008 *nextp = n;
1009}
1010
Tejun Heo8864b4e2013-03-12 11:30:04 -07001011/**
1012 * get_pwq - get an extra reference on the specified pool_workqueue
1013 * @pwq: pool_workqueue to get
1014 *
1015 * Obtain an extra reference on @pwq. The caller should guarantee that
1016 * @pwq has positive refcnt and be holding the matching pool->lock.
1017 */
1018static void get_pwq(struct pool_workqueue *pwq)
1019{
1020 lockdep_assert_held(&pwq->pool->lock);
1021 WARN_ON_ONCE(pwq->refcnt <= 0);
1022 pwq->refcnt++;
1023}
1024
1025/**
1026 * put_pwq - put a pool_workqueue reference
1027 * @pwq: pool_workqueue to put
1028 *
1029 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1030 * destruction. The caller should be holding the matching pool->lock.
1031 */
1032static void put_pwq(struct pool_workqueue *pwq)
1033{
1034 lockdep_assert_held(&pwq->pool->lock);
1035 if (likely(--pwq->refcnt))
1036 return;
1037 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1038 return;
1039 /*
1040 * @pwq can't be released under pool->lock, bounce to
1041 * pwq_unbound_release_workfn(). This never recurses on the same
1042 * pool->lock as this path is taken only for unbound workqueues and
1043 * the release work item is scheduled on a per-cpu workqueue. To
1044 * avoid lockdep warning, unbound pool->locks are given lockdep
1045 * subclass of 1 in get_unbound_pool().
1046 */
1047 schedule_work(&pwq->unbound_release_work);
1048}
1049
Tejun Heodce90d42013-04-01 11:23:35 -07001050/**
1051 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1052 * @pwq: pool_workqueue to put (can be %NULL)
1053 *
1054 * put_pwq() with locking. This function also allows %NULL @pwq.
1055 */
1056static void put_pwq_unlocked(struct pool_workqueue *pwq)
1057{
1058 if (pwq) {
1059 /*
1060 * As both pwqs and pools are sched-RCU protected, the
1061 * following lock operations are safe.
1062 */
1063 spin_lock_irq(&pwq->pool->lock);
1064 put_pwq(pwq);
1065 spin_unlock_irq(&pwq->pool->lock);
1066 }
1067}
1068
Tejun Heo112202d2013-02-13 19:29:12 -08001069static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001070{
Tejun Heo112202d2013-02-13 19:29:12 -08001071 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001072
1073 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001074 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001075 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001076 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001077}
1078
Tejun Heo112202d2013-02-13 19:29:12 -08001079static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001080{
Tejun Heo112202d2013-02-13 19:29:12 -08001081 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001082 struct work_struct, entry);
1083
Tejun Heo112202d2013-02-13 19:29:12 -08001084 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001085}
1086
Tejun Heobf4ede02012-08-03 10:30:46 -07001087/**
Tejun Heo112202d2013-02-13 19:29:12 -08001088 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1089 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001090 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001091 *
1092 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001093 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001094 *
1095 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001096 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001097 */
Tejun Heo112202d2013-02-13 19:29:12 -08001098static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001099{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001100 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001101 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001102 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001103
Tejun Heo112202d2013-02-13 19:29:12 -08001104 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001105
Tejun Heo112202d2013-02-13 19:29:12 -08001106 pwq->nr_active--;
1107 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001108 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001109 if (pwq->nr_active < pwq->max_active)
1110 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001111 }
1112
1113 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001114 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001115 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001116
1117 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001118 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001119 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001120
Tejun Heo112202d2013-02-13 19:29:12 -08001121 /* this pwq is done, clear flush_color */
1122 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001123
1124 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001125 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001126 * will handle the rest.
1127 */
Tejun Heo112202d2013-02-13 19:29:12 -08001128 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1129 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001130out_put:
1131 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001132}
1133
Tejun Heo36e227d2012-08-03 10:30:46 -07001134/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001135 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001136 * @work: work item to steal
1137 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001138 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001139 *
1140 * Try to grab PENDING bit of @work. This function can handle @work in any
Yacine Belkadid185af32013-07-31 14:59:24 -07001141 * stable state - idle, on timer or on worklist.
Tejun Heo36e227d2012-08-03 10:30:46 -07001142 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001143 * Return:
Tejun Heo36e227d2012-08-03 10:30:46 -07001144 * 1 if @work was pending and we successfully stole PENDING
1145 * 0 if @work was idle and we claimed PENDING
1146 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001147 * -ENOENT if someone else is canceling @work, this state may persist
1148 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001149 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001150 * Note:
Tejun Heobbb68df2012-08-03 10:30:46 -07001151 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001152 * interrupted while holding PENDING and @work off queue, irq must be
1153 * disabled on entry. This, combined with delayed_work->timer being
1154 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001155 *
1156 * On successful return, >= 0, irq is disabled and the caller is
1157 * responsible for releasing it using local_irq_restore(*@flags).
1158 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001159 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001160 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001161static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1162 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001163{
Tejun Heod565ed62013-01-24 11:01:33 -08001164 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001165 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001166
Tejun Heobbb68df2012-08-03 10:30:46 -07001167 local_irq_save(*flags);
1168
Tejun Heo36e227d2012-08-03 10:30:46 -07001169 /* try to steal the timer if it exists */
1170 if (is_dwork) {
1171 struct delayed_work *dwork = to_delayed_work(work);
1172
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001173 /*
1174 * dwork->timer is irqsafe. If del_timer() fails, it's
1175 * guaranteed that the timer is not queued anywhere and not
1176 * running on the local CPU.
1177 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001178 if (likely(del_timer(&dwork->timer)))
1179 return 1;
1180 }
1181
1182 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001183 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1184 return 0;
1185
1186 /*
1187 * The queueing is in progress, or it is already queued. Try to
1188 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1189 */
Tejun Heod565ed62013-01-24 11:01:33 -08001190 pool = get_work_pool(work);
1191 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001192 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001193
Tejun Heod565ed62013-01-24 11:01:33 -08001194 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001195 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001196 * work->data is guaranteed to point to pwq only while the work
1197 * item is queued on pwq->wq, and both updating work->data to point
1198 * to pwq on queueing and to pool on dequeueing are done under
1199 * pwq->pool->lock. This in turn guarantees that, if work->data
1200 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001201 * item is currently queued on that pool.
1202 */
Tejun Heo112202d2013-02-13 19:29:12 -08001203 pwq = get_work_pwq(work);
1204 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001205 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001206
Tejun Heo16062832013-02-06 18:04:53 -08001207 /*
1208 * A delayed work item cannot be grabbed directly because
1209 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001210 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001211 * management later on and cause stall. Make sure the work
1212 * item is activated before grabbing.
1213 */
1214 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001215 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001216
Tejun Heo16062832013-02-06 18:04:53 -08001217 list_del_init(&work->entry);
Lai Jiangshan9c34a702014-07-11 00:11:13 +08001218 pwq_dec_nr_in_flight(pwq, get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001219
Tejun Heo112202d2013-02-13 19:29:12 -08001220 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001221 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001222
Tejun Heo16062832013-02-06 18:04:53 -08001223 spin_unlock(&pool->lock);
1224 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001225 }
Tejun Heod565ed62013-01-24 11:01:33 -08001226 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001227fail:
1228 local_irq_restore(*flags);
1229 if (work_is_canceling(work))
1230 return -ENOENT;
1231 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001232 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001233}
1234
1235/**
Tejun Heo706026c2013-01-24 11:01:34 -08001236 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001237 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001238 * @work: work to insert
1239 * @head: insertion point
1240 * @extra_flags: extra WORK_STRUCT_* flags to set
1241 *
Tejun Heo112202d2013-02-13 19:29:12 -08001242 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001243 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001244 *
1245 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001246 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001247 */
Tejun Heo112202d2013-02-13 19:29:12 -08001248static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1249 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001250{
Tejun Heo112202d2013-02-13 19:29:12 -08001251 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001252
Tejun Heo4690c4a2010-06-29 10:07:10 +02001253 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001254 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001255 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001256 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001257
1258 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001259 * Ensure either wq_worker_sleeping() sees the above
1260 * list_add_tail() or we see zero nr_running to avoid workers lying
1261 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001262 */
1263 smp_mb();
1264
Tejun Heo63d95a92012-07-12 14:46:37 -07001265 if (__need_more_worker(pool))
1266 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001267}
1268
Tejun Heoc8efcc22010-12-20 19:32:04 +01001269/*
1270 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001271 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001272 */
1273static bool is_chained_work(struct workqueue_struct *wq)
1274{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001275 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001276
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001277 worker = current_wq_worker();
1278 /*
1279 * Return %true iff I'm a worker execuing a work item on @wq. If
1280 * I'm @worker, it's safe to dereference it without locking.
1281 */
Tejun Heo112202d2013-02-13 19:29:12 -08001282 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001283}
1284
Tejun Heod84ff052013-03-12 11:29:59 -07001285static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct work_struct *work)
1287{
Tejun Heo112202d2013-02-13 19:29:12 -08001288 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001289 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001290 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001291 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001292 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001293
1294 /*
1295 * While a work item is PENDING && off queue, a task trying to
1296 * steal the PENDING will busy-loop waiting for it to either get
1297 * queued or lose PENDING. Grabbing PENDING and queueing should
1298 * happen with IRQ disabled.
1299 */
1300 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001302 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001303
Li Bin9ef28a72013-09-09 13:13:58 +08001304 /* if draining, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001305 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001306 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001307 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001308retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001309 if (req_cpu == WORK_CPU_UNBOUND)
1310 cpu = raw_smp_processor_id();
1311
Tejun Heoc9178082013-03-12 11:30:04 -07001312 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001313 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001314 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001315 else
1316 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001317
Tejun Heoc9178082013-03-12 11:30:04 -07001318 /*
1319 * If @work was previously on a different pool, it might still be
1320 * running there, in which case the work needs to be queued on that
1321 * pool to guarantee non-reentrancy.
1322 */
1323 last_pool = get_work_pool(work);
1324 if (last_pool && last_pool != pwq->pool) {
1325 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001326
Tejun Heoc9178082013-03-12 11:30:04 -07001327 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001328
Tejun Heoc9178082013-03-12 11:30:04 -07001329 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001330
Tejun Heoc9178082013-03-12 11:30:04 -07001331 if (worker && worker->current_pwq->wq == wq) {
1332 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001333 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001334 /* meh... not running there, queue here */
1335 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001336 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001337 }
Tejun Heof3421792010-07-02 10:03:51 +02001338 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001339 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001340 }
1341
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001342 /*
1343 * pwq is determined and locked. For unbound pools, we could have
1344 * raced with pwq release and it could already be dead. If its
1345 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001346 * without another pwq replacing it in the numa_pwq_tbl or while
1347 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001348 * make forward-progress.
1349 */
1350 if (unlikely(!pwq->refcnt)) {
1351 if (wq->flags & WQ_UNBOUND) {
1352 spin_unlock(&pwq->pool->lock);
1353 cpu_relax();
1354 goto retry;
1355 }
1356 /* oops */
1357 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1358 wq->name, cpu);
1359 }
1360
Tejun Heo112202d2013-02-13 19:29:12 -08001361 /* pwq determined, queue */
1362 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001363
Dan Carpenterf5b25522012-04-13 22:06:58 +03001364 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001365 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001366 return;
1367 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001368
Tejun Heo112202d2013-02-13 19:29:12 -08001369 pwq->nr_in_flight[pwq->work_color]++;
1370 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001371
Tejun Heo112202d2013-02-13 19:29:12 -08001372 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001373 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001374 pwq->nr_active++;
1375 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001376 } else {
1377 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001378 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001379 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001380
Tejun Heo112202d2013-02-13 19:29:12 -08001381 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001382
Tejun Heo112202d2013-02-13 19:29:12 -08001383 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001386/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001387 * queue_work_on - queue work on specific cpu
1388 * @cpu: CPU number to execute work on
1389 * @wq: workqueue to use
1390 * @work: work to queue
1391 *
Zhang Ruic1a220e2008-07-23 21:28:39 -07001392 * We queue the work to a specific CPU, the caller must ensure it
1393 * can't go away.
Yacine Belkadid185af32013-07-31 14:59:24 -07001394 *
1395 * Return: %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001396 */
Tejun Heod4283e92012-08-03 10:30:44 -07001397bool queue_work_on(int cpu, struct workqueue_struct *wq,
1398 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001399{
Tejun Heod4283e92012-08-03 10:30:44 -07001400 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001401 unsigned long flags;
1402
1403 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001404
Tejun Heo22df02b2010-06-29 10:07:10 +02001405 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001406 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001407 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001408 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001409
1410 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001411 return ret;
1412}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001413EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001414
Tejun Heod8e794d2012-08-03 10:30:45 -07001415void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
David Howells52bad642006-11-22 14:54:01 +00001417 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001419 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001420 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001422EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001424static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1425 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001427 struct timer_list *timer = &dwork->timer;
1428 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001430 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1431 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001432 WARN_ON_ONCE(timer_pending(timer));
1433 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001434
Tejun Heo8852aac2012-12-01 16:23:42 -08001435 /*
1436 * If @delay is 0, queue @dwork->work immediately. This is for
1437 * both optimization and correctness. The earliest @timer can
1438 * expire is on the closest next tick and delayed_work users depend
1439 * on that there's no such delay when @delay is 0.
1440 */
1441 if (!delay) {
1442 __queue_work(cpu, wq, &dwork->work);
1443 return;
1444 }
1445
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001446 timer_stats_timer_set_start_info(&dwork->timer);
1447
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001448 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001449 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001450 timer->expires = jiffies + delay;
1451
1452 if (unlikely(cpu != WORK_CPU_UNBOUND))
1453 add_timer_on(timer, cpu);
1454 else
1455 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001458/**
1459 * queue_delayed_work_on - queue work on specific CPU after delay
1460 * @cpu: CPU number to execute work on
1461 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001462 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001463 * @delay: number of jiffies to wait before queueing
1464 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001465 * Return: %false if @work was already on a queue, %true otherwise. If
Tejun Heo715f1302012-08-03 10:30:46 -07001466 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1467 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001468 */
Tejun Heod4283e92012-08-03 10:30:44 -07001469bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1470 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001471{
David Howells52bad642006-11-22 14:54:01 +00001472 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001473 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001474 unsigned long flags;
1475
1476 /* read the comment in __queue_work() */
1477 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001478
Tejun Heo22df02b2010-06-29 10:07:10 +02001479 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001480 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001481 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001482 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001483
1484 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001485 return ret;
1486}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001487EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001490 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1491 * @cpu: CPU number to execute work on
1492 * @wq: workqueue to use
1493 * @dwork: work to queue
1494 * @delay: number of jiffies to wait before queueing
1495 *
1496 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1497 * modify @dwork's timer so that it expires after @delay. If @delay is
1498 * zero, @work is guaranteed to be scheduled immediately regardless of its
1499 * current state.
1500 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001501 * Return: %false if @dwork was idle and queued, %true if @dwork was
Tejun Heo8376fe22012-08-03 10:30:47 -07001502 * pending and its timer was modified.
1503 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001504 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001505 * See try_to_grab_pending() for details.
1506 */
1507bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1508 struct delayed_work *dwork, unsigned long delay)
1509{
1510 unsigned long flags;
1511 int ret;
1512
1513 do {
1514 ret = try_to_grab_pending(&dwork->work, true, &flags);
1515 } while (unlikely(ret == -EAGAIN));
1516
1517 if (likely(ret >= 0)) {
1518 __queue_delayed_work(cpu, wq, dwork, delay);
1519 local_irq_restore(flags);
1520 }
1521
1522 /* -ENOENT from try_to_grab_pending() becomes %true */
1523 return ret;
1524}
1525EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1526
1527/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001528 * worker_enter_idle - enter idle state
1529 * @worker: worker which is entering idle state
1530 *
1531 * @worker is entering idle state. Update stats and idle timer if
1532 * necessary.
1533 *
1534 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001535 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001536 */
1537static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001539 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Tejun Heo6183c002013-03-12 11:29:57 -07001541 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1542 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1543 (worker->hentry.next || worker->hentry.pprev)))
1544 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Lai Jiangshan051e1852014-07-22 13:03:02 +08001546 /* can't use worker_set_flags(), also called from create_worker() */
Tejun Heocb444762010-07-02 10:03:50 +02001547 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001548 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001549 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001550
Tejun Heoc8e55f32010-06-29 10:07:12 +02001551 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001552 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001553
Tejun Heo628c78e2012-07-17 12:39:27 -07001554 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1555 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001556
Tejun Heo544ecf32012-05-14 15:04:50 -07001557 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001558 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001559 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001560 * nr_running, the warning may trigger spuriously. Check iff
1561 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001562 */
Tejun Heo24647572013-01-24 11:01:33 -08001563 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001564 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001565 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Tejun Heoc8e55f32010-06-29 10:07:12 +02001568/**
1569 * worker_leave_idle - leave idle state
1570 * @worker: worker which is leaving idle state
1571 *
1572 * @worker is leaving idle state. Update stats.
1573 *
1574 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001575 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001576 */
1577static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001579 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Tejun Heo6183c002013-03-12 11:29:57 -07001581 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1582 return;
Tejun Heod302f012010-06-29 10:07:13 +02001583 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001584 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001585 list_del_init(&worker->entry);
1586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001588static struct worker *alloc_worker(int node)
Tejun Heoc34056a2010-06-29 10:07:11 +02001589{
1590 struct worker *worker;
1591
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001592 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001593 if (worker) {
1594 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001595 INIT_LIST_HEAD(&worker->scheduled);
Lai Jiangshanda028462014-05-20 17:46:31 +08001596 INIT_LIST_HEAD(&worker->node);
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 /* on creation a worker is in !idle && prep state */
1598 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001599 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001600 return worker;
1601}
1602
1603/**
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001604 * worker_attach_to_pool() - attach a worker to a pool
1605 * @worker: worker to be attached
1606 * @pool: the target pool
1607 *
1608 * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
1609 * cpu-binding of @worker are kept coordinated with the pool across
1610 * cpu-[un]hotplugs.
1611 */
1612static void worker_attach_to_pool(struct worker *worker,
1613 struct worker_pool *pool)
1614{
1615 mutex_lock(&pool->attach_mutex);
1616
1617 /*
1618 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1619 * online CPUs. It'll be re-applied when any of the CPUs come up.
1620 */
1621 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1622
1623 /*
1624 * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
1625 * stable across this function. See the comments above the
1626 * flag definition for details.
1627 */
1628 if (pool->flags & POOL_DISASSOCIATED)
1629 worker->flags |= WORKER_UNBOUND;
1630
1631 list_add_tail(&worker->node, &pool->workers);
1632
1633 mutex_unlock(&pool->attach_mutex);
1634}
1635
1636/**
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001637 * worker_detach_from_pool() - detach a worker from its pool
1638 * @worker: worker which is attached to its pool
1639 * @pool: the pool @worker is attached to
1640 *
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001641 * Undo the attaching which had been done in worker_attach_to_pool(). The
1642 * caller worker shouldn't access to the pool after detached except it has
1643 * other reference to the pool.
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001644 */
1645static void worker_detach_from_pool(struct worker *worker,
1646 struct worker_pool *pool)
1647{
1648 struct completion *detach_completion = NULL;
1649
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001650 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08001651 list_del(&worker->node);
1652 if (list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001653 detach_completion = pool->detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001654 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001655
Lai Jiangshanb62c0752014-06-03 15:32:52 +08001656 /* clear leftover flags without pool->lock after it is detached */
1657 worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
1658
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001659 if (detach_completion)
1660 complete(detach_completion);
1661}
1662
1663/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001664 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001665 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001666 *
Lai Jiangshan051e1852014-07-22 13:03:02 +08001667 * Create and start a new worker which is attached to @pool.
Tejun Heoc34056a2010-06-29 10:07:11 +02001668 *
1669 * CONTEXT:
1670 * Might sleep. Does GFP_KERNEL allocations.
1671 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001672 * Return:
Tejun Heoc34056a2010-06-29 10:07:11 +02001673 * Pointer to the newly created worker.
1674 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001675static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001676{
Tejun Heoc34056a2010-06-29 10:07:11 +02001677 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001678 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001679 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001680
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001681 /* ID is needed to determine kthread name */
1682 id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL);
Tejun Heo822d8402013-03-19 13:45:21 -07001683 if (id < 0)
1684 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001685
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001686 worker = alloc_worker(pool->node);
Tejun Heoc34056a2010-06-29 10:07:11 +02001687 if (!worker)
1688 goto fail;
1689
Tejun Heobd7bdd42012-07-12 14:46:37 -07001690 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001691 worker->id = id;
1692
Tejun Heo29c91e92013-03-12 11:30:03 -07001693 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001694 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1695 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001696 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001697 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1698
Tejun Heof3f90ad2013-04-01 11:23:34 -07001699 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001700 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001701 if (IS_ERR(worker->task))
1702 goto fail;
1703
Oleg Nesterov91151222013-11-14 12:56:18 +01001704 set_user_nice(worker->task, pool->attrs->nice);
1705
1706 /* prevent userland from meddling with cpumask of workqueue workers */
1707 worker->task->flags |= PF_NO_SETAFFINITY;
1708
Lai Jiangshanda028462014-05-20 17:46:31 +08001709 /* successful, attach the worker to the pool */
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001710 worker_attach_to_pool(worker, pool);
Tejun Heo822d8402013-03-19 13:45:21 -07001711
Lai Jiangshan051e1852014-07-22 13:03:02 +08001712 /* start the newly created worker */
1713 spin_lock_irq(&pool->lock);
1714 worker->pool->nr_workers++;
1715 worker_enter_idle(worker);
1716 wake_up_process(worker->task);
1717 spin_unlock_irq(&pool->lock);
1718
Tejun Heoc34056a2010-06-29 10:07:11 +02001719 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001720
Tejun Heoc34056a2010-06-29 10:07:11 +02001721fail:
Lai Jiangshan9625ab12014-05-20 17:46:27 +08001722 if (id >= 0)
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001723 ida_simple_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001724 kfree(worker);
1725 return NULL;
1726}
1727
1728/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 * destroy_worker - destroy a workqueue worker
1730 * @worker: worker to be destroyed
1731 *
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001732 * Destroy @worker and adjust @pool stats accordingly. The worker should
1733 * be idle.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001734 *
1735 * CONTEXT:
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001736 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 */
1738static void destroy_worker(struct worker *worker)
1739{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001740 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001741
Tejun Heocd549682013-03-13 19:47:39 -07001742 lockdep_assert_held(&pool->lock);
1743
Tejun Heoc34056a2010-06-29 10:07:11 +02001744 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001745 if (WARN_ON(worker->current_work) ||
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001746 WARN_ON(!list_empty(&worker->scheduled)) ||
1747 WARN_ON(!(worker->flags & WORKER_IDLE)))
Tejun Heo6183c002013-03-12 11:29:57 -07001748 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001749
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001750 pool->nr_workers--;
1751 pool->nr_idle--;
Lai Jiangshan5bdfff92014-02-15 22:02:28 +08001752
Tejun Heoc8e55f32010-06-29 10:07:12 +02001753 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001754 worker->flags |= WORKER_DIE;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001755 wake_up_process(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001756}
1757
Tejun Heo63d95a92012-07-12 14:46:37 -07001758static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001759{
Tejun Heo63d95a92012-07-12 14:46:37 -07001760 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001761
Tejun Heod565ed62013-01-24 11:01:33 -08001762 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001763
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001764 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001765 struct worker *worker;
1766 unsigned long expires;
1767
1768 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001769 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001770 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1771
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001772 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001773 mod_timer(&pool->idle_timer, expires);
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001774 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02001775 }
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001776
1777 destroy_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02001778 }
1779
Tejun Heod565ed62013-01-24 11:01:33 -08001780 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001781}
1782
Tejun Heo493a1722013-03-12 11:29:59 -07001783static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001784{
Tejun Heo112202d2013-02-13 19:29:12 -08001785 struct pool_workqueue *pwq = get_work_pwq(work);
1786 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001787
Tejun Heo2e109a22013-03-13 19:47:40 -07001788 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001789
Tejun Heo493008a2013-03-12 11:30:03 -07001790 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001791 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001792
1793 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001794 if (list_empty(&pwq->mayday_node)) {
Lai Jiangshan77668c82014-04-18 11:04:16 -04001795 /*
1796 * If @pwq is for an unbound wq, its base ref may be put at
1797 * any time due to an attribute change. Pin @pwq until the
1798 * rescuer is done with it.
1799 */
1800 get_pwq(pwq);
Tejun Heo493a1722013-03-12 11:29:59 -07001801 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001802 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001803 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001804}
1805
Tejun Heo706026c2013-01-24 11:01:34 -08001806static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001807{
Tejun Heo63d95a92012-07-12 14:46:37 -07001808 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001809 struct work_struct *work;
1810
Tejun Heob2d82902014-12-08 12:39:16 -05001811 spin_lock_irq(&pool->lock);
1812 spin_lock(&wq_mayday_lock); /* for wq->maydays */
Tejun Heoe22bee72010-06-29 10:07:14 +02001813
Tejun Heo63d95a92012-07-12 14:46:37 -07001814 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001815 /*
1816 * We've been trying to create a new worker but
1817 * haven't been successful. We might be hitting an
1818 * allocation deadlock. Send distress signals to
1819 * rescuers.
1820 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001821 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001822 send_mayday(work);
1823 }
1824
Tejun Heob2d82902014-12-08 12:39:16 -05001825 spin_unlock(&wq_mayday_lock);
1826 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001827
Tejun Heo63d95a92012-07-12 14:46:37 -07001828 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001829}
1830
1831/**
1832 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001833 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001834 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001835 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001836 * have at least one idle worker on return from this function. If
1837 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001838 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001839 * possible allocation deadlock.
1840 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001841 * On return, need_to_create_worker() is guaranteed to be %false and
1842 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001843 *
1844 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001845 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001846 * multiple times. Does GFP_KERNEL allocations. Called only from
1847 * manager.
Tejun Heoe22bee72010-06-29 10:07:14 +02001848 */
Tejun Heo29187a92015-01-16 14:21:16 -05001849static void maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001850__releases(&pool->lock)
1851__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001852{
Tejun Heoe22bee72010-06-29 10:07:14 +02001853restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001854 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001855
Tejun Heoe22bee72010-06-29 10:07:14 +02001856 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001857 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001858
1859 while (true) {
Lai Jiangshan051e1852014-07-22 13:03:02 +08001860 if (create_worker(pool) || !need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001861 break;
1862
Lai Jiangshane212f362014-06-03 15:32:17 +08001863 schedule_timeout_interruptible(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001864
Tejun Heo63d95a92012-07-12 14:46:37 -07001865 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001866 break;
1867 }
1868
Tejun Heo63d95a92012-07-12 14:46:37 -07001869 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001870 spin_lock_irq(&pool->lock);
Lai Jiangshan051e1852014-07-22 13:03:02 +08001871 /*
1872 * This is necessary even after a new worker was just successfully
1873 * created as @pool->lock was dropped and the new worker might have
1874 * already become busy.
1875 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001876 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001877 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001878}
1879
1880/**
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 * manage_workers - manage worker pool
1882 * @worker: self
1883 *
Tejun Heo706026c2013-01-24 11:01:34 -08001884 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001885 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001886 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001887 *
1888 * The caller can safely start processing works on false return. On
1889 * true return, it's guaranteed that need_to_create_worker() is false
1890 * and may_start_working() is true.
1891 *
1892 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001893 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001894 * multiple times. Does GFP_KERNEL allocations.
1895 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001896 * Return:
Tejun Heo29187a92015-01-16 14:21:16 -05001897 * %false if the pool doesn't need management and the caller can safely
1898 * start processing works, %true if management function was performed and
1899 * the conditions that the caller verified before calling the function may
1900 * no longer be true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001901 */
1902static bool manage_workers(struct worker *worker)
1903{
Tejun Heo63d95a92012-07-12 14:46:37 -07001904 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001905
Tejun Heobc3a1af2013-03-13 19:47:39 -07001906 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07001907 * Anyone who successfully grabs manager_arb wins the arbitration
1908 * and becomes the manager. mutex_trylock() on pool->manager_arb
1909 * failure while holding pool->lock reliably indicates that someone
1910 * else is managing the pool and the worker which failed trylock
1911 * can proceed to executing work items. This means that anyone
1912 * grabbing manager_arb is responsible for actually performing
1913 * manager duties. If manager_arb is grabbed and released without
1914 * actual management, the pool may stall indefinitely.
Tejun Heobc3a1af2013-03-13 19:47:39 -07001915 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07001916 if (!mutex_trylock(&pool->manager_arb))
Tejun Heo29187a92015-01-16 14:21:16 -05001917 return false;
Tejun Heo2607d7a2015-03-09 09:22:28 -04001918 pool->manager = worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02001919
Tejun Heo29187a92015-01-16 14:21:16 -05001920 maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001921
Tejun Heo2607d7a2015-03-09 09:22:28 -04001922 pool->manager = NULL;
Tejun Heo34a06bd2013-03-12 11:30:00 -07001923 mutex_unlock(&pool->manager_arb);
Tejun Heo29187a92015-01-16 14:21:16 -05001924 return true;
Tejun Heoe22bee72010-06-29 10:07:14 +02001925}
1926
Tejun Heoa62428c2010-06-29 10:07:10 +02001927/**
1928 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001929 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001930 * @work: work to process
1931 *
1932 * Process @work. This function contains all the logics necessary to
1933 * process a single work including synchronization against and
1934 * interaction with other workers on the same cpu, queueing and
1935 * flushing. As long as context requirement is met, any worker can
1936 * call this function to process a work.
1937 *
1938 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001939 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001940 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001941static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08001942__releases(&pool->lock)
1943__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001944{
Tejun Heo112202d2013-02-13 19:29:12 -08001945 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001946 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001947 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001948 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001949 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001950#ifdef CONFIG_LOCKDEP
1951 /*
1952 * It is permissible to free the struct work_struct from
1953 * inside the function that is called from it, this we need to
1954 * take into account for lockdep too. To avoid bogus "held
1955 * lock freed" warnings as well as problems when looking into
1956 * work->lockdep_map, make a copy and use that here.
1957 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001958 struct lockdep_map lockdep_map;
1959
1960 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001961#endif
Lai Jiangshan807407c2014-06-03 15:33:28 +08001962 /* ensure we're on the correct CPU */
Lai Jiangshan85327af2014-06-03 15:33:28 +08001963 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08001964 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07001965
Tejun Heo7e116292010-06-29 10:07:13 +02001966 /*
1967 * A single work shouldn't be executed concurrently by
1968 * multiple workers on a single cpu. Check whether anyone is
1969 * already processing the work. If so, defer the work to the
1970 * currently executing one.
1971 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001972 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02001973 if (unlikely(collision)) {
1974 move_linked_works(work, &collision->scheduled, NULL);
1975 return;
1976 }
1977
Tejun Heo8930cab2012-08-03 10:30:45 -07001978 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02001979 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001980 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02001981 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08001982 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08001983 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001984 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001985
Tejun Heoa62428c2010-06-29 10:07:10 +02001986 list_del_init(&work->entry);
1987
Tejun Heo649027d2010-06-29 10:07:14 +02001988 /*
Lai Jiangshan228f1d02014-07-22 13:02:00 +08001989 * CPU intensive works don't participate in concurrency management.
1990 * They're the scheduler's responsibility. This takes @worker out
1991 * of concurrency management and the next code block will chain
1992 * execution of the pending work items.
Tejun Heofb0e7be2010-06-29 10:07:15 +02001993 */
1994 if (unlikely(cpu_intensive))
Lai Jiangshan228f1d02014-07-22 13:02:00 +08001995 worker_set_flags(worker, WORKER_CPU_INTENSIVE);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001996
Tejun Heo974271c2012-07-12 14:46:37 -07001997 /*
Lai Jiangshana489a032014-07-22 13:01:59 +08001998 * Wake up another worker if necessary. The condition is always
1999 * false for normal per-cpu workers since nr_running would always
2000 * be >= 1 at this point. This is used to chain execution of the
2001 * pending work items for WORKER_NOT_RUNNING workers such as the
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002002 * UNBOUND and CPU_INTENSIVE ones.
Tejun Heo974271c2012-07-12 14:46:37 -07002003 */
Lai Jiangshana489a032014-07-22 13:01:59 +08002004 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002005 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002006
Tejun Heo8930cab2012-08-03 10:30:45 -07002007 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002008 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002009 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002010 * PENDING and queued state changes happen together while IRQ is
2011 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002012 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002013 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002014
Tejun Heod565ed62013-01-24 11:01:33 -08002015 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002016
Tejun Heo112202d2013-02-13 19:29:12 -08002017 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002018 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002019 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002020 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002021 /*
2022 * While we must be careful to not use "work" after this, the trace
2023 * point will only record its address.
2024 */
2025 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002026 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002027 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002028
2029 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002030 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2031 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002032 current->comm, preempt_count(), task_pid_nr(current),
2033 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002034 debug_show_held_locks(current);
2035 dump_stack();
2036 }
2037
Tejun Heob22ce272013-08-28 17:33:37 -04002038 /*
2039 * The following prevents a kworker from hogging CPU on !PREEMPT
2040 * kernels, where a requeueing work item waiting for something to
2041 * happen could deadlock with stop_machine as such work item could
2042 * indefinitely requeue itself while all other CPUs are trapped in
Joe Lawrence789cbbe2014-10-05 13:24:21 -04002043 * stop_machine. At the same time, report a quiescent RCU state so
2044 * the same condition doesn't freeze RCU.
Tejun Heob22ce272013-08-28 17:33:37 -04002045 */
Joe Lawrence3e28e372014-10-05 13:24:22 -04002046 cond_resched_rcu_qs();
Tejun Heob22ce272013-08-28 17:33:37 -04002047
Tejun Heod565ed62013-01-24 11:01:33 -08002048 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002049
Tejun Heofb0e7be2010-06-29 10:07:15 +02002050 /* clear cpu intensive status */
2051 if (unlikely(cpu_intensive))
2052 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2053
Tejun Heoa62428c2010-06-29 10:07:10 +02002054 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002055 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002056 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002057 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002058 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002059 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002060 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002061}
2062
Tejun Heoaffee4b2010-06-29 10:07:12 +02002063/**
2064 * process_scheduled_works - process scheduled works
2065 * @worker: self
2066 *
2067 * Process all scheduled works. Please note that the scheduled list
2068 * may change while processing a work, so this function repeatedly
2069 * fetches a work from the top and executes it.
2070 *
2071 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002072 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002073 * multiple times.
2074 */
2075static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002077 while (!list_empty(&worker->scheduled)) {
2078 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002080 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
Tejun Heo4690c4a2010-06-29 10:07:10 +02002084/**
2085 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002086 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002087 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002088 * The worker thread function. All workers belong to a worker_pool -
2089 * either a per-cpu one or dynamic unbound one. These workers process all
2090 * work items regardless of their specific target workqueue. The only
2091 * exception is work items which belong to workqueues with a rescuer which
2092 * will be explained in rescuer_thread().
Yacine Belkadid185af32013-07-31 14:59:24 -07002093 *
2094 * Return: 0
Tejun Heo4690c4a2010-06-29 10:07:10 +02002095 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002096static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Tejun Heoc34056a2010-06-29 10:07:11 +02002098 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002099 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Tejun Heoe22bee72010-06-29 10:07:14 +02002101 /* tell the scheduler that this is a workqueue worker */
2102 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002103woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002104 spin_lock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002105
Tejun Heoa9ab7752013-03-19 13:45:21 -07002106 /* am I supposed to die? */
2107 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002108 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002109 WARN_ON_ONCE(!list_empty(&worker->entry));
2110 worker->task->flags &= ~PF_WQ_WORKER;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002111
2112 set_task_comm(worker->task, "kworker/dying");
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08002113 ida_simple_remove(&pool->worker_ida, worker->id);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002114 worker_detach_from_pool(worker, pool);
2115 kfree(worker);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002118
Tejun Heoc8e55f32010-06-29 10:07:12 +02002119 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002120recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002121 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002122 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002123 goto sleep;
2124
2125 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002126 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002127 goto recheck;
2128
Tejun Heoc8e55f32010-06-29 10:07:12 +02002129 /*
2130 * ->scheduled list can only be filled while a worker is
2131 * preparing to process a work or actually processing it.
2132 * Make sure nobody diddled with it while I was sleeping.
2133 */
Tejun Heo6183c002013-03-12 11:29:57 -07002134 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002135
Tejun Heoe22bee72010-06-29 10:07:14 +02002136 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002137 * Finish PREP stage. We're guaranteed to have at least one idle
2138 * worker or that someone else has already assumed the manager
2139 * role. This is where @worker starts participating in concurrency
2140 * management if applicable and concurrency management is restored
2141 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002142 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002143 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002144
2145 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002146 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002147 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002148 struct work_struct, entry);
2149
2150 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2151 /* optimization path, not strictly necessary */
2152 process_one_work(worker, work);
2153 if (unlikely(!list_empty(&worker->scheduled)))
2154 process_scheduled_works(worker);
2155 } else {
2156 move_linked_works(work, &worker->scheduled, NULL);
2157 process_scheduled_works(worker);
2158 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002159 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002160
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002161 worker_set_flags(worker, WORKER_PREP);
Tejun Heod313dd82010-07-02 10:03:51 +02002162sleep:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002163 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002164 * pool->lock is held and there's no work to process and no need to
2165 * manage, sleep. Workers are woken up only while holding
2166 * pool->lock or from local cpu, so setting the current state
2167 * before releasing pool->lock is enough to prevent losing any
2168 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002169 */
2170 worker_enter_idle(worker);
2171 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002172 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002173 schedule();
2174 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Tejun Heoe22bee72010-06-29 10:07:14 +02002177/**
2178 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002179 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002180 *
2181 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002182 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002183 *
Tejun Heo706026c2013-01-24 11:01:34 -08002184 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002185 * worker which uses GFP_KERNEL allocation which has slight chance of
2186 * developing into deadlock if some works currently on the same queue
2187 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2188 * the problem rescuer solves.
2189 *
Tejun Heo706026c2013-01-24 11:01:34 -08002190 * When such condition is possible, the pool summons rescuers of all
2191 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002192 * those works so that forward progress can be guaranteed.
2193 *
2194 * This should happen rarely.
Yacine Belkadid185af32013-07-31 14:59:24 -07002195 *
2196 * Return: 0
Tejun Heoe22bee72010-06-29 10:07:14 +02002197 */
Tejun Heo111c2252013-01-17 17:16:24 -08002198static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002199{
Tejun Heo111c2252013-01-17 17:16:24 -08002200 struct worker *rescuer = __rescuer;
2201 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002202 struct list_head *scheduled = &rescuer->scheduled;
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002203 bool should_stop;
Tejun Heoe22bee72010-06-29 10:07:14 +02002204
2205 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002206
2207 /*
2208 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2209 * doesn't participate in concurrency management.
2210 */
2211 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002212repeat:
2213 set_current_state(TASK_INTERRUPTIBLE);
2214
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002215 /*
2216 * By the time the rescuer is requested to stop, the workqueue
2217 * shouldn't have any work pending, but @wq->maydays may still have
2218 * pwq(s) queued. This can happen by non-rescuer workers consuming
2219 * all the work items before the rescuer got to them. Go through
2220 * @wq->maydays processing before acting on should_stop so that the
2221 * list is always empty on exit.
2222 */
2223 should_stop = kthread_should_stop();
Tejun Heoe22bee72010-06-29 10:07:14 +02002224
Tejun Heo493a1722013-03-12 11:29:59 -07002225 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002226 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002227
2228 while (!list_empty(&wq->maydays)) {
2229 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2230 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002231 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002232 struct work_struct *work, *n;
2233
2234 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002235 list_del_init(&pwq->mayday_node);
2236
Tejun Heo2e109a22013-03-13 19:47:40 -07002237 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002238
Lai Jiangshan51697d392014-05-20 17:46:36 +08002239 worker_attach_to_pool(rescuer, pool);
2240
2241 spin_lock_irq(&pool->lock);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002242 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002243
2244 /*
2245 * Slurp in all works issued via this workqueue and
2246 * process'em.
2247 */
Tejun Heo0479c8c2014-12-04 10:14:13 -05002248 WARN_ON_ONCE(!list_empty(scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002249 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002250 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002251 move_linked_works(work, scheduled, &n);
2252
NeilBrown008847f2014-12-08 12:39:16 -05002253 if (!list_empty(scheduled)) {
2254 process_scheduled_works(rescuer);
2255
2256 /*
2257 * The above execution of rescued work items could
2258 * have created more to rescue through
2259 * pwq_activate_first_delayed() or chained
2260 * queueing. Let's put @pwq back on mayday list so
2261 * that such back-to-back work items, which may be
2262 * being used to relieve memory pressure, don't
2263 * incur MAYDAY_INTERVAL delay inbetween.
2264 */
2265 if (need_to_create_worker(pool)) {
2266 spin_lock(&wq_mayday_lock);
2267 get_pwq(pwq);
2268 list_move_tail(&pwq->mayday_node, &wq->maydays);
2269 spin_unlock(&wq_mayday_lock);
2270 }
2271 }
Tejun Heo75769582011-02-14 14:04:46 +01002272
2273 /*
Lai Jiangshan77668c82014-04-18 11:04:16 -04002274 * Put the reference grabbed by send_mayday(). @pool won't
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002275 * go away while we're still attached to it.
Lai Jiangshan77668c82014-04-18 11:04:16 -04002276 */
2277 put_pwq(pwq);
2278
2279 /*
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002280 * Leave this pool. If need_more_worker() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002281 * regular worker; otherwise, we end up with 0 concurrency
2282 * and stalling the execution.
2283 */
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002284 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002285 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002286
Lai Jiangshanb3104102013-02-19 12:17:02 -08002287 rescuer->pool = NULL;
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002288 spin_unlock_irq(&pool->lock);
2289
2290 worker_detach_from_pool(rescuer, pool);
2291
2292 spin_lock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002293 }
2294
Tejun Heo2e109a22013-03-13 19:47:40 -07002295 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002296
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002297 if (should_stop) {
2298 __set_current_state(TASK_RUNNING);
2299 rescuer->task->flags &= ~PF_WQ_WORKER;
2300 return 0;
2301 }
2302
Tejun Heo111c2252013-01-17 17:16:24 -08002303 /* rescuers should never participate in concurrency management */
2304 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002305 schedule();
2306 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002309struct wq_barrier {
2310 struct work_struct work;
2311 struct completion done;
Tejun Heo2607d7a2015-03-09 09:22:28 -04002312 struct task_struct *task; /* purely informational */
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002313};
2314
2315static void wq_barrier_func(struct work_struct *work)
2316{
2317 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2318 complete(&barr->done);
2319}
2320
Tejun Heo4690c4a2010-06-29 10:07:10 +02002321/**
2322 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002323 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002324 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002325 * @target: target work to attach @barr to
2326 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002327 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002328 * @barr is linked to @target such that @barr is completed only after
2329 * @target finishes execution. Please note that the ordering
2330 * guarantee is observed only with respect to @target and on the local
2331 * cpu.
2332 *
2333 * Currently, a queued barrier can't be canceled. This is because
2334 * try_to_grab_pending() can't determine whether the work to be
2335 * grabbed is at the head of the queue and thus can't clear LINKED
2336 * flag of the previous work while there must be a valid next work
2337 * after a work with LINKED flag set.
2338 *
2339 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002340 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002341 *
2342 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002343 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002344 */
Tejun Heo112202d2013-02-13 19:29:12 -08002345static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002346 struct wq_barrier *barr,
2347 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002348{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002349 struct list_head *head;
2350 unsigned int linked = 0;
2351
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002352 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002353 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002354 * as we know for sure that this will not trigger any of the
2355 * checks and call back into the fixup functions where we
2356 * might deadlock.
2357 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002358 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002359 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002360 init_completion(&barr->done);
Tejun Heo2607d7a2015-03-09 09:22:28 -04002361 barr->task = current;
Oleg Nesterov83c22522007-05-09 02:33:54 -07002362
Tejun Heoaffee4b2010-06-29 10:07:12 +02002363 /*
2364 * If @target is currently being executed, schedule the
2365 * barrier to the worker; otherwise, put it after @target.
2366 */
2367 if (worker)
2368 head = worker->scheduled.next;
2369 else {
2370 unsigned long *bits = work_data_bits(target);
2371
2372 head = target->entry.next;
2373 /* there can already be other linked works, inherit and set */
2374 linked = *bits & WORK_STRUCT_LINKED;
2375 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2376 }
2377
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002378 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002379 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002380 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002381}
2382
Tejun Heo73f53c42010-06-29 10:07:11 +02002383/**
Tejun Heo112202d2013-02-13 19:29:12 -08002384 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002385 * @wq: workqueue being flushed
2386 * @flush_color: new flush color, < 0 for no-op
2387 * @work_color: new work color, < 0 for no-op
2388 *
Tejun Heo112202d2013-02-13 19:29:12 -08002389 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002390 *
Tejun Heo112202d2013-02-13 19:29:12 -08002391 * If @flush_color is non-negative, flush_color on all pwqs should be
2392 * -1. If no pwq has in-flight commands at the specified color, all
2393 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2394 * has in flight commands, its pwq->flush_color is set to
2395 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002396 * wakeup logic is armed and %true is returned.
2397 *
2398 * The caller should have initialized @wq->first_flusher prior to
2399 * calling this function with non-negative @flush_color. If
2400 * @flush_color is negative, no flush color update is done and %false
2401 * is returned.
2402 *
Tejun Heo112202d2013-02-13 19:29:12 -08002403 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002404 * work_color which is previous to @work_color and all will be
2405 * advanced to @work_color.
2406 *
2407 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002408 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002409 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002410 * Return:
Tejun Heo73f53c42010-06-29 10:07:11 +02002411 * %true if @flush_color >= 0 and there's something to flush. %false
2412 * otherwise.
2413 */
Tejun Heo112202d2013-02-13 19:29:12 -08002414static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002415 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Tejun Heo73f53c42010-06-29 10:07:11 +02002417 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002418 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002419
Tejun Heo73f53c42010-06-29 10:07:11 +02002420 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002421 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002422 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002423 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002424
Tejun Heo49e3cf42013-03-12 11:29:58 -07002425 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002426 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002427
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002428 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002429
2430 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002431 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002432
Tejun Heo112202d2013-02-13 19:29:12 -08002433 if (pwq->nr_in_flight[flush_color]) {
2434 pwq->flush_color = flush_color;
2435 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002436 wait = true;
2437 }
2438 }
2439
2440 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002441 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002442 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002443 }
2444
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002445 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002446 }
2447
Tejun Heo112202d2013-02-13 19:29:12 -08002448 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002449 complete(&wq->first_flusher->done);
2450
2451 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002454/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002456 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002458 * This function sleeps until all work items which were queued on entry
2459 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002461void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Tejun Heo73f53c42010-06-29 10:07:11 +02002463 struct wq_flusher this_flusher = {
2464 .list = LIST_HEAD_INIT(this_flusher.list),
2465 .flush_color = -1,
2466 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2467 };
2468 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002469
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002470 lock_map_acquire(&wq->lockdep_map);
2471 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002472
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002473 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002474
2475 /*
2476 * Start-to-wait phase
2477 */
2478 next_color = work_next_color(wq->work_color);
2479
2480 if (next_color != wq->flush_color) {
2481 /*
2482 * Color space is not full. The current work_color
2483 * becomes our flush_color and work_color is advanced
2484 * by one.
2485 */
Tejun Heo6183c002013-03-12 11:29:57 -07002486 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002487 this_flusher.flush_color = wq->work_color;
2488 wq->work_color = next_color;
2489
2490 if (!wq->first_flusher) {
2491 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002492 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002493
2494 wq->first_flusher = &this_flusher;
2495
Tejun Heo112202d2013-02-13 19:29:12 -08002496 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002497 wq->work_color)) {
2498 /* nothing to flush, done */
2499 wq->flush_color = next_color;
2500 wq->first_flusher = NULL;
2501 goto out_unlock;
2502 }
2503 } else {
2504 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002505 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002506 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002507 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 }
2509 } else {
2510 /*
2511 * Oops, color space is full, wait on overflow queue.
2512 * The next flush completion will assign us
2513 * flush_color and transfer to flusher_queue.
2514 */
2515 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2516 }
2517
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002518 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002519
2520 wait_for_completion(&this_flusher.done);
2521
2522 /*
2523 * Wake-up-and-cascade phase
2524 *
2525 * First flushers are responsible for cascading flushes and
2526 * handling overflow. Non-first flushers can simply return.
2527 */
2528 if (wq->first_flusher != &this_flusher)
2529 return;
2530
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002531 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002532
Tejun Heo4ce48b32010-07-02 10:03:51 +02002533 /* we might have raced, check again with mutex held */
2534 if (wq->first_flusher != &this_flusher)
2535 goto out_unlock;
2536
Tejun Heo73f53c42010-06-29 10:07:11 +02002537 wq->first_flusher = NULL;
2538
Tejun Heo6183c002013-03-12 11:29:57 -07002539 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2540 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002541
2542 while (true) {
2543 struct wq_flusher *next, *tmp;
2544
2545 /* complete all the flushers sharing the current flush color */
2546 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2547 if (next->flush_color != wq->flush_color)
2548 break;
2549 list_del_init(&next->list);
2550 complete(&next->done);
2551 }
2552
Tejun Heo6183c002013-03-12 11:29:57 -07002553 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2554 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002555
2556 /* this flush_color is finished, advance by one */
2557 wq->flush_color = work_next_color(wq->flush_color);
2558
2559 /* one color has been freed, handle overflow queue */
2560 if (!list_empty(&wq->flusher_overflow)) {
2561 /*
2562 * Assign the same color to all overflowed
2563 * flushers, advance work_color and append to
2564 * flusher_queue. This is the start-to-wait
2565 * phase for these overflowed flushers.
2566 */
2567 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2568 tmp->flush_color = wq->work_color;
2569
2570 wq->work_color = work_next_color(wq->work_color);
2571
2572 list_splice_tail_init(&wq->flusher_overflow,
2573 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002574 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002575 }
2576
2577 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002578 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002579 break;
2580 }
2581
2582 /*
2583 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002584 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002585 */
Tejun Heo6183c002013-03-12 11:29:57 -07002586 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2587 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002588
2589 list_del_init(&next->list);
2590 wq->first_flusher = next;
2591
Tejun Heo112202d2013-02-13 19:29:12 -08002592 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002593 break;
2594
2595 /*
2596 * Meh... this color is already done, clear first
2597 * flusher and repeat cascading.
2598 */
2599 wq->first_flusher = NULL;
2600 }
2601
2602out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002603 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
Dave Jonesae90dd52006-06-30 01:40:45 -04002605EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002607/**
2608 * drain_workqueue - drain a workqueue
2609 * @wq: workqueue to drain
2610 *
2611 * Wait until the workqueue becomes empty. While draining is in progress,
2612 * only chain queueing is allowed. IOW, only currently pending or running
2613 * work items on @wq can queue further work items on it. @wq is flushed
2614 * repeatedly until it becomes empty. The number of flushing is detemined
2615 * by the depth of chaining and should be relatively short. Whine if it
2616 * takes too long.
2617 */
2618void drain_workqueue(struct workqueue_struct *wq)
2619{
2620 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002621 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002622
2623 /*
2624 * __queue_work() needs to test whether there are drainers, is much
2625 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002626 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002627 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002628 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002629 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002630 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002631 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002632reflush:
2633 flush_workqueue(wq);
2634
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002635 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002636
Tejun Heo49e3cf42013-03-12 11:29:58 -07002637 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002638 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002639
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002640 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002641 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002642 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002643
2644 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002645 continue;
2646
2647 if (++flush_cnt == 10 ||
2648 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002649 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002650 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002651
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002652 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002653 goto reflush;
2654 }
2655
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002656 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002657 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002658 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002659}
2660EXPORT_SYMBOL_GPL(drain_workqueue);
2661
Tejun Heo606a5022012-08-20 14:51:23 -07002662static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002663{
2664 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002665 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002666 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002667
2668 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002669
Tejun Heofa1b54e2013-03-12 11:30:00 -07002670 local_irq_disable();
2671 pool = get_work_pool(work);
2672 if (!pool) {
2673 local_irq_enable();
2674 return false;
2675 }
2676
2677 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002678 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002679 pwq = get_work_pwq(work);
2680 if (pwq) {
2681 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002682 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002683 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002684 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002685 if (!worker)
2686 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002687 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002688 }
Tejun Heobaf59022010-09-16 10:42:16 +02002689
Tejun Heo112202d2013-02-13 19:29:12 -08002690 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002691 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002692
Tejun Heoe1594892011-01-09 23:32:15 +01002693 /*
2694 * If @max_active is 1 or rescuer is in use, flushing another work
2695 * item on the same workqueue may lead to deadlock. Make sure the
2696 * flusher is not running on the same workqueue by verifying write
2697 * access.
2698 */
Tejun Heo493008a2013-03-12 11:30:03 -07002699 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002700 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002701 else
Tejun Heo112202d2013-02-13 19:29:12 -08002702 lock_map_acquire_read(&pwq->wq->lockdep_map);
2703 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002704
Tejun Heobaf59022010-09-16 10:42:16 +02002705 return true;
2706already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002707 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002708 return false;
2709}
2710
Oleg Nesterovdb700892008-07-25 01:47:49 -07002711/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002712 * flush_work - wait for a work to finish executing the last queueing instance
2713 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002714 *
Tejun Heo606a5022012-08-20 14:51:23 -07002715 * Wait until @work has finished execution. @work is guaranteed to be idle
2716 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002717 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002718 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002719 * %true if flush_work() waited for the work to finish execution,
2720 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002721 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002722bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002723{
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002724 struct wq_barrier barr;
2725
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002726 lock_map_acquire(&work->lockdep_map);
2727 lock_map_release(&work->lockdep_map);
2728
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002729 if (start_flush_work(work, &barr)) {
2730 wait_for_completion(&barr.done);
2731 destroy_work_on_stack(&barr.work);
2732 return true;
2733 } else {
2734 return false;
2735 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002736}
2737EXPORT_SYMBOL_GPL(flush_work);
2738
Tejun Heo8603e1b32015-03-05 08:04:13 -05002739struct cwt_wait {
2740 wait_queue_t wait;
2741 struct work_struct *work;
2742};
2743
2744static int cwt_wakefn(wait_queue_t *wait, unsigned mode, int sync, void *key)
2745{
2746 struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait);
2747
2748 if (cwait->work != key)
2749 return 0;
2750 return autoremove_wake_function(wait, mode, sync, key);
2751}
2752
Tejun Heo36e227d2012-08-03 10:30:46 -07002753static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002754{
Tejun Heo8603e1b32015-03-05 08:04:13 -05002755 static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq);
Tejun Heobbb68df2012-08-03 10:30:46 -07002756 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002757 int ret;
2758
2759 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002760 ret = try_to_grab_pending(work, is_dwork, &flags);
2761 /*
Tejun Heo8603e1b32015-03-05 08:04:13 -05002762 * If someone else is already canceling, wait for it to
2763 * finish. flush_work() doesn't work for PREEMPT_NONE
2764 * because we may get scheduled between @work's completion
2765 * and the other canceling task resuming and clearing
2766 * CANCELING - flush_work() will return false immediately
2767 * as @work is no longer busy, try_to_grab_pending() will
2768 * return -ENOENT as @work is still being canceled and the
2769 * other canceling task won't be able to clear CANCELING as
2770 * we're hogging the CPU.
2771 *
2772 * Let's wait for completion using a waitqueue. As this
2773 * may lead to the thundering herd problem, use a custom
2774 * wake function which matches @work along with exclusive
2775 * wait and wakeup.
Tejun Heobbb68df2012-08-03 10:30:46 -07002776 */
Tejun Heo8603e1b32015-03-05 08:04:13 -05002777 if (unlikely(ret == -ENOENT)) {
2778 struct cwt_wait cwait;
2779
2780 init_wait(&cwait.wait);
2781 cwait.wait.func = cwt_wakefn;
2782 cwait.work = work;
2783
2784 prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait,
2785 TASK_UNINTERRUPTIBLE);
2786 if (work_is_canceling(work))
2787 schedule();
2788 finish_wait(&cancel_waitq, &cwait.wait);
2789 }
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002790 } while (unlikely(ret < 0));
2791
Tejun Heobbb68df2012-08-03 10:30:46 -07002792 /* tell other tasks trying to grab @work to back off */
2793 mark_work_canceling(work);
2794 local_irq_restore(flags);
2795
Tejun Heo606a5022012-08-20 14:51:23 -07002796 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002797 clear_work_data(work);
Tejun Heo8603e1b32015-03-05 08:04:13 -05002798
2799 /*
2800 * Paired with prepare_to_wait() above so that either
2801 * waitqueue_active() is visible here or !work_is_canceling() is
2802 * visible there.
2803 */
2804 smp_mb();
2805 if (waitqueue_active(&cancel_waitq))
2806 __wake_up(&cancel_waitq, TASK_NORMAL, 1, work);
2807
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002808 return ret;
2809}
2810
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002811/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002812 * cancel_work_sync - cancel a work and wait for it to finish
2813 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002814 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002815 * Cancel @work and wait for its execution to finish. This function
2816 * can be used even if the work re-queues itself or migrates to
2817 * another workqueue. On return from this function, @work is
2818 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002819 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002820 * cancel_work_sync(&delayed_work->work) must not be used for
2821 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002822 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002823 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002824 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002825 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002826 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002827 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002828 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002829bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002830{
Tejun Heo36e227d2012-08-03 10:30:46 -07002831 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002832}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002833EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002834
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2837 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002838 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002839 * Delayed timer is cancelled and the pending work is queued for
2840 * immediate execution. Like flush_work(), this function only
2841 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002842 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002843 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002844 * %true if flush_work() waited for the work to finish execution,
2845 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002846 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002847bool flush_delayed_work(struct delayed_work *dwork)
2848{
Tejun Heo8930cab2012-08-03 10:30:45 -07002849 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002850 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002851 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002852 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002853 return flush_work(&dwork->work);
2854}
2855EXPORT_SYMBOL(flush_delayed_work);
2856
2857/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002858 * cancel_delayed_work - cancel a delayed work
2859 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002860 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002861 * Kill off a pending delayed_work.
2862 *
2863 * Return: %true if @dwork was pending and canceled; %false if it wasn't
2864 * pending.
2865 *
2866 * Note:
2867 * The work callback function may still be running on return, unless
2868 * it returns %true and the work doesn't re-arm itself. Explicitly flush or
2869 * use cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002870 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002871 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002872 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002873bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002874{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002875 unsigned long flags;
2876 int ret;
2877
2878 do {
2879 ret = try_to_grab_pending(&dwork->work, true, &flags);
2880 } while (unlikely(ret == -EAGAIN));
2881
2882 if (unlikely(ret < 0))
2883 return false;
2884
Tejun Heo7c3eed52013-01-24 11:01:33 -08002885 set_work_pool_and_clear_pending(&dwork->work,
2886 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002887 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002888 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002889}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002890EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002891
2892/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002893 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2894 * @dwork: the delayed work cancel
2895 *
2896 * This is cancel_work_sync() for delayed works.
2897 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002898 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002899 * %true if @dwork was pending, %false otherwise.
2900 */
2901bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002902{
Tejun Heo36e227d2012-08-03 10:30:46 -07002903 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002904}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002905EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002907/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002908 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002909 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002910 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002911 * schedule_on_each_cpu() executes @func on each online CPU using the
2912 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002913 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002914 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002915 * Return:
Tejun Heo31ddd872010-10-19 11:14:49 +02002916 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002917 */
David Howells65f27f32006-11-22 14:55:48 +00002918int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002919{
2920 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002921 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002922
Andrew Mortonb6136772006-06-25 05:47:49 -07002923 works = alloc_percpu(struct work_struct);
2924 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002925 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002926
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002927 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002928
Christoph Lameter15316ba2006-01-08 01:00:43 -08002929 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002930 struct work_struct *work = per_cpu_ptr(works, cpu);
2931
2932 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002933 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002934 }
Tejun Heo93981802009-11-17 14:06:20 -08002935
2936 for_each_online_cpu(cpu)
2937 flush_work(per_cpu_ptr(works, cpu));
2938
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002939 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002940 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002941 return 0;
2942}
2943
Alan Sterneef6a7d2010-02-12 17:39:21 +09002944/**
2945 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2946 *
2947 * Forces execution of the kernel-global workqueue and blocks until its
2948 * completion.
2949 *
2950 * Think twice before calling this function! It's very easy to get into
2951 * trouble if you don't take great care. Either of the following situations
2952 * will lead to deadlock:
2953 *
2954 * One of the work items currently on the workqueue needs to acquire
2955 * a lock held by your code or its caller.
2956 *
2957 * Your code is running in the context of a work routine.
2958 *
2959 * They will be detected by lockdep when they occur, but the first might not
2960 * occur very often. It depends on what work items are on the workqueue and
2961 * what locks they need, which you have no control over.
2962 *
2963 * In most situations flushing the entire workqueue is overkill; you merely
2964 * need to know that a particular work item isn't queued and isn't running.
2965 * In such cases you should use cancel_delayed_work_sync() or
2966 * cancel_work_sync() instead.
2967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968void flush_scheduled_work(void)
2969{
Tejun Heod320c032010-06-29 10:07:14 +02002970 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
Dave Jonesae90dd52006-06-30 01:40:45 -04002972EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002975 * execute_in_process_context - reliably execute the routine with user context
2976 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002977 * @ew: guaranteed storage for the execute work structure (must
2978 * be available when the work executes)
2979 *
2980 * Executes the function immediately if process context is available,
2981 * otherwise schedules the function for delayed execution.
2982 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002983 * Return: 0 - function was executed
James Bottomley1fa44ec2006-02-23 12:43:43 -06002984 * 1 - function was scheduled for execution
2985 */
David Howells65f27f32006-11-22 14:55:48 +00002986int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002987{
2988 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002989 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002990 return 0;
2991 }
2992
David Howells65f27f32006-11-22 14:55:48 +00002993 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002994 schedule_work(&ew->work);
2995
2996 return 1;
2997}
2998EXPORT_SYMBOL_GPL(execute_in_process_context);
2999
Tejun Heo7a4e3442013-03-12 11:30:00 -07003000/**
3001 * free_workqueue_attrs - free a workqueue_attrs
3002 * @attrs: workqueue_attrs to free
3003 *
3004 * Undo alloc_workqueue_attrs().
3005 */
3006void free_workqueue_attrs(struct workqueue_attrs *attrs)
3007{
3008 if (attrs) {
3009 free_cpumask_var(attrs->cpumask);
3010 kfree(attrs);
3011 }
3012}
3013
3014/**
3015 * alloc_workqueue_attrs - allocate a workqueue_attrs
3016 * @gfp_mask: allocation mask to use
3017 *
3018 * Allocate a new workqueue_attrs, initialize with default settings and
Yacine Belkadid185af32013-07-31 14:59:24 -07003019 * return it.
3020 *
3021 * Return: The allocated new workqueue_attr on success. %NULL on failure.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003022 */
3023struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3024{
3025 struct workqueue_attrs *attrs;
3026
3027 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3028 if (!attrs)
3029 goto fail;
3030 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3031 goto fail;
3032
Tejun Heo13e2e552013-04-01 11:23:31 -07003033 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003034 return attrs;
3035fail:
3036 free_workqueue_attrs(attrs);
3037 return NULL;
3038}
3039
Tejun Heo29c91e92013-03-12 11:30:03 -07003040static void copy_workqueue_attrs(struct workqueue_attrs *to,
3041 const struct workqueue_attrs *from)
3042{
3043 to->nice = from->nice;
3044 cpumask_copy(to->cpumask, from->cpumask);
Shaohua Li2865a8f2013-08-01 09:56:36 +08003045 /*
3046 * Unlike hash and equality test, this function doesn't ignore
3047 * ->no_numa as it is used for both pool and wq attrs. Instead,
3048 * get_unbound_pool() explicitly clears ->no_numa after copying.
3049 */
3050 to->no_numa = from->no_numa;
Tejun Heo29c91e92013-03-12 11:30:03 -07003051}
3052
Tejun Heo29c91e92013-03-12 11:30:03 -07003053/* hash value of the content of @attr */
3054static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3055{
3056 u32 hash = 0;
3057
3058 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003059 hash = jhash(cpumask_bits(attrs->cpumask),
3060 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003061 return hash;
3062}
3063
3064/* content equality test */
3065static bool wqattrs_equal(const struct workqueue_attrs *a,
3066 const struct workqueue_attrs *b)
3067{
3068 if (a->nice != b->nice)
3069 return false;
3070 if (!cpumask_equal(a->cpumask, b->cpumask))
3071 return false;
3072 return true;
3073}
3074
Tejun Heo7a4e3442013-03-12 11:30:00 -07003075/**
3076 * init_worker_pool - initialize a newly zalloc'd worker_pool
3077 * @pool: worker_pool to initialize
3078 *
3079 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Yacine Belkadid185af32013-07-31 14:59:24 -07003080 *
3081 * Return: 0 on success, -errno on failure. Even on failure, all fields
Tejun Heo29c91e92013-03-12 11:30:03 -07003082 * inside @pool proper are initialized and put_unbound_pool() can be called
3083 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003084 */
3085static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003086{
3087 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003088 pool->id = -1;
3089 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003090 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003091 pool->flags |= POOL_DISASSOCIATED;
3092 INIT_LIST_HEAD(&pool->worklist);
3093 INIT_LIST_HEAD(&pool->idle_list);
3094 hash_init(pool->busy_hash);
3095
3096 init_timer_deferrable(&pool->idle_timer);
3097 pool->idle_timer.function = idle_worker_timeout;
3098 pool->idle_timer.data = (unsigned long)pool;
3099
3100 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3101 (unsigned long)pool);
3102
3103 mutex_init(&pool->manager_arb);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003104 mutex_init(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003105 INIT_LIST_HEAD(&pool->workers);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003106
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003107 ida_init(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003108 INIT_HLIST_NODE(&pool->hash_node);
3109 pool->refcnt = 1;
3110
3111 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003112 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3113 if (!pool->attrs)
3114 return -ENOMEM;
3115 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003116}
3117
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003118static void rcu_free_wq(struct rcu_head *rcu)
3119{
3120 struct workqueue_struct *wq =
3121 container_of(rcu, struct workqueue_struct, rcu);
3122
3123 if (!(wq->flags & WQ_UNBOUND))
3124 free_percpu(wq->cpu_pwqs);
3125 else
3126 free_workqueue_attrs(wq->unbound_attrs);
3127
3128 kfree(wq->rescuer);
3129 kfree(wq);
3130}
3131
Tejun Heo29c91e92013-03-12 11:30:03 -07003132static void rcu_free_pool(struct rcu_head *rcu)
3133{
3134 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3135
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003136 ida_destroy(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003137 free_workqueue_attrs(pool->attrs);
3138 kfree(pool);
3139}
3140
3141/**
3142 * put_unbound_pool - put a worker_pool
3143 * @pool: worker_pool to put
3144 *
3145 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003146 * safe manner. get_unbound_pool() calls this function on its failure path
3147 * and this function should be able to release pools which went through,
3148 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003149 *
3150 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003151 */
3152static void put_unbound_pool(struct worker_pool *pool)
3153{
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003154 DECLARE_COMPLETION_ONSTACK(detach_completion);
Tejun Heo29c91e92013-03-12 11:30:03 -07003155 struct worker *worker;
3156
Tejun Heoa892cac2013-04-01 11:23:32 -07003157 lockdep_assert_held(&wq_pool_mutex);
3158
3159 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003160 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003161
3162 /* sanity checks */
Lai Jiangshan61d0fbb2014-06-03 15:31:45 +08003163 if (WARN_ON(!(pool->cpu < 0)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003164 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003165 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003166
3167 /* release id and unhash */
3168 if (pool->id >= 0)
3169 idr_remove(&worker_pool_idr, pool->id);
3170 hash_del(&pool->hash_node);
3171
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003172 /*
3173 * Become the manager and destroy all workers. Grabbing
3174 * manager_arb prevents @pool's workers from blocking on
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003175 * attach_mutex.
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003176 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003177 mutex_lock(&pool->manager_arb);
Tejun Heo29c91e92013-03-12 11:30:03 -07003178
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003179 spin_lock_irq(&pool->lock);
Lai Jiangshan1037de32014-05-22 16:44:07 +08003180 while ((worker = first_idle_worker(pool)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003181 destroy_worker(worker);
3182 WARN_ON(pool->nr_workers || pool->nr_idle);
Tejun Heo29c91e92013-03-12 11:30:03 -07003183 spin_unlock_irq(&pool->lock);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003184
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003185 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003186 if (!list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003187 pool->detach_completion = &detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003188 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003189
3190 if (pool->detach_completion)
3191 wait_for_completion(pool->detach_completion);
3192
Tejun Heo29c91e92013-03-12 11:30:03 -07003193 mutex_unlock(&pool->manager_arb);
3194
3195 /* shut down the timers */
3196 del_timer_sync(&pool->idle_timer);
3197 del_timer_sync(&pool->mayday_timer);
3198
3199 /* sched-RCU protected to allow dereferences from get_work_pool() */
3200 call_rcu_sched(&pool->rcu, rcu_free_pool);
3201}
3202
3203/**
3204 * get_unbound_pool - get a worker_pool with the specified attributes
3205 * @attrs: the attributes of the worker_pool to get
3206 *
3207 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3208 * reference count and return it. If there already is a matching
3209 * worker_pool, it will be used; otherwise, this function attempts to
Yacine Belkadid185af32013-07-31 14:59:24 -07003210 * create a new one.
Tejun Heoa892cac2013-04-01 11:23:32 -07003211 *
3212 * Should be called with wq_pool_mutex held.
Yacine Belkadid185af32013-07-31 14:59:24 -07003213 *
3214 * Return: On success, a worker_pool with the same attributes as @attrs.
3215 * On failure, %NULL.
Tejun Heo29c91e92013-03-12 11:30:03 -07003216 */
3217static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3218{
Tejun Heo29c91e92013-03-12 11:30:03 -07003219 u32 hash = wqattrs_hash(attrs);
3220 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003221 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003222
Tejun Heoa892cac2013-04-01 11:23:32 -07003223 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003224
3225 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003226 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3227 if (wqattrs_equal(pool->attrs, attrs)) {
3228 pool->refcnt++;
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003229 return pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003230 }
3231 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003232
3233 /* nope, create a new one */
3234 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3235 if (!pool || init_worker_pool(pool) < 0)
3236 goto fail;
3237
Tejun Heo8864b4e2013-03-12 11:30:04 -07003238 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003239 copy_workqueue_attrs(pool->attrs, attrs);
3240
Shaohua Li2865a8f2013-08-01 09:56:36 +08003241 /*
3242 * no_numa isn't a worker_pool attribute, always clear it. See
3243 * 'struct workqueue_attrs' comments for detail.
3244 */
3245 pool->attrs->no_numa = false;
3246
Tejun Heof3f90ad2013-04-01 11:23:34 -07003247 /* if cpumask is contained inside a NUMA node, we belong to that node */
3248 if (wq_numa_enabled) {
3249 for_each_node(node) {
3250 if (cpumask_subset(pool->attrs->cpumask,
3251 wq_numa_possible_cpumask[node])) {
3252 pool->node = node;
3253 break;
3254 }
3255 }
3256 }
3257
Tejun Heo29c91e92013-03-12 11:30:03 -07003258 if (worker_pool_assign_id(pool) < 0)
3259 goto fail;
3260
3261 /* create and start the initial worker */
Lai Jiangshan051e1852014-07-22 13:03:02 +08003262 if (!create_worker(pool))
Tejun Heo29c91e92013-03-12 11:30:03 -07003263 goto fail;
3264
Tejun Heo29c91e92013-03-12 11:30:03 -07003265 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003266 hash_add(unbound_pool_hash, &pool->hash_node, hash);
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003267
Tejun Heo29c91e92013-03-12 11:30:03 -07003268 return pool;
3269fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003270 if (pool)
3271 put_unbound_pool(pool);
3272 return NULL;
3273}
3274
Tejun Heo8864b4e2013-03-12 11:30:04 -07003275static void rcu_free_pwq(struct rcu_head *rcu)
3276{
3277 kmem_cache_free(pwq_cache,
3278 container_of(rcu, struct pool_workqueue, rcu));
3279}
3280
3281/*
3282 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3283 * and needs to be destroyed.
3284 */
3285static void pwq_unbound_release_workfn(struct work_struct *work)
3286{
3287 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3288 unbound_release_work);
3289 struct workqueue_struct *wq = pwq->wq;
3290 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003291 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003292
3293 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3294 return;
3295
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003296 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003297 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003298 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003299 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003300
Tejun Heoa892cac2013-04-01 11:23:32 -07003301 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003302 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003303 mutex_unlock(&wq_pool_mutex);
3304
Tejun Heo8864b4e2013-03-12 11:30:04 -07003305 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3306
3307 /*
3308 * If we're the last pwq going away, @wq is already dead and no one
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003309 * is gonna access it anymore. Schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003310 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003311 if (is_last)
3312 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003313}
3314
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003315/**
Tejun Heo699ce092013-03-13 16:51:35 -07003316 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003317 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003318 *
Tejun Heo699ce092013-03-13 16:51:35 -07003319 * If @pwq isn't freezing, set @pwq->max_active to the associated
3320 * workqueue's saved_max_active and activate delayed work items
3321 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003322 */
Tejun Heo699ce092013-03-13 16:51:35 -07003323static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003324{
Tejun Heo699ce092013-03-13 16:51:35 -07003325 struct workqueue_struct *wq = pwq->wq;
3326 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003327
Tejun Heo699ce092013-03-13 16:51:35 -07003328 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003329 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003330
3331 /* fast exit for non-freezable wqs */
3332 if (!freezable && pwq->max_active == wq->saved_max_active)
3333 return;
3334
Lai Jiangshana357fc02013-03-25 16:57:19 -07003335 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003336
Lai Jiangshan74b414e2014-05-22 19:01:16 +08003337 /*
3338 * During [un]freezing, the caller is responsible for ensuring that
3339 * this function is called at least once after @workqueue_freezing
3340 * is updated and visible.
3341 */
3342 if (!freezable || !workqueue_freezing) {
Tejun Heo699ce092013-03-13 16:51:35 -07003343 pwq->max_active = wq->saved_max_active;
3344
3345 while (!list_empty(&pwq->delayed_works) &&
3346 pwq->nr_active < pwq->max_active)
3347 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003348
3349 /*
3350 * Need to kick a worker after thawed or an unbound wq's
3351 * max_active is bumped. It's a slow path. Do it always.
3352 */
3353 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003354 } else {
3355 pwq->max_active = 0;
3356 }
3357
Lai Jiangshana357fc02013-03-25 16:57:19 -07003358 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003359}
3360
Tejun Heoe50aba92013-04-01 11:23:35 -07003361/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003362static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3363 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003364{
3365 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3366
Tejun Heoe50aba92013-04-01 11:23:35 -07003367 memset(pwq, 0, sizeof(*pwq));
3368
Tejun Heod2c1d402013-03-12 11:30:04 -07003369 pwq->pool = pool;
3370 pwq->wq = wq;
3371 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003372 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003373 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003374 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003375 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003376 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003377}
Tejun Heod2c1d402013-03-12 11:30:04 -07003378
Tejun Heof147f292013-04-01 11:23:35 -07003379/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003380static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003381{
3382 struct workqueue_struct *wq = pwq->wq;
3383
3384 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003385
Tejun Heo1befcf32013-04-01 11:23:35 -07003386 /* may be called multiple times, ignore if already linked */
3387 if (!list_empty(&pwq->pwqs_node))
3388 return;
3389
Lai Jiangshan29b1cb42014-07-22 13:04:27 +08003390 /* set the matching work_color */
Tejun Heo75ccf592013-03-12 11:30:04 -07003391 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003392
3393 /* sync max_active to the current setting */
3394 pwq_adjust_max_active(pwq);
3395
3396 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003397 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003398}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003399
Tejun Heof147f292013-04-01 11:23:35 -07003400/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3401static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3402 const struct workqueue_attrs *attrs)
3403{
3404 struct worker_pool *pool;
3405 struct pool_workqueue *pwq;
3406
3407 lockdep_assert_held(&wq_pool_mutex);
3408
3409 pool = get_unbound_pool(attrs);
3410 if (!pool)
3411 return NULL;
3412
Tejun Heoe50aba92013-04-01 11:23:35 -07003413 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003414 if (!pwq) {
3415 put_unbound_pool(pool);
3416 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003417 }
Tejun Heo6029a912013-04-01 11:23:34 -07003418
Tejun Heof147f292013-04-01 11:23:35 -07003419 init_pwq(pwq, wq, pool);
3420 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003421}
3422
Tejun Heo4c16bd32013-04-01 11:23:36 -07003423/* undo alloc_unbound_pwq(), used only in the error path */
3424static void free_unbound_pwq(struct pool_workqueue *pwq)
3425{
3426 lockdep_assert_held(&wq_pool_mutex);
3427
3428 if (pwq) {
3429 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003430 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003431 }
3432}
3433
3434/**
3435 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3436 * @attrs: the wq_attrs of interest
3437 * @node: the target NUMA node
3438 * @cpu_going_down: if >= 0, the CPU to consider as offline
3439 * @cpumask: outarg, the resulting cpumask
3440 *
3441 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3442 * @cpu_going_down is >= 0, that cpu is considered offline during
Yacine Belkadid185af32013-07-31 14:59:24 -07003443 * calculation. The result is stored in @cpumask.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003444 *
3445 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3446 * enabled and @node has online CPUs requested by @attrs, the returned
3447 * cpumask is the intersection of the possible CPUs of @node and
3448 * @attrs->cpumask.
3449 *
3450 * The caller is responsible for ensuring that the cpumask of @node stays
3451 * stable.
Yacine Belkadid185af32013-07-31 14:59:24 -07003452 *
3453 * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
3454 * %false if equal.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003455 */
3456static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3457 int cpu_going_down, cpumask_t *cpumask)
3458{
Tejun Heod55262c2013-04-01 11:23:38 -07003459 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003460 goto use_dfl;
3461
3462 /* does @node have any online CPUs @attrs wants? */
3463 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3464 if (cpu_going_down >= 0)
3465 cpumask_clear_cpu(cpu_going_down, cpumask);
3466
3467 if (cpumask_empty(cpumask))
3468 goto use_dfl;
3469
3470 /* yeap, return possible CPUs in @node that @attrs wants */
3471 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3472 return !cpumask_equal(cpumask, attrs->cpumask);
3473
3474use_dfl:
3475 cpumask_copy(cpumask, attrs->cpumask);
3476 return false;
3477}
3478
Tejun Heo1befcf32013-04-01 11:23:35 -07003479/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3480static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3481 int node,
3482 struct pool_workqueue *pwq)
3483{
3484 struct pool_workqueue *old_pwq;
3485
3486 lockdep_assert_held(&wq->mutex);
3487
3488 /* link_pwq() can handle duplicate calls */
3489 link_pwq(pwq);
3490
3491 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3492 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3493 return old_pwq;
3494}
3495
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003496/**
3497 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3498 * @wq: the target workqueue
3499 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3500 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003501 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3502 * machines, this function maps a separate pwq to each NUMA node with
3503 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3504 * NUMA node it was issued on. Older pwqs are released as in-flight work
3505 * items finish. Note that a work item which repeatedly requeues itself
3506 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003507 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003508 * Performs GFP_KERNEL allocations.
3509 *
3510 * Return: 0 on success and -errno on failure.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003511 */
3512int apply_workqueue_attrs(struct workqueue_struct *wq,
3513 const struct workqueue_attrs *attrs)
3514{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003515 struct workqueue_attrs *new_attrs, *tmp_attrs;
3516 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003517 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003518
Tejun Heo8719dce2013-03-12 11:30:04 -07003519 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003520 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3521 return -EINVAL;
3522
Tejun Heo8719dce2013-03-12 11:30:04 -07003523 /* creating multiple pwqs breaks ordering guarantee */
3524 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3525 return -EINVAL;
3526
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08003527 pwq_tbl = kzalloc(nr_node_ids * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003528 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003529 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3530 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003531 goto enomem;
3532
Tejun Heo4c16bd32013-04-01 11:23:36 -07003533 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003534 copy_workqueue_attrs(new_attrs, attrs);
3535 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3536
Tejun Heo4c16bd32013-04-01 11:23:36 -07003537 /*
3538 * We may create multiple pwqs with differing cpumasks. Make a
3539 * copy of @new_attrs which will be modified and used to obtain
3540 * pools.
3541 */
3542 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003543
Tejun Heo4c16bd32013-04-01 11:23:36 -07003544 /*
3545 * CPUs should stay stable across pwq creations and installations.
3546 * Pin CPUs, determine the target cpumask for each node and create
3547 * pwqs accordingly.
3548 */
3549 get_online_cpus();
3550
3551 mutex_lock(&wq_pool_mutex);
3552
3553 /*
3554 * If something goes wrong during CPU up/down, we'll fall back to
3555 * the default pwq covering whole @attrs->cpumask. Always create
3556 * it even if we don't use it immediately.
3557 */
3558 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3559 if (!dfl_pwq)
3560 goto enomem_pwq;
3561
3562 for_each_node(node) {
3563 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3564 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3565 if (!pwq_tbl[node])
3566 goto enomem_pwq;
3567 } else {
3568 dfl_pwq->refcnt++;
3569 pwq_tbl[node] = dfl_pwq;
3570 }
3571 }
3572
3573 mutex_unlock(&wq_pool_mutex);
3574
3575 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003576 mutex_lock(&wq->mutex);
3577
Tejun Heof147f292013-04-01 11:23:35 -07003578 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003579
3580 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003581 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003582 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3583
3584 /* @dfl_pwq might not have been used, ensure it's linked */
3585 link_pwq(dfl_pwq);
3586 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003587
3588 mutex_unlock(&wq->mutex);
3589
Tejun Heo4c16bd32013-04-01 11:23:36 -07003590 /* put the old pwqs */
3591 for_each_node(node)
3592 put_pwq_unlocked(pwq_tbl[node]);
3593 put_pwq_unlocked(dfl_pwq);
3594
3595 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003596 ret = 0;
3597 /* fall through */
3598out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003599 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003600 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003601 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003602 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003603
Tejun Heo4c16bd32013-04-01 11:23:36 -07003604enomem_pwq:
3605 free_unbound_pwq(dfl_pwq);
3606 for_each_node(node)
3607 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3608 free_unbound_pwq(pwq_tbl[node]);
3609 mutex_unlock(&wq_pool_mutex);
3610 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003611enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003612 ret = -ENOMEM;
3613 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003614}
3615
Tejun Heo4c16bd32013-04-01 11:23:36 -07003616/**
3617 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3618 * @wq: the target workqueue
3619 * @cpu: the CPU coming up or going down
3620 * @online: whether @cpu is coming up or going down
3621 *
3622 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3623 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3624 * @wq accordingly.
3625 *
3626 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3627 * falls back to @wq->dfl_pwq which may not be optimal but is always
3628 * correct.
3629 *
3630 * Note that when the last allowed CPU of a NUMA node goes offline for a
3631 * workqueue with a cpumask spanning multiple nodes, the workers which were
3632 * already executing the work items for the workqueue will lose their CPU
3633 * affinity and may execute on any CPU. This is similar to how per-cpu
3634 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3635 * affinity, it's the user's responsibility to flush the work item from
3636 * CPU_DOWN_PREPARE.
3637 */
3638static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3639 bool online)
3640{
3641 int node = cpu_to_node(cpu);
3642 int cpu_off = online ? -1 : cpu;
3643 struct pool_workqueue *old_pwq = NULL, *pwq;
3644 struct workqueue_attrs *target_attrs;
3645 cpumask_t *cpumask;
3646
3647 lockdep_assert_held(&wq_pool_mutex);
3648
3649 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3650 return;
3651
3652 /*
3653 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3654 * Let's use a preallocated one. The following buf is protected by
3655 * CPU hotplug exclusion.
3656 */
3657 target_attrs = wq_update_unbound_numa_attrs_buf;
3658 cpumask = target_attrs->cpumask;
3659
3660 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07003661 if (wq->unbound_attrs->no_numa)
3662 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003663
3664 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3665 pwq = unbound_pwq_by_node(wq, node);
3666
3667 /*
3668 * Let's determine what needs to be done. If the target cpumask is
3669 * different from wq's, we need to compare it to @pwq's and create
3670 * a new one if they don't match. If the target cpumask equals
Daeseok Youn534a3fb2014-04-18 09:08:14 +09003671 * wq's, the default pwq should be used.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003672 */
3673 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3674 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
3675 goto out_unlock;
3676 } else {
Daeseok Youn534a3fb2014-04-18 09:08:14 +09003677 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003678 }
3679
3680 mutex_unlock(&wq->mutex);
3681
3682 /* create a new pwq */
3683 pwq = alloc_unbound_pwq(wq, target_attrs);
3684 if (!pwq) {
Fabian Frederick2d916032014-05-12 13:59:35 -04003685 pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
3686 wq->name);
Daeseok Youn77f300b2014-04-16 14:32:29 +09003687 mutex_lock(&wq->mutex);
3688 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003689 }
3690
3691 /*
3692 * Install the new pwq. As this function is called only from CPU
3693 * hotplug callbacks and applying a new attrs is wrapped with
3694 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
3695 * inbetween.
3696 */
3697 mutex_lock(&wq->mutex);
3698 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
3699 goto out_unlock;
3700
3701use_dfl_pwq:
3702 spin_lock_irq(&wq->dfl_pwq->pool->lock);
3703 get_pwq(wq->dfl_pwq);
3704 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
3705 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
3706out_unlock:
3707 mutex_unlock(&wq->mutex);
3708 put_pwq_unlocked(old_pwq);
3709}
3710
Tejun Heo30cdf242013-03-12 11:29:57 -07003711static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003713 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo8a2b7532013-09-05 12:30:04 -04003714 int cpu, ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003715
Tejun Heo30cdf242013-03-12 11:29:57 -07003716 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003717 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3718 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003719 return -ENOMEM;
3720
3721 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003722 struct pool_workqueue *pwq =
3723 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003724 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003725 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003726
Tejun Heof147f292013-04-01 11:23:35 -07003727 init_pwq(pwq, wq, &cpu_pools[highpri]);
3728
3729 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07003730 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003731 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07003732 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003733 return 0;
Tejun Heo8a2b7532013-09-05 12:30:04 -04003734 } else if (wq->flags & __WQ_ORDERED) {
3735 ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]);
3736 /* there should only be single pwq for ordering guarantee */
3737 WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node ||
3738 wq->pwqs.prev != &wq->dfl_pwq->pwqs_node),
3739 "ordering guarantee broken for workqueue %s\n", wq->name);
3740 return ret;
Tejun Heo30cdf242013-03-12 11:29:57 -07003741 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003742 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003743 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003744}
3745
Tejun Heof3421792010-07-02 10:03:51 +02003746static int wq_clamp_max_active(int max_active, unsigned int flags,
3747 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003748{
Tejun Heof3421792010-07-02 10:03:51 +02003749 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3750
3751 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003752 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3753 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003754
Tejun Heof3421792010-07-02 10:03:51 +02003755 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003756}
3757
Tejun Heob196be82012-01-10 15:11:35 -08003758struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003759 unsigned int flags,
3760 int max_active,
3761 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003762 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003763{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003764 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07003765 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003766 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003767 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003768
Viresh Kumarcee22a12013-04-08 16:45:40 +05303769 /* see the comment above the definition of WQ_POWER_EFFICIENT */
3770 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
3771 flags |= WQ_UNBOUND;
3772
Tejun Heoecf68812013-04-01 11:23:34 -07003773 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003774 if (flags & WQ_UNBOUND)
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08003775 tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003776
3777 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08003778 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003779 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003780
Tejun Heo6029a912013-04-01 11:23:34 -07003781 if (flags & WQ_UNBOUND) {
3782 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3783 if (!wq->unbound_attrs)
3784 goto err_free_wq;
3785 }
3786
Tejun Heoecf68812013-04-01 11:23:34 -07003787 va_start(args, lock_name);
3788 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08003789 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003790
Tejun Heod320c032010-06-29 10:07:14 +02003791 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003792 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003793
Tejun Heob196be82012-01-10 15:11:35 -08003794 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003795 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003796 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003797 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003798 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003799 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003800 INIT_LIST_HEAD(&wq->flusher_queue);
3801 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003802 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003803
Johannes Bergeb13ba82008-01-16 09:51:58 +01003804 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003805 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003806
Tejun Heo30cdf242013-03-12 11:29:57 -07003807 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003808 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003809
Tejun Heo493008a2013-03-12 11:30:03 -07003810 /*
3811 * Workqueues which may be used during memory reclaim should
3812 * have a rescuer to guarantee forward progress.
3813 */
3814 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003815 struct worker *rescuer;
3816
Lai Jiangshanf7537df2014-07-15 17:24:15 +08003817 rescuer = alloc_worker(NUMA_NO_NODE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003818 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003819 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003820
Tejun Heo111c2252013-01-17 17:16:24 -08003821 rescuer->rescue_wq = wq;
3822 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003823 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003824 if (IS_ERR(rescuer->task)) {
3825 kfree(rescuer);
3826 goto err_destroy;
3827 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003828
Tejun Heod2c1d402013-03-12 11:30:04 -07003829 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003830 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003831 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003832 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003833
Tejun Heo226223a2013-03-12 11:30:05 -07003834 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3835 goto err_destroy;
3836
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003838 * wq_pool_mutex protects global freeze state and workqueues list.
3839 * Grab it, adjust max_active and add the new @wq to workqueues
3840 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003841 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003842 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003843
Lai Jiangshana357fc02013-03-25 16:57:19 -07003844 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003845 for_each_pwq(pwq, wq)
3846 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003847 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003848
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003849 list_add_tail_rcu(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003851 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003852
Oleg Nesterov3af244332007-05-09 02:34:09 -07003853 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003854
3855err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07003856 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07003857 kfree(wq);
3858 return NULL;
3859err_destroy:
3860 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003861 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003862}
Tejun Heod320c032010-06-29 10:07:14 +02003863EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003864
3865/**
3866 * destroy_workqueue - safely terminate a workqueue
3867 * @wq: target workqueue
3868 *
3869 * Safely destroy a workqueue. All work currently pending will be done first.
3870 */
3871void destroy_workqueue(struct workqueue_struct *wq)
3872{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003873 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003874 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003875
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003876 /* drain it before proceeding with destruction */
3877 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003878
Tejun Heo6183c002013-03-12 11:29:57 -07003879 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003880 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003881 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003882 int i;
3883
Tejun Heo76af4d92013-03-12 11:30:00 -07003884 for (i = 0; i < WORK_NR_COLORS; i++) {
3885 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003886 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003887 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003888 }
3889 }
3890
Lai Jiangshan5c529592013-04-04 10:05:38 +08003891 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07003892 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003893 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003894 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003895 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003896 }
Tejun Heo6183c002013-03-12 11:29:57 -07003897 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003898 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003899
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003900 /*
3901 * wq list is used to freeze wq, remove from list after
3902 * flushing is complete in case freeze races us.
3903 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003904 mutex_lock(&wq_pool_mutex);
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003905 list_del_rcu(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003906 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003907
Tejun Heo226223a2013-03-12 11:30:05 -07003908 workqueue_sysfs_unregister(wq);
3909
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003910 if (wq->rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02003911 kthread_stop(wq->rescuer->task);
Tejun Heoe22bee72010-06-29 10:07:14 +02003912
Tejun Heo8864b4e2013-03-12 11:30:04 -07003913 if (!(wq->flags & WQ_UNBOUND)) {
3914 /*
3915 * The base ref is never dropped on per-cpu pwqs. Directly
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003916 * schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003917 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003918 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003919 } else {
3920 /*
3921 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07003922 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
3923 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003924 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07003925 for_each_node(node) {
3926 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3927 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
3928 put_pwq_unlocked(pwq);
3929 }
3930
3931 /*
3932 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
3933 * put. Don't access it afterwards.
3934 */
3935 pwq = wq->dfl_pwq;
3936 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07003937 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07003938 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003939}
3940EXPORT_SYMBOL_GPL(destroy_workqueue);
3941
Tejun Heodcd989c2010-06-29 10:07:14 +02003942/**
3943 * workqueue_set_max_active - adjust max_active of a workqueue
3944 * @wq: target workqueue
3945 * @max_active: new max_active value.
3946 *
3947 * Set max_active of @wq to @max_active.
3948 *
3949 * CONTEXT:
3950 * Don't call from IRQ context.
3951 */
3952void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3953{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003954 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003955
Tejun Heo8719dce2013-03-12 11:30:04 -07003956 /* disallow meddling with max_active for ordered workqueues */
3957 if (WARN_ON(wq->flags & __WQ_ORDERED))
3958 return;
3959
Tejun Heof3421792010-07-02 10:03:51 +02003960 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003961
Lai Jiangshana357fc02013-03-25 16:57:19 -07003962 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003963
3964 wq->saved_max_active = max_active;
3965
Tejun Heo699ce092013-03-13 16:51:35 -07003966 for_each_pwq(pwq, wq)
3967 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003968
Lai Jiangshana357fc02013-03-25 16:57:19 -07003969 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003970}
3971EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3972
3973/**
Tejun Heoe62676162013-03-12 17:41:37 -07003974 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3975 *
3976 * Determine whether %current is a workqueue rescuer. Can be used from
3977 * work functions to determine whether it's being run off the rescuer task.
Yacine Belkadid185af32013-07-31 14:59:24 -07003978 *
3979 * Return: %true if %current is a workqueue rescuer. %false otherwise.
Tejun Heoe62676162013-03-12 17:41:37 -07003980 */
3981bool current_is_workqueue_rescuer(void)
3982{
3983 struct worker *worker = current_wq_worker();
3984
Lai Jiangshan6a092df2013-03-20 03:28:03 +08003985 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07003986}
3987
3988/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003989 * workqueue_congested - test whether a workqueue is congested
3990 * @cpu: CPU in question
3991 * @wq: target workqueue
3992 *
3993 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3994 * no synchronization around this function and the test result is
3995 * unreliable and only useful as advisory hints or for debugging.
3996 *
Tejun Heod3251852013-05-10 11:10:17 -07003997 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
3998 * Note that both per-cpu and unbound workqueues may be associated with
3999 * multiple pool_workqueues which have separate congested states. A
4000 * workqueue being congested on one CPU doesn't mean the workqueue is also
4001 * contested on other CPUs / NUMA nodes.
4002 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004003 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004004 * %true if congested, %false otherwise.
4005 */
Tejun Heod84ff052013-03-12 11:29:59 -07004006bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004007{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004008 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004009 bool ret;
4010
Lai Jiangshan88109452013-03-20 03:28:10 +08004011 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004012
Tejun Heod3251852013-05-10 11:10:17 -07004013 if (cpu == WORK_CPU_UNBOUND)
4014 cpu = smp_processor_id();
4015
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004016 if (!(wq->flags & WQ_UNBOUND))
4017 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4018 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004019 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004020
Tejun Heo76af4d92013-03-12 11:30:00 -07004021 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004022 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004023
4024 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004025}
4026EXPORT_SYMBOL_GPL(workqueue_congested);
4027
4028/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004029 * work_busy - test whether a work is currently pending or running
4030 * @work: the work to be tested
4031 *
4032 * Test whether @work is currently pending or running. There is no
4033 * synchronization around this function and the test result is
4034 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004035 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004036 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004037 * OR'd bitmask of WORK_BUSY_* bits.
4038 */
4039unsigned int work_busy(struct work_struct *work)
4040{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004041 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004042 unsigned long flags;
4043 unsigned int ret = 0;
4044
Tejun Heodcd989c2010-06-29 10:07:14 +02004045 if (work_pending(work))
4046 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004047
Tejun Heofa1b54e2013-03-12 11:30:00 -07004048 local_irq_save(flags);
4049 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004050 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004051 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004052 if (find_worker_executing_work(pool, work))
4053 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004054 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004055 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004056 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004057
4058 return ret;
4059}
4060EXPORT_SYMBOL_GPL(work_busy);
4061
Tejun Heo3d1cb202013-04-30 15:27:22 -07004062/**
4063 * set_worker_desc - set description for the current work item
4064 * @fmt: printf-style format string
4065 * @...: arguments for the format string
4066 *
4067 * This function can be called by a running work function to describe what
4068 * the work item is about. If the worker task gets dumped, this
4069 * information will be printed out together to help debugging. The
4070 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4071 */
4072void set_worker_desc(const char *fmt, ...)
4073{
4074 struct worker *worker = current_wq_worker();
4075 va_list args;
4076
4077 if (worker) {
4078 va_start(args, fmt);
4079 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4080 va_end(args);
4081 worker->desc_valid = true;
4082 }
4083}
4084
4085/**
4086 * print_worker_info - print out worker information and description
4087 * @log_lvl: the log level to use when printing
4088 * @task: target task
4089 *
4090 * If @task is a worker and currently executing a work item, print out the
4091 * name of the workqueue being serviced and worker description set with
4092 * set_worker_desc() by the currently executing work item.
4093 *
4094 * This function can be safely called on any task as long as the
4095 * task_struct itself is accessible. While safe, this function isn't
4096 * synchronized and may print out mixups or garbages of limited length.
4097 */
4098void print_worker_info(const char *log_lvl, struct task_struct *task)
4099{
4100 work_func_t *fn = NULL;
4101 char name[WQ_NAME_LEN] = { };
4102 char desc[WORKER_DESC_LEN] = { };
4103 struct pool_workqueue *pwq = NULL;
4104 struct workqueue_struct *wq = NULL;
4105 bool desc_valid = false;
4106 struct worker *worker;
4107
4108 if (!(task->flags & PF_WQ_WORKER))
4109 return;
4110
4111 /*
4112 * This function is called without any synchronization and @task
4113 * could be in any state. Be careful with dereferences.
4114 */
4115 worker = probe_kthread_data(task);
4116
4117 /*
4118 * Carefully copy the associated workqueue's workfn and name. Keep
4119 * the original last '\0' in case the original contains garbage.
4120 */
4121 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4122 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4123 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4124 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4125
4126 /* copy worker description */
4127 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4128 if (desc_valid)
4129 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4130
4131 if (fn || name[0] || desc[0]) {
4132 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4133 if (desc[0])
4134 pr_cont(" (%s)", desc);
4135 pr_cont("\n");
4136 }
4137}
4138
Tejun Heo3494fc32015-03-09 09:22:28 -04004139static void pr_cont_pool_info(struct worker_pool *pool)
4140{
4141 pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);
4142 if (pool->node != NUMA_NO_NODE)
4143 pr_cont(" node=%d", pool->node);
4144 pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice);
4145}
4146
4147static void pr_cont_work(bool comma, struct work_struct *work)
4148{
4149 if (work->func == wq_barrier_func) {
4150 struct wq_barrier *barr;
4151
4152 barr = container_of(work, struct wq_barrier, work);
4153
4154 pr_cont("%s BAR(%d)", comma ? "," : "",
4155 task_pid_nr(barr->task));
4156 } else {
4157 pr_cont("%s %pf", comma ? "," : "", work->func);
4158 }
4159}
4160
4161static void show_pwq(struct pool_workqueue *pwq)
4162{
4163 struct worker_pool *pool = pwq->pool;
4164 struct work_struct *work;
4165 struct worker *worker;
4166 bool has_in_flight = false, has_pending = false;
4167 int bkt;
4168
4169 pr_info(" pwq %d:", pool->id);
4170 pr_cont_pool_info(pool);
4171
4172 pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active,
4173 !list_empty(&pwq->mayday_node) ? " MAYDAY" : "");
4174
4175 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4176 if (worker->current_pwq == pwq) {
4177 has_in_flight = true;
4178 break;
4179 }
4180 }
4181 if (has_in_flight) {
4182 bool comma = false;
4183
4184 pr_info(" in-flight:");
4185 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4186 if (worker->current_pwq != pwq)
4187 continue;
4188
4189 pr_cont("%s %d%s:%pf", comma ? "," : "",
4190 task_pid_nr(worker->task),
4191 worker == pwq->wq->rescuer ? "(RESCUER)" : "",
4192 worker->current_func);
4193 list_for_each_entry(work, &worker->scheduled, entry)
4194 pr_cont_work(false, work);
4195 comma = true;
4196 }
4197 pr_cont("\n");
4198 }
4199
4200 list_for_each_entry(work, &pool->worklist, entry) {
4201 if (get_work_pwq(work) == pwq) {
4202 has_pending = true;
4203 break;
4204 }
4205 }
4206 if (has_pending) {
4207 bool comma = false;
4208
4209 pr_info(" pending:");
4210 list_for_each_entry(work, &pool->worklist, entry) {
4211 if (get_work_pwq(work) != pwq)
4212 continue;
4213
4214 pr_cont_work(comma, work);
4215 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4216 }
4217 pr_cont("\n");
4218 }
4219
4220 if (!list_empty(&pwq->delayed_works)) {
4221 bool comma = false;
4222
4223 pr_info(" delayed:");
4224 list_for_each_entry(work, &pwq->delayed_works, entry) {
4225 pr_cont_work(comma, work);
4226 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4227 }
4228 pr_cont("\n");
4229 }
4230}
4231
4232/**
4233 * show_workqueue_state - dump workqueue state
4234 *
4235 * Called from a sysrq handler and prints out all busy workqueues and
4236 * pools.
4237 */
4238void show_workqueue_state(void)
4239{
4240 struct workqueue_struct *wq;
4241 struct worker_pool *pool;
4242 unsigned long flags;
4243 int pi;
4244
4245 rcu_read_lock_sched();
4246
4247 pr_info("Showing busy workqueues and worker pools:\n");
4248
4249 list_for_each_entry_rcu(wq, &workqueues, list) {
4250 struct pool_workqueue *pwq;
4251 bool idle = true;
4252
4253 for_each_pwq(pwq, wq) {
4254 if (pwq->nr_active || !list_empty(&pwq->delayed_works)) {
4255 idle = false;
4256 break;
4257 }
4258 }
4259 if (idle)
4260 continue;
4261
4262 pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags);
4263
4264 for_each_pwq(pwq, wq) {
4265 spin_lock_irqsave(&pwq->pool->lock, flags);
4266 if (pwq->nr_active || !list_empty(&pwq->delayed_works))
4267 show_pwq(pwq);
4268 spin_unlock_irqrestore(&pwq->pool->lock, flags);
4269 }
4270 }
4271
4272 for_each_pool(pool, pi) {
4273 struct worker *worker;
4274 bool first = true;
4275
4276 spin_lock_irqsave(&pool->lock, flags);
4277 if (pool->nr_workers == pool->nr_idle)
4278 goto next_pool;
4279
4280 pr_info("pool %d:", pool->id);
4281 pr_cont_pool_info(pool);
4282 pr_cont(" workers=%d", pool->nr_workers);
4283 if (pool->manager)
4284 pr_cont(" manager: %d",
4285 task_pid_nr(pool->manager->task));
4286 list_for_each_entry(worker, &pool->idle_list, entry) {
4287 pr_cont(" %s%d", first ? "idle: " : "",
4288 task_pid_nr(worker->task));
4289 first = false;
4290 }
4291 pr_cont("\n");
4292 next_pool:
4293 spin_unlock_irqrestore(&pool->lock, flags);
4294 }
4295
4296 rcu_read_unlock_sched();
4297}
4298
Tejun Heodb7bccf2010-06-29 10:07:12 +02004299/*
4300 * CPU hotplug.
4301 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004302 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004303 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004304 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004305 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004306 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004307 * blocked draining impractical.
4308 *
Tejun Heo24647572013-01-24 11:01:33 -08004309 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004310 * running as an unbound one and allowing it to be reattached later if the
4311 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004312 */
4313
Tejun Heo706026c2013-01-24 11:01:34 -08004314static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004315{
Tejun Heo38db41d2013-01-24 11:01:34 -08004316 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004317 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004318 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004319
Tejun Heof02ae732013-03-12 11:30:03 -07004320 for_each_cpu_worker_pool(pool, cpu) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004321 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004322 spin_lock_irq(&pool->lock);
4323
4324 /*
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004325 * We've blocked all attach/detach operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004326 * unbound and set DISASSOCIATED. Before this, all workers
4327 * except for the ones which are still executing works from
4328 * before the last CPU down must be on the cpu. After
4329 * this, they may become diasporas.
4330 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004331 for_each_pool_worker(worker, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004332 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004333
Tejun Heo24647572013-01-24 11:01:33 -08004334 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004335
Tejun Heo94cf58b2013-01-24 11:01:33 -08004336 spin_unlock_irq(&pool->lock);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004337 mutex_unlock(&pool->attach_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004338
Lai Jiangshaneb283422013-03-08 15:18:28 -08004339 /*
4340 * Call schedule() so that we cross rq->lock and thus can
4341 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4342 * This is necessary as scheduler callbacks may be invoked
4343 * from other cpus.
4344 */
4345 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004346
Lai Jiangshaneb283422013-03-08 15:18:28 -08004347 /*
4348 * Sched callbacks are disabled now. Zap nr_running.
4349 * After this, nr_running stays zero and need_more_worker()
4350 * and keep_working() are always true as long as the
4351 * worklist is not empty. This pool now behaves as an
4352 * unbound (in terms of concurrency management) pool which
4353 * are served by workers tied to the pool.
4354 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004355 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004356
4357 /*
4358 * With concurrency management just turned off, a busy
4359 * worker blocking could lead to lengthy stalls. Kick off
4360 * unbound chain execution of currently pending work items.
4361 */
4362 spin_lock_irq(&pool->lock);
4363 wake_up_worker(pool);
4364 spin_unlock_irq(&pool->lock);
4365 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004366}
4367
Tejun Heobd7c0892013-03-19 13:45:21 -07004368/**
4369 * rebind_workers - rebind all workers of a pool to the associated CPU
4370 * @pool: pool of interest
4371 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004372 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004373 */
4374static void rebind_workers(struct worker_pool *pool)
4375{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004376 struct worker *worker;
Tejun Heobd7c0892013-03-19 13:45:21 -07004377
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004378 lockdep_assert_held(&pool->attach_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004379
Tejun Heoa9ab7752013-03-19 13:45:21 -07004380 /*
4381 * Restore CPU affinity of all workers. As all idle workers should
4382 * be on the run-queue of the associated CPU before any local
4383 * wake-ups for concurrency management happen, restore CPU affinty
4384 * of all workers first and then clear UNBOUND. As we're called
4385 * from CPU_ONLINE, the following shouldn't fail.
4386 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004387 for_each_pool_worker(worker, pool)
Tejun Heoa9ab7752013-03-19 13:45:21 -07004388 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4389 pool->attrs->cpumask) < 0);
4390
4391 spin_lock_irq(&pool->lock);
Lai Jiangshan3de5e882014-06-03 15:33:27 +08004392 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004393
Lai Jiangshanda028462014-05-20 17:46:31 +08004394 for_each_pool_worker(worker, pool) {
Tejun Heoa9ab7752013-03-19 13:45:21 -07004395 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004396
4397 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004398 * A bound idle worker should actually be on the runqueue
4399 * of the associated CPU for local wake-ups targeting it to
4400 * work. Kick all idle workers so that they migrate to the
4401 * associated CPU. Doing this in the same loop as
4402 * replacing UNBOUND with REBOUND is safe as no worker will
4403 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004404 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004405 if (worker_flags & WORKER_IDLE)
4406 wake_up_process(worker->task);
4407
4408 /*
4409 * We want to clear UNBOUND but can't directly call
4410 * worker_clr_flags() or adjust nr_running. Atomically
4411 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4412 * @worker will clear REBOUND using worker_clr_flags() when
4413 * it initiates the next execution cycle thus restoring
4414 * concurrency management. Note that when or whether
4415 * @worker clears REBOUND doesn't affect correctness.
4416 *
4417 * ACCESS_ONCE() is necessary because @worker->flags may be
4418 * tested without holding any lock in
4419 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4420 * fail incorrectly leading to premature concurrency
4421 * management operations.
4422 */
4423 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4424 worker_flags |= WORKER_REBOUND;
4425 worker_flags &= ~WORKER_UNBOUND;
4426 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004427 }
4428
Tejun Heoa9ab7752013-03-19 13:45:21 -07004429 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004430}
4431
Tejun Heo7dbc7252013-03-19 13:45:21 -07004432/**
4433 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4434 * @pool: unbound pool of interest
4435 * @cpu: the CPU which is coming up
4436 *
4437 * An unbound pool may end up with a cpumask which doesn't have any online
4438 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4439 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4440 * online CPU before, cpus_allowed of all its workers should be restored.
4441 */
4442static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4443{
4444 static cpumask_t cpumask;
4445 struct worker *worker;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004446
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004447 lockdep_assert_held(&pool->attach_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004448
4449 /* is @cpu allowed for @pool? */
4450 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4451 return;
4452
4453 /* is @cpu the only online CPU? */
4454 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4455 if (cpumask_weight(&cpumask) != 1)
4456 return;
4457
4458 /* as we're called from CPU_ONLINE, the following shouldn't fail */
Lai Jiangshanda028462014-05-20 17:46:31 +08004459 for_each_pool_worker(worker, pool)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004460 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4461 pool->attrs->cpumask) < 0);
4462}
4463
Tejun Heo8db25e72012-07-17 12:39:28 -07004464/*
4465 * Workqueues should be brought up before normal priority CPU notifiers.
4466 * This will be registered high priority CPU notifier.
4467 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004468static int workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004469 unsigned long action,
4470 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004471{
Tejun Heod84ff052013-03-12 11:29:59 -07004472 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004473 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004474 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004475 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Tejun Heo8db25e72012-07-17 12:39:28 -07004477 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004479 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004480 if (pool->nr_workers)
4481 continue;
Lai Jiangshan051e1852014-07-22 13:03:02 +08004482 if (!create_worker(pool))
Tejun Heo3ce63372012-07-17 12:39:27 -07004483 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004485 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004486
Tejun Heo65758202012-07-17 12:39:26 -07004487 case CPU_DOWN_FAILED:
4488 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004489 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004490
4491 for_each_pool(pool, pi) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004492 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004493
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004494 if (pool->cpu == cpu)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004495 rebind_workers(pool);
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004496 else if (pool->cpu < 0)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004497 restore_unbound_workers_cpumask(pool, cpu);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004498
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004499 mutex_unlock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004500 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004501
Tejun Heo4c16bd32013-04-01 11:23:36 -07004502 /* update NUMA affinity of unbound workqueues */
4503 list_for_each_entry(wq, &workqueues, list)
4504 wq_update_unbound_numa(wq, cpu, true);
4505
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004506 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004507 break;
Tejun Heo65758202012-07-17 12:39:26 -07004508 }
4509 return NOTIFY_OK;
4510}
4511
4512/*
4513 * Workqueues should be brought down after normal priority CPU notifiers.
4514 * This will be registered as low priority CPU notifier.
4515 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004516static int workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004517 unsigned long action,
4518 void *hcpu)
4519{
Tejun Heod84ff052013-03-12 11:29:59 -07004520 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004521 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004522 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004523
Tejun Heo65758202012-07-17 12:39:26 -07004524 switch (action & ~CPU_TASKS_FROZEN) {
4525 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004526 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004527 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004528 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004529
4530 /* update NUMA affinity of unbound workqueues */
4531 mutex_lock(&wq_pool_mutex);
4532 list_for_each_entry(wq, &workqueues, list)
4533 wq_update_unbound_numa(wq, cpu, false);
4534 mutex_unlock(&wq_pool_mutex);
4535
4536 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004537 flush_work(&unbind_work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004538 destroy_work_on_stack(&unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004539 break;
Tejun Heo65758202012-07-17 12:39:26 -07004540 }
4541 return NOTIFY_OK;
4542}
4543
Rusty Russell2d3854a2008-11-05 13:39:10 +11004544#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004545
Rusty Russell2d3854a2008-11-05 13:39:10 +11004546struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004547 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004548 long (*fn)(void *);
4549 void *arg;
4550 long ret;
4551};
4552
Tejun Heoed48ece2012-09-18 12:48:43 -07004553static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004554{
Tejun Heoed48ece2012-09-18 12:48:43 -07004555 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4556
Rusty Russell2d3854a2008-11-05 13:39:10 +11004557 wfc->ret = wfc->fn(wfc->arg);
4558}
4559
4560/**
4561 * work_on_cpu - run a function in user context on a particular cpu
4562 * @cpu: the cpu to run on
4563 * @fn: the function to run
4564 * @arg: the function arg
4565 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004566 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004567 * The caller must not hold any locks which would prevent @fn from completing.
Yacine Belkadid185af32013-07-31 14:59:24 -07004568 *
4569 * Return: The value @fn returns.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004570 */
Tejun Heod84ff052013-03-12 11:29:59 -07004571long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004572{
Tejun Heoed48ece2012-09-18 12:48:43 -07004573 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004574
Tejun Heoed48ece2012-09-18 12:48:43 -07004575 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4576 schedule_work_on(cpu, &wfc.work);
Bjorn Helgaas12997d12013-11-18 11:00:29 -07004577 flush_work(&wfc.work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004578 destroy_work_on_stack(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004579 return wfc.ret;
4580}
4581EXPORT_SYMBOL_GPL(work_on_cpu);
4582#endif /* CONFIG_SMP */
4583
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004584#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304585
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004586/**
4587 * freeze_workqueues_begin - begin freezing workqueues
4588 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004589 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004590 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004591 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004592 *
4593 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004594 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004595 */
4596void freeze_workqueues_begin(void)
4597{
Tejun Heo24b8a842013-03-12 11:29:58 -07004598 struct workqueue_struct *wq;
4599 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004600
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004601 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004602
Tejun Heo6183c002013-03-12 11:29:57 -07004603 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004604 workqueue_freezing = true;
4605
Tejun Heo24b8a842013-03-12 11:29:58 -07004606 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004607 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004608 for_each_pwq(pwq, wq)
4609 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004610 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004611 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004612
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004613 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004615
4616/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004617 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004618 *
4619 * Check whether freezing is complete. This function must be called
4620 * between freeze_workqueues_begin() and thaw_workqueues().
4621 *
4622 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004623 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004624 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004625 * Return:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004626 * %true if some freezable workqueues are still busy. %false if freezing
4627 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004628 */
4629bool freeze_workqueues_busy(void)
4630{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004631 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004632 struct workqueue_struct *wq;
4633 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004634
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004635 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004636
Tejun Heo6183c002013-03-12 11:29:57 -07004637 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004638
Tejun Heo24b8a842013-03-12 11:29:58 -07004639 list_for_each_entry(wq, &workqueues, list) {
4640 if (!(wq->flags & WQ_FREEZABLE))
4641 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004642 /*
4643 * nr_active is monotonically decreasing. It's safe
4644 * to peek without lock.
4645 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004646 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004647 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004648 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004649 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004650 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004651 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004652 goto out_unlock;
4653 }
4654 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004655 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004656 }
4657out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004658 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004659 return busy;
4660}
4661
4662/**
4663 * thaw_workqueues - thaw workqueues
4664 *
4665 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004666 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004667 *
4668 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004669 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004670 */
4671void thaw_workqueues(void)
4672{
Tejun Heo24b8a842013-03-12 11:29:58 -07004673 struct workqueue_struct *wq;
4674 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004675
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004676 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004677
4678 if (!workqueue_freezing)
4679 goto out_unlock;
4680
Lai Jiangshan74b414e2014-05-22 19:01:16 +08004681 workqueue_freezing = false;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004682
Tejun Heo24b8a842013-03-12 11:29:58 -07004683 /* restore max_active and repopulate worklist */
4684 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004685 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004686 for_each_pwq(pwq, wq)
4687 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004688 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004689 }
4690
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004691out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004692 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004693}
4694#endif /* CONFIG_FREEZER */
4695
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004696#ifdef CONFIG_SYSFS
4697/*
4698 * Workqueues with WQ_SYSFS flag set is visible to userland via
4699 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
4700 * following attributes.
4701 *
4702 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
4703 * max_active RW int : maximum number of in-flight work items
4704 *
4705 * Unbound workqueues have the following extra attributes.
4706 *
4707 * id RO int : the associated pool ID
4708 * nice RW int : nice value of the workers
4709 * cpumask RW mask : bitmask of allowed CPUs for the workers
4710 */
4711struct wq_device {
4712 struct workqueue_struct *wq;
4713 struct device dev;
4714};
4715
4716static struct workqueue_struct *dev_to_wq(struct device *dev)
4717{
4718 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
4719
4720 return wq_dev->wq;
4721}
4722
4723static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
4724 char *buf)
4725{
4726 struct workqueue_struct *wq = dev_to_wq(dev);
4727
4728 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
4729}
4730static DEVICE_ATTR_RO(per_cpu);
4731
4732static ssize_t max_active_show(struct device *dev,
4733 struct device_attribute *attr, char *buf)
4734{
4735 struct workqueue_struct *wq = dev_to_wq(dev);
4736
4737 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
4738}
4739
4740static ssize_t max_active_store(struct device *dev,
4741 struct device_attribute *attr, const char *buf,
4742 size_t count)
4743{
4744 struct workqueue_struct *wq = dev_to_wq(dev);
4745 int val;
4746
4747 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
4748 return -EINVAL;
4749
4750 workqueue_set_max_active(wq, val);
4751 return count;
4752}
4753static DEVICE_ATTR_RW(max_active);
4754
4755static struct attribute *wq_sysfs_attrs[] = {
4756 &dev_attr_per_cpu.attr,
4757 &dev_attr_max_active.attr,
4758 NULL,
4759};
4760ATTRIBUTE_GROUPS(wq_sysfs);
4761
4762static ssize_t wq_pool_ids_show(struct device *dev,
4763 struct device_attribute *attr, char *buf)
4764{
4765 struct workqueue_struct *wq = dev_to_wq(dev);
4766 const char *delim = "";
4767 int node, written = 0;
4768
4769 rcu_read_lock_sched();
4770 for_each_node(node) {
4771 written += scnprintf(buf + written, PAGE_SIZE - written,
4772 "%s%d:%d", delim, node,
4773 unbound_pwq_by_node(wq, node)->pool->id);
4774 delim = " ";
4775 }
4776 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
4777 rcu_read_unlock_sched();
4778
4779 return written;
4780}
4781
4782static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
4783 char *buf)
4784{
4785 struct workqueue_struct *wq = dev_to_wq(dev);
4786 int written;
4787
4788 mutex_lock(&wq->mutex);
4789 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
4790 mutex_unlock(&wq->mutex);
4791
4792 return written;
4793}
4794
4795/* prepare workqueue_attrs for sysfs store operations */
4796static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
4797{
4798 struct workqueue_attrs *attrs;
4799
4800 attrs = alloc_workqueue_attrs(GFP_KERNEL);
4801 if (!attrs)
4802 return NULL;
4803
4804 mutex_lock(&wq->mutex);
4805 copy_workqueue_attrs(attrs, wq->unbound_attrs);
4806 mutex_unlock(&wq->mutex);
4807 return attrs;
4808}
4809
4810static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
4811 const char *buf, size_t count)
4812{
4813 struct workqueue_struct *wq = dev_to_wq(dev);
4814 struct workqueue_attrs *attrs;
4815 int ret;
4816
4817 attrs = wq_sysfs_prep_attrs(wq);
4818 if (!attrs)
4819 return -ENOMEM;
4820
4821 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
4822 attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
4823 ret = apply_workqueue_attrs(wq, attrs);
4824 else
4825 ret = -EINVAL;
4826
4827 free_workqueue_attrs(attrs);
4828 return ret ?: count;
4829}
4830
4831static ssize_t wq_cpumask_show(struct device *dev,
4832 struct device_attribute *attr, char *buf)
4833{
4834 struct workqueue_struct *wq = dev_to_wq(dev);
4835 int written;
4836
4837 mutex_lock(&wq->mutex);
4838 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
4839 cpumask_pr_args(wq->unbound_attrs->cpumask));
4840 mutex_unlock(&wq->mutex);
4841 return written;
4842}
4843
4844static ssize_t wq_cpumask_store(struct device *dev,
4845 struct device_attribute *attr,
4846 const char *buf, size_t count)
4847{
4848 struct workqueue_struct *wq = dev_to_wq(dev);
4849 struct workqueue_attrs *attrs;
4850 int ret;
4851
4852 attrs = wq_sysfs_prep_attrs(wq);
4853 if (!attrs)
4854 return -ENOMEM;
4855
4856 ret = cpumask_parse(buf, attrs->cpumask);
4857 if (!ret)
4858 ret = apply_workqueue_attrs(wq, attrs);
4859
4860 free_workqueue_attrs(attrs);
4861 return ret ?: count;
4862}
4863
4864static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
4865 char *buf)
4866{
4867 struct workqueue_struct *wq = dev_to_wq(dev);
4868 int written;
4869
4870 mutex_lock(&wq->mutex);
4871 written = scnprintf(buf, PAGE_SIZE, "%d\n",
4872 !wq->unbound_attrs->no_numa);
4873 mutex_unlock(&wq->mutex);
4874
4875 return written;
4876}
4877
4878static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
4879 const char *buf, size_t count)
4880{
4881 struct workqueue_struct *wq = dev_to_wq(dev);
4882 struct workqueue_attrs *attrs;
4883 int v, ret;
4884
4885 attrs = wq_sysfs_prep_attrs(wq);
4886 if (!attrs)
4887 return -ENOMEM;
4888
4889 ret = -EINVAL;
4890 if (sscanf(buf, "%d", &v) == 1) {
4891 attrs->no_numa = !v;
4892 ret = apply_workqueue_attrs(wq, attrs);
4893 }
4894
4895 free_workqueue_attrs(attrs);
4896 return ret ?: count;
4897}
4898
4899static struct device_attribute wq_sysfs_unbound_attrs[] = {
4900 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
4901 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
4902 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
4903 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
4904 __ATTR_NULL,
4905};
4906
4907static struct bus_type wq_subsys = {
4908 .name = "workqueue",
4909 .dev_groups = wq_sysfs_groups,
4910};
4911
4912static int __init wq_sysfs_init(void)
4913{
4914 return subsys_virtual_register(&wq_subsys, NULL);
4915}
4916core_initcall(wq_sysfs_init);
4917
4918static void wq_device_release(struct device *dev)
4919{
4920 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
4921
4922 kfree(wq_dev);
4923}
4924
4925/**
4926 * workqueue_sysfs_register - make a workqueue visible in sysfs
4927 * @wq: the workqueue to register
4928 *
4929 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
4930 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
4931 * which is the preferred method.
4932 *
4933 * Workqueue user should use this function directly iff it wants to apply
4934 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
4935 * apply_workqueue_attrs() may race against userland updating the
4936 * attributes.
4937 *
4938 * Return: 0 on success, -errno on failure.
4939 */
4940int workqueue_sysfs_register(struct workqueue_struct *wq)
4941{
4942 struct wq_device *wq_dev;
4943 int ret;
4944
4945 /*
4946 * Adjusting max_active or creating new pwqs by applyting
4947 * attributes breaks ordering guarantee. Disallow exposing ordered
4948 * workqueues.
4949 */
4950 if (WARN_ON(wq->flags & __WQ_ORDERED))
4951 return -EINVAL;
4952
4953 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
4954 if (!wq_dev)
4955 return -ENOMEM;
4956
4957 wq_dev->wq = wq;
4958 wq_dev->dev.bus = &wq_subsys;
4959 wq_dev->dev.init_name = wq->name;
4960 wq_dev->dev.release = wq_device_release;
4961
4962 /*
4963 * unbound_attrs are created separately. Suppress uevent until
4964 * everything is ready.
4965 */
4966 dev_set_uevent_suppress(&wq_dev->dev, true);
4967
4968 ret = device_register(&wq_dev->dev);
4969 if (ret) {
4970 kfree(wq_dev);
4971 wq->wq_dev = NULL;
4972 return ret;
4973 }
4974
4975 if (wq->flags & WQ_UNBOUND) {
4976 struct device_attribute *attr;
4977
4978 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
4979 ret = device_create_file(&wq_dev->dev, attr);
4980 if (ret) {
4981 device_unregister(&wq_dev->dev);
4982 wq->wq_dev = NULL;
4983 return ret;
4984 }
4985 }
4986 }
4987
4988 dev_set_uevent_suppress(&wq_dev->dev, false);
4989 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
4990 return 0;
4991}
4992
4993/**
4994 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
4995 * @wq: the workqueue to unregister
4996 *
4997 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
4998 */
4999static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
5000{
5001 struct wq_device *wq_dev = wq->wq_dev;
5002
5003 if (!wq->wq_dev)
5004 return;
5005
5006 wq->wq_dev = NULL;
5007 device_unregister(&wq_dev->dev);
5008}
5009#else /* CONFIG_SYSFS */
5010static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
5011#endif /* CONFIG_SYSFS */
5012
Tejun Heobce90382013-04-01 11:23:32 -07005013static void __init wq_numa_init(void)
5014{
5015 cpumask_var_t *tbl;
5016 int node, cpu;
5017
Tejun Heobce90382013-04-01 11:23:32 -07005018 if (num_possible_nodes() <= 1)
5019 return;
5020
Tejun Heod55262c2013-04-01 11:23:38 -07005021 if (wq_disable_numa) {
5022 pr_info("workqueue: NUMA affinity support disabled\n");
5023 return;
5024 }
5025
Tejun Heo4c16bd32013-04-01 11:23:36 -07005026 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
5027 BUG_ON(!wq_update_unbound_numa_attrs_buf);
5028
Tejun Heobce90382013-04-01 11:23:32 -07005029 /*
5030 * We want masks of possible CPUs of each node which isn't readily
5031 * available. Build one from cpu_to_node() which should have been
5032 * fully initialized by now.
5033 */
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08005034 tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL);
Tejun Heobce90382013-04-01 11:23:32 -07005035 BUG_ON(!tbl);
5036
5037 for_each_node(node)
Yasuaki Ishimatsu5a6024f2014-07-07 09:56:48 -04005038 BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
Tejun Heo1be0c252013-05-15 14:24:24 -07005039 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07005040
5041 for_each_possible_cpu(cpu) {
5042 node = cpu_to_node(cpu);
5043 if (WARN_ON(node == NUMA_NO_NODE)) {
5044 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
5045 /* happens iff arch is bonkers, let's just proceed */
5046 return;
5047 }
5048 cpumask_set_cpu(cpu, tbl[node]);
5049 }
5050
5051 wq_numa_possible_cpumask = tbl;
5052 wq_numa_enabled = true;
5053}
5054
Suresh Siddha6ee05782010-07-30 14:57:37 -07005055static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
Tejun Heo7a4e3442013-03-12 11:30:00 -07005057 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
5058 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02005059
Tejun Heoe904e6c2013-03-12 11:29:57 -07005060 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
5061
5062 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
5063
Tejun Heo65758202012-07-17 12:39:26 -07005064 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07005065 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02005066
Tejun Heobce90382013-04-01 11:23:32 -07005067 wq_numa_init();
5068
Tejun Heo706026c2013-01-24 11:01:34 -08005069 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07005070 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005071 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02005072
Tejun Heo7a4e3442013-03-12 11:30:00 -07005073 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07005074 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07005075 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08005076 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07005077 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07005078 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07005079 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07005080
Tejun Heo9daf9e62013-01-24 11:01:33 -08005081 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005082 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08005083 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005084 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07005085 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02005086 }
5087
Tejun Heoe22bee72010-06-29 10:07:14 +02005088 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07005089 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005090 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02005091
Tejun Heof02ae732013-03-12 11:30:03 -07005092 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07005093 pool->flags &= ~POOL_DISASSOCIATED;
Lai Jiangshan051e1852014-07-22 13:03:02 +08005094 BUG_ON(!create_worker(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07005095 }
Tejun Heoe22bee72010-06-29 10:07:14 +02005096 }
5097
Tejun Heo8a2b7532013-09-05 12:30:04 -04005098 /* create default unbound and ordered wq attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -07005099 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
5100 struct workqueue_attrs *attrs;
5101
5102 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07005103 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07005104 unbound_std_wq_attrs[i] = attrs;
Tejun Heo8a2b7532013-09-05 12:30:04 -04005105
5106 /*
5107 * An ordered wq should have only one pwq as ordering is
5108 * guaranteed by max_active which is enforced by pwqs.
5109 * Turn off NUMA so that dfl_pwq is used for all nodes.
5110 */
5111 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
5112 attrs->nice = std_nice[i];
5113 attrs->no_numa = true;
5114 ordered_wq_attrs[i] = attrs;
Tejun Heo29c91e92013-03-12 11:30:03 -07005115 }
5116
Tejun Heod320c032010-06-29 10:07:14 +02005117 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005118 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005119 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005120 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5121 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005122 system_freezable_wq = alloc_workqueue("events_freezable",
5123 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305124 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5125 WQ_POWER_EFFICIENT, 0);
5126 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5127 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5128 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005129 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305130 !system_unbound_wq || !system_freezable_wq ||
5131 !system_power_efficient_wq ||
5132 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005135early_initcall(init_workqueues);