blob: 6b9b0dc3dea50269a85266509687ad9e69c5392f [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 */
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800162 struct mutex attach_mutex; /* attach/detach exclusion */
163 struct list_head workers; /* A: attached workers */
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +0800164 struct completion *detach_completion; /* all workers detached */
Tejun Heoe19e3972013-01-24 11:39:44 -0800165
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +0800166 struct ida worker_ida; /* worker IDs for task name */
Tejun Heoe19e3972013-01-24 11:39:44 -0800167
Tejun Heo7a4e3442013-03-12 11:30:00 -0700168 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700169 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
170 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700171
Tejun Heoe19e3972013-01-24 11:39:44 -0800172 /*
173 * The current concurrency level. As it's likely to be accessed
174 * from other CPUs during try_to_wake_up(), put it in a separate
175 * cacheline.
176 */
177 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700178
179 /*
180 * Destruction of pool is sched-RCU protected to allow dereferences
181 * from get_work_pool().
182 */
183 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184} ____cacheline_aligned_in_smp;
185
186/*
Tejun Heo112202d2013-02-13 19:29:12 -0800187 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
188 * of work_struct->data are used for flags and the remaining high bits
189 * point to the pwq; thus, pwqs need to be aligned at two's power of the
190 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Tejun Heo112202d2013-02-13 19:29:12 -0800192struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700193 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200194 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200195 int work_color; /* L: current color */
196 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700197 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200198 int nr_in_flight[WORK_NR_COLORS];
199 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200201 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200202 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700203 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700204 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700205
206 /*
207 * Release of unbound pwq is punted to system_wq. See put_pwq()
208 * and pwq_unbound_release_workfn() for details. pool_workqueue
209 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700210 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700211 */
212 struct work_struct unbound_release_work;
213 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700214} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200217 * Structure used to wait for workqueue flush.
218 */
219struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700220 struct list_head list; /* WQ: list of flushers */
221 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200222 struct completion done; /* flush completion */
223};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Tejun Heo226223a2013-03-12 11:30:05 -0700225struct wq_device;
226
Tejun Heo73f53c42010-06-29 10:07:11 +0200227/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700228 * The externally visible workqueue. It relays the issued work items to
229 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700232 struct list_head pwqs; /* WR: all pwqs of this wq */
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400233 struct list_head list; /* PR: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200234
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700235 struct mutex mutex; /* protects this wq */
236 int work_color; /* WQ: current work color */
237 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800238 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700239 struct wq_flusher *first_flusher; /* WQ: first flusher */
240 struct list_head flusher_queue; /* WQ: flush waiters */
241 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200242
Tejun Heo2e109a22013-03-13 19:47:40 -0700243 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200244 struct worker *rescuer; /* I: rescue worker */
245
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700246 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700247 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700248
Tejun Heo6029a912013-04-01 11:23:34 -0700249 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
Tejun Heo4c16bd32013-04-01 11:23:36 -0700250 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700251
Tejun Heo226223a2013-03-12 11:30:05 -0700252#ifdef CONFIG_SYSFS
253 struct wq_device *wq_dev; /* I: for sysfs interface */
254#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700255#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200256 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700257#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700258 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700259
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400260 /*
261 * Destruction of workqueue_struct is sched-RCU protected to allow
262 * walking the workqueues list without grabbing wq_pool_mutex.
263 * This is used to dump all workqueues from sysrq.
264 */
265 struct rcu_head rcu;
266
Tejun Heo2728fd22013-04-01 11:23:35 -0700267 /* hot fields used during command issue, aligned to cacheline */
268 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
269 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700270 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Tejun Heoe904e6c2013-03-12 11:29:57 -0700273static struct kmem_cache *pwq_cache;
274
Tejun Heobce90382013-04-01 11:23:32 -0700275static cpumask_var_t *wq_numa_possible_cpumask;
276 /* possible CPUs of each node */
277
Tejun Heod55262c2013-04-01 11:23:38 -0700278static bool wq_disable_numa;
279module_param_named(disable_numa, wq_disable_numa, bool, 0444);
280
Viresh Kumarcee22a12013-04-08 16:45:40 +0530281/* see the comment above the definition of WQ_POWER_EFFICIENT */
282#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
283static bool wq_power_efficient = true;
284#else
285static bool wq_power_efficient;
286#endif
287
288module_param_named(power_efficient, wq_power_efficient, bool, 0444);
289
Tejun Heobce90382013-04-01 11:23:32 -0700290static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
291
Tejun Heo4c16bd32013-04-01 11:23:36 -0700292/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
293static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
294
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700295static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700296static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700297
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400298static LIST_HEAD(workqueues); /* PR: list of all workqueues */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700299static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700300
301/* the per-cpu worker pools */
302static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
303 cpu_worker_pools);
304
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700305static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700306
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700307/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700308static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
309
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700310/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700311static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
312
Tejun Heo8a2b7532013-09-05 12:30:04 -0400313/* I: attributes used when instantiating ordered pools on demand */
314static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS];
315
Tejun Heod320c032010-06-29 10:07:14 +0200316struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400317EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300318struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900319EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300320struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200321EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300322struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200323EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300324struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100325EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530326struct workqueue_struct *system_power_efficient_wq __read_mostly;
327EXPORT_SYMBOL_GPL(system_power_efficient_wq);
328struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
329EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200330
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700331static int worker_thread(void *__worker);
332static void copy_workqueue_attrs(struct workqueue_attrs *to,
333 const struct workqueue_attrs *from);
334
Tejun Heo97bd2342010-10-05 10:41:14 +0200335#define CREATE_TRACE_POINTS
336#include <trace/events/workqueue.h>
337
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700338#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700339 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700340 lockdep_is_held(&wq_pool_mutex), \
341 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700342
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700343#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700344 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700345 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700346 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700347
Tejun Heof02ae732013-03-12 11:30:03 -0700348#define for_each_cpu_worker_pool(pool, cpu) \
349 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
350 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700351 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700352
Tejun Heo49e3cf42013-03-12 11:29:58 -0700353/**
Tejun Heo17116962013-03-12 11:29:58 -0700354 * for_each_pool - iterate through all worker_pools in the system
355 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700356 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700357 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700358 * This must be called either with wq_pool_mutex held or sched RCU read
359 * locked. If the pool needs to be used beyond the locking in effect, the
360 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700361 *
362 * The if/else clause exists only for the lockdep assertion and can be
363 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700364 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700365#define for_each_pool(pool, pi) \
366 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700367 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700368 else
Tejun Heo17116962013-03-12 11:29:58 -0700369
370/**
Tejun Heo822d8402013-03-19 13:45:21 -0700371 * for_each_pool_worker - iterate through all workers of a worker_pool
372 * @worker: iteration cursor
Tejun Heo822d8402013-03-19 13:45:21 -0700373 * @pool: worker_pool to iterate workers of
374 *
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800375 * This must be called with @pool->attach_mutex.
Tejun Heo822d8402013-03-19 13:45:21 -0700376 *
377 * The if/else clause exists only for the lockdep assertion and can be
378 * ignored.
379 */
Lai Jiangshanda028462014-05-20 17:46:31 +0800380#define for_each_pool_worker(worker, pool) \
381 list_for_each_entry((worker), &(pool)->workers, node) \
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800382 if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \
Tejun Heo822d8402013-03-19 13:45:21 -0700383 else
384
385/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700386 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
387 * @pwq: iteration cursor
388 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700389 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700390 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700391 * If the pwq needs to be used beyond the locking in effect, the caller is
392 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700393 *
394 * The if/else clause exists only for the lockdep assertion and can be
395 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700396 */
397#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700398 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700399 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700400 else
Tejun Heof3421792010-07-02 10:03:51 +0200401
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900402#ifdef CONFIG_DEBUG_OBJECTS_WORK
403
404static struct debug_obj_descr work_debug_descr;
405
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100406static void *work_debug_hint(void *addr)
407{
408 return ((struct work_struct *) addr)->func;
409}
410
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900411/*
412 * fixup_init is called when:
413 * - an active object is initialized
414 */
415static int work_fixup_init(void *addr, enum debug_obj_state state)
416{
417 struct work_struct *work = addr;
418
419 switch (state) {
420 case ODEBUG_STATE_ACTIVE:
421 cancel_work_sync(work);
422 debug_object_init(work, &work_debug_descr);
423 return 1;
424 default:
425 return 0;
426 }
427}
428
429/*
430 * fixup_activate is called when:
431 * - an active object is activated
432 * - an unknown object is activated (might be a statically initialized object)
433 */
434static int work_fixup_activate(void *addr, enum debug_obj_state state)
435{
436 struct work_struct *work = addr;
437
438 switch (state) {
439
440 case ODEBUG_STATE_NOTAVAILABLE:
441 /*
442 * This is not really a fixup. The work struct was
443 * statically initialized. We just make sure that it
444 * is tracked in the object tracker.
445 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200446 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900447 debug_object_init(work, &work_debug_descr);
448 debug_object_activate(work, &work_debug_descr);
449 return 0;
450 }
451 WARN_ON_ONCE(1);
452 return 0;
453
454 case ODEBUG_STATE_ACTIVE:
455 WARN_ON(1);
456
457 default:
458 return 0;
459 }
460}
461
462/*
463 * fixup_free is called when:
464 * - an active object is freed
465 */
466static int work_fixup_free(void *addr, enum debug_obj_state state)
467{
468 struct work_struct *work = addr;
469
470 switch (state) {
471 case ODEBUG_STATE_ACTIVE:
472 cancel_work_sync(work);
473 debug_object_free(work, &work_debug_descr);
474 return 1;
475 default:
476 return 0;
477 }
478}
479
480static struct debug_obj_descr work_debug_descr = {
481 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100482 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900483 .fixup_init = work_fixup_init,
484 .fixup_activate = work_fixup_activate,
485 .fixup_free = work_fixup_free,
486};
487
488static inline void debug_work_activate(struct work_struct *work)
489{
490 debug_object_activate(work, &work_debug_descr);
491}
492
493static inline void debug_work_deactivate(struct work_struct *work)
494{
495 debug_object_deactivate(work, &work_debug_descr);
496}
497
498void __init_work(struct work_struct *work, int onstack)
499{
500 if (onstack)
501 debug_object_init_on_stack(work, &work_debug_descr);
502 else
503 debug_object_init(work, &work_debug_descr);
504}
505EXPORT_SYMBOL_GPL(__init_work);
506
507void destroy_work_on_stack(struct work_struct *work)
508{
509 debug_object_free(work, &work_debug_descr);
510}
511EXPORT_SYMBOL_GPL(destroy_work_on_stack);
512
Thomas Gleixnerea2e64f2014-03-23 14:20:44 +0000513void destroy_delayed_work_on_stack(struct delayed_work *work)
514{
515 destroy_timer_on_stack(&work->timer);
516 debug_object_free(&work->work, &work_debug_descr);
517}
518EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack);
519
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900520#else
521static inline void debug_work_activate(struct work_struct *work) { }
522static inline void debug_work_deactivate(struct work_struct *work) { }
523#endif
524
Li Bin4e8b22b2013-09-10 09:52:35 +0800525/**
526 * worker_pool_assign_id - allocate ID and assing it to @pool
527 * @pool: the pool pointer of interest
528 *
529 * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned
530 * successfully, -errno on failure.
531 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800532static int worker_pool_assign_id(struct worker_pool *pool)
533{
534 int ret;
535
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700536 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700537
Li Bin4e8b22b2013-09-10 09:52:35 +0800538 ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,
539 GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700540 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700541 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700542 return 0;
543 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800544 return ret;
545}
546
Tejun Heo76af4d92013-03-12 11:30:00 -0700547/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700548 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
549 * @wq: the target workqueue
550 * @node: the node ID
551 *
552 * This must be called either with pwq_lock held or sched RCU read locked.
553 * If the pwq needs to be used beyond the locking in effect, the caller is
554 * responsible for guaranteeing that the pwq stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700555 *
556 * Return: The unbound pool_workqueue for @node.
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700557 */
558static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
559 int node)
560{
561 assert_rcu_or_wq_mutex(wq);
562 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
563}
564
Tejun Heo73f53c42010-06-29 10:07:11 +0200565static unsigned int work_color_to_flags(int color)
566{
567 return color << WORK_STRUCT_COLOR_SHIFT;
568}
569
570static int get_work_color(struct work_struct *work)
571{
572 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
573 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
574}
575
576static int work_next_color(int color)
577{
578 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700579}
580
David Howells4594bf12006-12-07 11:33:26 +0000581/*
Tejun Heo112202d2013-02-13 19:29:12 -0800582 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
583 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800584 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585 *
Tejun Heo112202d2013-02-13 19:29:12 -0800586 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
587 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700588 * work->data. These functions should only be called while the work is
589 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590 *
Tejun Heo112202d2013-02-13 19:29:12 -0800591 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800592 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800593 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800594 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700595 *
596 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
597 * canceled. While being canceled, a work item may have its PENDING set
598 * but stay off timer and worklist for arbitrarily long and nobody should
599 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000600 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200601static inline void set_work_data(struct work_struct *work, unsigned long data,
602 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000603{
Tejun Heo6183c002013-03-12 11:29:57 -0700604 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000606}
David Howells365970a2006-11-22 14:54:49 +0000607
Tejun Heo112202d2013-02-13 19:29:12 -0800608static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200609 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200610{
Tejun Heo112202d2013-02-13 19:29:12 -0800611 set_work_data(work, (unsigned long)pwq,
612 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200613}
614
Lai Jiangshan4468a002013-02-06 18:04:53 -0800615static void set_work_pool_and_keep_pending(struct work_struct *work,
616 int pool_id)
617{
618 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
619 WORK_STRUCT_PENDING);
620}
621
Tejun Heo7c3eed52013-01-24 11:01:33 -0800622static void set_work_pool_and_clear_pending(struct work_struct *work,
623 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000624{
Tejun Heo23657bb2012-08-13 17:08:19 -0700625 /*
626 * The following wmb is paired with the implied mb in
627 * test_and_set_bit(PENDING) and ensures all updates to @work made
628 * here are visible to and precede any updates by the next PENDING
629 * owner.
630 */
631 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800632 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200633}
634
635static void clear_work_data(struct work_struct *work)
636{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800637 smp_wmb(); /* see set_work_pool_and_clear_pending() */
638 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200639}
640
Tejun Heo112202d2013-02-13 19:29:12 -0800641static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200642{
Tejun Heoe1201532010-07-22 14:14:25 +0200643 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200644
Tejun Heo112202d2013-02-13 19:29:12 -0800645 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200646 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
647 else
648 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200649}
650
Tejun Heo7c3eed52013-01-24 11:01:33 -0800651/**
652 * get_work_pool - return the worker_pool a given work was associated with
653 * @work: the work item of interest
654 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700655 * Pools are created and destroyed under wq_pool_mutex, and allows read
656 * access under sched-RCU read lock. As such, this function should be
657 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700658 *
659 * All fields of the returned pool are accessible as long as the above
660 * mentioned locking is in effect. If the returned pool needs to be used
661 * beyond the critical section, the caller is responsible for ensuring the
662 * returned pool is and stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700663 *
664 * Return: The worker_pool @work was last associated with. %NULL if none.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800665 */
666static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200667{
Tejun Heoe1201532010-07-22 14:14:25 +0200668 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800669 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200670
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700671 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700672
Tejun Heo112202d2013-02-13 19:29:12 -0800673 if (data & WORK_STRUCT_PWQ)
674 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800675 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200676
Tejun Heo7c3eed52013-01-24 11:01:33 -0800677 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
678 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200679 return NULL;
680
Tejun Heofa1b54e2013-03-12 11:30:00 -0700681 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800682}
683
684/**
685 * get_work_pool_id - return the worker pool ID a given work is associated with
686 * @work: the work item of interest
687 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700688 * Return: The worker_pool ID @work was last associated with.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800689 * %WORK_OFFQ_POOL_NONE if none.
690 */
691static int get_work_pool_id(struct work_struct *work)
692{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800693 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800694
Tejun Heo112202d2013-02-13 19:29:12 -0800695 if (data & WORK_STRUCT_PWQ)
696 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800697 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
698
699 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800700}
701
Tejun Heobbb68df2012-08-03 10:30:46 -0700702static void mark_work_canceling(struct work_struct *work)
703{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800704 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700705
Tejun Heo7c3eed52013-01-24 11:01:33 -0800706 pool_id <<= WORK_OFFQ_POOL_SHIFT;
707 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700708}
709
710static bool work_is_canceling(struct work_struct *work)
711{
712 unsigned long data = atomic_long_read(&work->data);
713
Tejun Heo112202d2013-02-13 19:29:12 -0800714 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700715}
716
David Howells365970a2006-11-22 14:54:49 +0000717/*
Tejun Heo32704762012-07-13 22:16:45 -0700718 * Policy functions. These define the policies on how the global worker
719 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800720 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000721 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200722
Tejun Heo63d95a92012-07-12 14:46:37 -0700723static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000724{
Tejun Heoe19e3972013-01-24 11:39:44 -0800725 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000726}
727
Tejun Heoe22bee72010-06-29 10:07:14 +0200728/*
729 * Need to wake up a worker? Called from anything but currently
730 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700731 *
732 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800733 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700734 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200735 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700736static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000737{
Tejun Heo63d95a92012-07-12 14:46:37 -0700738 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000739}
740
Tejun Heoe22bee72010-06-29 10:07:14 +0200741/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700742static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200743{
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200745}
746
747/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700748static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200749{
Tejun Heoe19e3972013-01-24 11:39:44 -0800750 return !list_empty(&pool->worklist) &&
751 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200752}
753
754/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700755static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200756{
Tejun Heo63d95a92012-07-12 14:46:37 -0700757 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200758}
759
Tejun Heoe22bee72010-06-29 10:07:14 +0200760/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700761static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200762{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700763 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700764 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
765 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200766
767 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
768}
769
770/*
771 * Wake up functions.
772 */
773
Lai Jiangshan1037de32014-05-22 16:44:07 +0800774/* Return the first idle worker. Safe with preemption disabled */
775static struct worker *first_idle_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200776{
Tejun Heo63d95a92012-07-12 14:46:37 -0700777 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200778 return NULL;
779
Tejun Heo63d95a92012-07-12 14:46:37 -0700780 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200781}
782
783/**
784 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700785 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200786 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700787 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200788 *
789 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800790 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200791 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700792static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200793{
Lai Jiangshan1037de32014-05-22 16:44:07 +0800794 struct worker *worker = first_idle_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200795
796 if (likely(worker))
797 wake_up_process(worker->task);
798}
799
Tejun Heo4690c4a2010-06-29 10:07:10 +0200800/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200801 * wq_worker_waking_up - a worker is waking up
802 * @task: task waking up
803 * @cpu: CPU @task is waking up to
804 *
805 * This function is called during try_to_wake_up() when a worker is
806 * being awoken.
807 *
808 * CONTEXT:
809 * spin_lock_irq(rq->lock)
810 */
Tejun Heod84ff052013-03-12 11:29:59 -0700811void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200812{
813 struct worker *worker = kthread_data(task);
814
Joonsoo Kim36576002012-10-26 23:03:49 +0900815 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800816 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800817 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900818 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200819}
820
821/**
822 * wq_worker_sleeping - a worker is going to sleep
823 * @task: task going to sleep
824 * @cpu: CPU in question, must be the current CPU number
825 *
826 * This function is called during schedule() when a busy worker is
827 * going to sleep. Worker on the same cpu can be woken up by
828 * returning pointer to its task.
829 *
830 * CONTEXT:
831 * spin_lock_irq(rq->lock)
832 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700833 * Return:
Tejun Heoe22bee72010-06-29 10:07:14 +0200834 * Worker task on @cpu to wake up, %NULL if none.
835 */
Tejun Heod84ff052013-03-12 11:29:59 -0700836struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200837{
838 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800839 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
Tejun Heo111c2252013-01-17 17:16:24 -0800841 /*
842 * Rescuers, which may not have all the fields set up like normal
843 * workers, also reach here, let's not access anything before
844 * checking NOT_RUNNING.
845 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500846 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200847 return NULL;
848
Tejun Heo111c2252013-01-17 17:16:24 -0800849 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800850
Tejun Heoe22bee72010-06-29 10:07:14 +0200851 /* this can only happen on the local cpu */
Lai Jiangshan92b69f52014-06-03 15:33:08 +0800852 if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
Tejun Heo6183c002013-03-12 11:29:57 -0700853 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200854
855 /*
856 * The counterpart of the following dec_and_test, implied mb,
857 * worklist not empty test sequence is in insert_work().
858 * Please read comment there.
859 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700860 * NOT_RUNNING is clear. This means that we're bound to and
861 * running on the local cpu w/ rq lock held and preemption
862 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800863 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700864 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800866 if (atomic_dec_and_test(&pool->nr_running) &&
867 !list_empty(&pool->worklist))
Lai Jiangshan1037de32014-05-22 16:44:07 +0800868 to_wakeup = first_idle_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 return to_wakeup ? to_wakeup->task : NULL;
870}
871
872/**
873 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200874 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200875 * @flags: flags to set
Tejun Heod302f012010-06-29 10:07:13 +0200876 *
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800877 * Set @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200878 *
Tejun Heocb444762010-07-02 10:03:50 +0200879 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800880 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200881 */
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800882static inline void worker_set_flags(struct worker *worker, unsigned int flags)
Tejun Heod302f012010-06-29 10:07:13 +0200883{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700884 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200885
Tejun Heocb444762010-07-02 10:03:50 +0200886 WARN_ON_ONCE(worker->task != current);
887
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800888 /* If transitioning into NOT_RUNNING, adjust nr_running. */
Tejun Heoe22bee72010-06-29 10:07:14 +0200889 if ((flags & WORKER_NOT_RUNNING) &&
890 !(worker->flags & WORKER_NOT_RUNNING)) {
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800891 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200892 }
893
Tejun Heod302f012010-06-29 10:07:13 +0200894 worker->flags |= flags;
895}
896
897/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200898 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200899 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200900 * @flags: flags to clear
901 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200902 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200903 *
Tejun Heocb444762010-07-02 10:03:50 +0200904 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800905 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200906 */
907static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
908{
Tejun Heo63d95a92012-07-12 14:46:37 -0700909 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200910 unsigned int oflags = worker->flags;
911
Tejun Heocb444762010-07-02 10:03:50 +0200912 WARN_ON_ONCE(worker->task != current);
913
Tejun Heod302f012010-06-29 10:07:13 +0200914 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200915
Tejun Heo42c025f2011-01-11 15:58:49 +0100916 /*
917 * If transitioning out of NOT_RUNNING, increment nr_running. Note
918 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
919 * of multiple flags, not a single flag.
920 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200921 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
922 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800923 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200924}
925
926/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200927 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800928 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200929 * @work: work to find worker for
930 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800931 * Find a worker which is executing @work on @pool by searching
932 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800933 * to match, its current execution should match the address of @work and
934 * its work function. This is to avoid unwanted dependency between
935 * unrelated work executions through a work item being recycled while still
936 * being executed.
937 *
938 * This is a bit tricky. A work item may be freed once its execution
939 * starts and nothing prevents the freed area from being recycled for
940 * another work item. If the same work item address ends up being reused
941 * before the original execution finishes, workqueue will identify the
942 * recycled work item as currently executing and make it wait until the
943 * current execution finishes, introducing an unwanted dependency.
944 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700945 * This function checks the work item address and work function to avoid
946 * false positives. Note that this isn't complete as one may construct a
947 * work function which can introduce dependency onto itself through a
948 * recycled work item. Well, if somebody wants to shoot oneself in the
949 * foot that badly, there's only so much we can do, and if such deadlock
950 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200951 *
952 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800953 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200954 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700955 * Return:
956 * Pointer to worker which is executing @work if found, %NULL
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200957 * otherwise.
958 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800959static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200960 struct work_struct *work)
961{
Sasha Levin42f85702012-12-17 10:01:23 -0500962 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500963
Sasha Levinb67bfe02013-02-27 17:06:00 -0800964 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800965 (unsigned long)work)
966 if (worker->current_work == work &&
967 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500968 return worker;
969
970 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200971}
972
973/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700974 * move_linked_works - move linked works to a list
975 * @work: start of series of works to be scheduled
976 * @head: target list to append @work to
977 * @nextp: out paramter for nested worklist walking
978 *
979 * Schedule linked works starting from @work to @head. Work series to
980 * be scheduled starts at @work and includes any consecutive work with
981 * WORK_STRUCT_LINKED set in its predecessor.
982 *
983 * If @nextp is not NULL, it's updated to point to the next work of
984 * the last scheduled work. This allows move_linked_works() to be
985 * nested inside outer list_for_each_entry_safe().
986 *
987 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800988 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700989 */
990static void move_linked_works(struct work_struct *work, struct list_head *head,
991 struct work_struct **nextp)
992{
993 struct work_struct *n;
994
995 /*
996 * Linked worklist will always end before the end of the list,
997 * use NULL for list head.
998 */
999 list_for_each_entry_safe_from(work, n, NULL, entry) {
1000 list_move_tail(&work->entry, head);
1001 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1002 break;
1003 }
1004
1005 /*
1006 * If we're already inside safe list traversal and have moved
1007 * multiple works to the scheduled queue, the next position
1008 * needs to be updated.
1009 */
1010 if (nextp)
1011 *nextp = n;
1012}
1013
Tejun Heo8864b4e2013-03-12 11:30:04 -07001014/**
1015 * get_pwq - get an extra reference on the specified pool_workqueue
1016 * @pwq: pool_workqueue to get
1017 *
1018 * Obtain an extra reference on @pwq. The caller should guarantee that
1019 * @pwq has positive refcnt and be holding the matching pool->lock.
1020 */
1021static void get_pwq(struct pool_workqueue *pwq)
1022{
1023 lockdep_assert_held(&pwq->pool->lock);
1024 WARN_ON_ONCE(pwq->refcnt <= 0);
1025 pwq->refcnt++;
1026}
1027
1028/**
1029 * put_pwq - put a pool_workqueue reference
1030 * @pwq: pool_workqueue to put
1031 *
1032 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1033 * destruction. The caller should be holding the matching pool->lock.
1034 */
1035static void put_pwq(struct pool_workqueue *pwq)
1036{
1037 lockdep_assert_held(&pwq->pool->lock);
1038 if (likely(--pwq->refcnt))
1039 return;
1040 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1041 return;
1042 /*
1043 * @pwq can't be released under pool->lock, bounce to
1044 * pwq_unbound_release_workfn(). This never recurses on the same
1045 * pool->lock as this path is taken only for unbound workqueues and
1046 * the release work item is scheduled on a per-cpu workqueue. To
1047 * avoid lockdep warning, unbound pool->locks are given lockdep
1048 * subclass of 1 in get_unbound_pool().
1049 */
1050 schedule_work(&pwq->unbound_release_work);
1051}
1052
Tejun Heodce90d42013-04-01 11:23:35 -07001053/**
1054 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1055 * @pwq: pool_workqueue to put (can be %NULL)
1056 *
1057 * put_pwq() with locking. This function also allows %NULL @pwq.
1058 */
1059static void put_pwq_unlocked(struct pool_workqueue *pwq)
1060{
1061 if (pwq) {
1062 /*
1063 * As both pwqs and pools are sched-RCU protected, the
1064 * following lock operations are safe.
1065 */
1066 spin_lock_irq(&pwq->pool->lock);
1067 put_pwq(pwq);
1068 spin_unlock_irq(&pwq->pool->lock);
1069 }
1070}
1071
Tejun Heo112202d2013-02-13 19:29:12 -08001072static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001073{
Tejun Heo112202d2013-02-13 19:29:12 -08001074 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001075
1076 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001077 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001078 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001079 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001080}
1081
Tejun Heo112202d2013-02-13 19:29:12 -08001082static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001083{
Tejun Heo112202d2013-02-13 19:29:12 -08001084 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001085 struct work_struct, entry);
1086
Tejun Heo112202d2013-02-13 19:29:12 -08001087 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001088}
1089
Tejun Heobf4ede02012-08-03 10:30:46 -07001090/**
Tejun Heo112202d2013-02-13 19:29:12 -08001091 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1092 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001093 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001094 *
1095 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001096 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001097 *
1098 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001099 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001100 */
Tejun Heo112202d2013-02-13 19:29:12 -08001101static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001102{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001103 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001104 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001105 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001106
Tejun Heo112202d2013-02-13 19:29:12 -08001107 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001108
Tejun Heo112202d2013-02-13 19:29:12 -08001109 pwq->nr_active--;
1110 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001111 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001112 if (pwq->nr_active < pwq->max_active)
1113 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001114 }
1115
1116 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001117 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001118 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001119
1120 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001121 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001122 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001123
Tejun Heo112202d2013-02-13 19:29:12 -08001124 /* this pwq is done, clear flush_color */
1125 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001126
1127 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001128 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001129 * will handle the rest.
1130 */
Tejun Heo112202d2013-02-13 19:29:12 -08001131 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1132 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001133out_put:
1134 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001135}
1136
Tejun Heo36e227d2012-08-03 10:30:46 -07001137/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001138 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001139 * @work: work item to steal
1140 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001141 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001142 *
1143 * Try to grab PENDING bit of @work. This function can handle @work in any
Yacine Belkadid185af32013-07-31 14:59:24 -07001144 * stable state - idle, on timer or on worklist.
Tejun Heo36e227d2012-08-03 10:30:46 -07001145 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001146 * Return:
Tejun Heo36e227d2012-08-03 10:30:46 -07001147 * 1 if @work was pending and we successfully stole PENDING
1148 * 0 if @work was idle and we claimed PENDING
1149 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001150 * -ENOENT if someone else is canceling @work, this state may persist
1151 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001152 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001153 * Note:
Tejun Heobbb68df2012-08-03 10:30:46 -07001154 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001155 * interrupted while holding PENDING and @work off queue, irq must be
1156 * disabled on entry. This, combined with delayed_work->timer being
1157 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001158 *
1159 * On successful return, >= 0, irq is disabled and the caller is
1160 * responsible for releasing it using local_irq_restore(*@flags).
1161 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001162 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001163 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001164static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1165 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001166{
Tejun Heod565ed62013-01-24 11:01:33 -08001167 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001168 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001169
Tejun Heobbb68df2012-08-03 10:30:46 -07001170 local_irq_save(*flags);
1171
Tejun Heo36e227d2012-08-03 10:30:46 -07001172 /* try to steal the timer if it exists */
1173 if (is_dwork) {
1174 struct delayed_work *dwork = to_delayed_work(work);
1175
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001176 /*
1177 * dwork->timer is irqsafe. If del_timer() fails, it's
1178 * guaranteed that the timer is not queued anywhere and not
1179 * running on the local CPU.
1180 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001181 if (likely(del_timer(&dwork->timer)))
1182 return 1;
1183 }
1184
1185 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001186 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1187 return 0;
1188
1189 /*
1190 * The queueing is in progress, or it is already queued. Try to
1191 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1192 */
Tejun Heod565ed62013-01-24 11:01:33 -08001193 pool = get_work_pool(work);
1194 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001195 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001196
Tejun Heod565ed62013-01-24 11:01:33 -08001197 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001198 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001199 * work->data is guaranteed to point to pwq only while the work
1200 * item is queued on pwq->wq, and both updating work->data to point
1201 * to pwq on queueing and to pool on dequeueing are done under
1202 * pwq->pool->lock. This in turn guarantees that, if work->data
1203 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001204 * item is currently queued on that pool.
1205 */
Tejun Heo112202d2013-02-13 19:29:12 -08001206 pwq = get_work_pwq(work);
1207 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001208 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001209
Tejun Heo16062832013-02-06 18:04:53 -08001210 /*
1211 * A delayed work item cannot be grabbed directly because
1212 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001213 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001214 * management later on and cause stall. Make sure the work
1215 * item is activated before grabbing.
1216 */
1217 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001218 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001219
Tejun Heo16062832013-02-06 18:04:53 -08001220 list_del_init(&work->entry);
Lai Jiangshan9c34a702014-07-11 00:11:13 +08001221 pwq_dec_nr_in_flight(pwq, get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001222
Tejun Heo112202d2013-02-13 19:29:12 -08001223 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001224 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001225
Tejun Heo16062832013-02-06 18:04:53 -08001226 spin_unlock(&pool->lock);
1227 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001228 }
Tejun Heod565ed62013-01-24 11:01:33 -08001229 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001230fail:
1231 local_irq_restore(*flags);
1232 if (work_is_canceling(work))
1233 return -ENOENT;
1234 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001235 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001236}
1237
1238/**
Tejun Heo706026c2013-01-24 11:01:34 -08001239 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001240 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001241 * @work: work to insert
1242 * @head: insertion point
1243 * @extra_flags: extra WORK_STRUCT_* flags to set
1244 *
Tejun Heo112202d2013-02-13 19:29:12 -08001245 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001246 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001247 *
1248 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001249 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001250 */
Tejun Heo112202d2013-02-13 19:29:12 -08001251static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1252 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001253{
Tejun Heo112202d2013-02-13 19:29:12 -08001254 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001255
Tejun Heo4690c4a2010-06-29 10:07:10 +02001256 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001257 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001258 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001259 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001260
1261 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001262 * Ensure either wq_worker_sleeping() sees the above
1263 * list_add_tail() or we see zero nr_running to avoid workers lying
1264 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001265 */
1266 smp_mb();
1267
Tejun Heo63d95a92012-07-12 14:46:37 -07001268 if (__need_more_worker(pool))
1269 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001270}
1271
Tejun Heoc8efcc22010-12-20 19:32:04 +01001272/*
1273 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001274 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001275 */
1276static bool is_chained_work(struct workqueue_struct *wq)
1277{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001278 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001279
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001280 worker = current_wq_worker();
1281 /*
1282 * Return %true iff I'm a worker execuing a work item on @wq. If
1283 * I'm @worker, it's safe to dereference it without locking.
1284 */
Tejun Heo112202d2013-02-13 19:29:12 -08001285 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001286}
1287
Tejun Heod84ff052013-03-12 11:29:59 -07001288static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct work_struct *work)
1290{
Tejun Heo112202d2013-02-13 19:29:12 -08001291 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001292 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001293 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001294 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001295 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001296
1297 /*
1298 * While a work item is PENDING && off queue, a task trying to
1299 * steal the PENDING will busy-loop waiting for it to either get
1300 * queued or lose PENDING. Grabbing PENDING and queueing should
1301 * happen with IRQ disabled.
1302 */
1303 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001305 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001306
Li Bin9ef28a72013-09-09 13:13:58 +08001307 /* if draining, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001308 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001309 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001310 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001311retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001312 if (req_cpu == WORK_CPU_UNBOUND)
1313 cpu = raw_smp_processor_id();
1314
Tejun Heoc9178082013-03-12 11:30:04 -07001315 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001316 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001317 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001318 else
1319 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001320
Tejun Heoc9178082013-03-12 11:30:04 -07001321 /*
1322 * If @work was previously on a different pool, it might still be
1323 * running there, in which case the work needs to be queued on that
1324 * pool to guarantee non-reentrancy.
1325 */
1326 last_pool = get_work_pool(work);
1327 if (last_pool && last_pool != pwq->pool) {
1328 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001329
Tejun Heoc9178082013-03-12 11:30:04 -07001330 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001331
Tejun Heoc9178082013-03-12 11:30:04 -07001332 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001333
Tejun Heoc9178082013-03-12 11:30:04 -07001334 if (worker && worker->current_pwq->wq == wq) {
1335 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001336 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001337 /* meh... not running there, queue here */
1338 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001339 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001340 }
Tejun Heof3421792010-07-02 10:03:51 +02001341 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001342 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001343 }
1344
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001345 /*
1346 * pwq is determined and locked. For unbound pools, we could have
1347 * raced with pwq release and it could already be dead. If its
1348 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001349 * without another pwq replacing it in the numa_pwq_tbl or while
1350 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001351 * make forward-progress.
1352 */
1353 if (unlikely(!pwq->refcnt)) {
1354 if (wq->flags & WQ_UNBOUND) {
1355 spin_unlock(&pwq->pool->lock);
1356 cpu_relax();
1357 goto retry;
1358 }
1359 /* oops */
1360 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1361 wq->name, cpu);
1362 }
1363
Tejun Heo112202d2013-02-13 19:29:12 -08001364 /* pwq determined, queue */
1365 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001366
Dan Carpenterf5b25522012-04-13 22:06:58 +03001367 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001368 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001369 return;
1370 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001371
Tejun Heo112202d2013-02-13 19:29:12 -08001372 pwq->nr_in_flight[pwq->work_color]++;
1373 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001374
Tejun Heo112202d2013-02-13 19:29:12 -08001375 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001376 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001377 pwq->nr_active++;
1378 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001379 } else {
1380 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001381 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001382 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001383
Tejun Heo112202d2013-02-13 19:29:12 -08001384 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001385
Tejun Heo112202d2013-02-13 19:29:12 -08001386 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001389/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001390 * queue_work_on - queue work on specific cpu
1391 * @cpu: CPU number to execute work on
1392 * @wq: workqueue to use
1393 * @work: work to queue
1394 *
Zhang Ruic1a220e2008-07-23 21:28:39 -07001395 * We queue the work to a specific CPU, the caller must ensure it
1396 * can't go away.
Yacine Belkadid185af32013-07-31 14:59:24 -07001397 *
1398 * Return: %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001399 */
Tejun Heod4283e92012-08-03 10:30:44 -07001400bool queue_work_on(int cpu, struct workqueue_struct *wq,
1401 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001402{
Tejun Heod4283e92012-08-03 10:30:44 -07001403 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001404 unsigned long flags;
1405
1406 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001407
Tejun Heo22df02b2010-06-29 10:07:10 +02001408 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001409 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001410 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001411 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001412
1413 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001414 return ret;
1415}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001416EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001417
Tejun Heod8e794d2012-08-03 10:30:45 -07001418void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
David Howells52bad642006-11-22 14:54:01 +00001420 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001422 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001423 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001425EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001427static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1428 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001430 struct timer_list *timer = &dwork->timer;
1431 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001433 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1434 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001435 WARN_ON_ONCE(timer_pending(timer));
1436 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001437
Tejun Heo8852aac2012-12-01 16:23:42 -08001438 /*
1439 * If @delay is 0, queue @dwork->work immediately. This is for
1440 * both optimization and correctness. The earliest @timer can
1441 * expire is on the closest next tick and delayed_work users depend
1442 * on that there's no such delay when @delay is 0.
1443 */
1444 if (!delay) {
1445 __queue_work(cpu, wq, &dwork->work);
1446 return;
1447 }
1448
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001449 timer_stats_timer_set_start_info(&dwork->timer);
1450
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001451 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001452 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001453 timer->expires = jiffies + delay;
1454
1455 if (unlikely(cpu != WORK_CPU_UNBOUND))
1456 add_timer_on(timer, cpu);
1457 else
1458 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001461/**
1462 * queue_delayed_work_on - queue work on specific CPU after delay
1463 * @cpu: CPU number to execute work on
1464 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001465 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001466 * @delay: number of jiffies to wait before queueing
1467 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001468 * Return: %false if @work was already on a queue, %true otherwise. If
Tejun Heo715f1302012-08-03 10:30:46 -07001469 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1470 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001471 */
Tejun Heod4283e92012-08-03 10:30:44 -07001472bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1473 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001474{
David Howells52bad642006-11-22 14:54:01 +00001475 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001476 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001477 unsigned long flags;
1478
1479 /* read the comment in __queue_work() */
1480 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001481
Tejun Heo22df02b2010-06-29 10:07:10 +02001482 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001483 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001484 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001485 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001486
1487 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001488 return ret;
1489}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001490EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Tejun Heoc8e55f32010-06-29 10:07:12 +02001492/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001493 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1494 * @cpu: CPU number to execute work on
1495 * @wq: workqueue to use
1496 * @dwork: work to queue
1497 * @delay: number of jiffies to wait before queueing
1498 *
1499 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1500 * modify @dwork's timer so that it expires after @delay. If @delay is
1501 * zero, @work is guaranteed to be scheduled immediately regardless of its
1502 * current state.
1503 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001504 * Return: %false if @dwork was idle and queued, %true if @dwork was
Tejun Heo8376fe22012-08-03 10:30:47 -07001505 * pending and its timer was modified.
1506 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001507 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001508 * See try_to_grab_pending() for details.
1509 */
1510bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1511 struct delayed_work *dwork, unsigned long delay)
1512{
1513 unsigned long flags;
1514 int ret;
1515
1516 do {
1517 ret = try_to_grab_pending(&dwork->work, true, &flags);
1518 } while (unlikely(ret == -EAGAIN));
1519
1520 if (likely(ret >= 0)) {
1521 __queue_delayed_work(cpu, wq, dwork, delay);
1522 local_irq_restore(flags);
1523 }
1524
1525 /* -ENOENT from try_to_grab_pending() becomes %true */
1526 return ret;
1527}
1528EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1529
1530/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001531 * worker_enter_idle - enter idle state
1532 * @worker: worker which is entering idle state
1533 *
1534 * @worker is entering idle state. Update stats and idle timer if
1535 * necessary.
1536 *
1537 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001538 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001539 */
1540static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001542 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Tejun Heo6183c002013-03-12 11:29:57 -07001544 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1545 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1546 (worker->hentry.next || worker->hentry.pprev)))
1547 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Lai Jiangshan051e1852014-07-22 13:03:02 +08001549 /* can't use worker_set_flags(), also called from create_worker() */
Tejun Heocb444762010-07-02 10:03:50 +02001550 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001551 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001553
Tejun Heoc8e55f32010-06-29 10:07:12 +02001554 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001555 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001556
Tejun Heo628c78e2012-07-17 12:39:27 -07001557 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1558 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001559
Tejun Heo544ecf32012-05-14 15:04:50 -07001560 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001561 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001562 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001563 * nr_running, the warning may trigger spuriously. Check iff
1564 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001565 */
Tejun Heo24647572013-01-24 11:01:33 -08001566 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001567 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001568 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Tejun Heoc8e55f32010-06-29 10:07:12 +02001571/**
1572 * worker_leave_idle - leave idle state
1573 * @worker: worker which is leaving idle state
1574 *
1575 * @worker is leaving idle state. Update stats.
1576 *
1577 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001578 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001579 */
1580static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001582 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Tejun Heo6183c002013-03-12 11:29:57 -07001584 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1585 return;
Tejun Heod302f012010-06-29 10:07:13 +02001586 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001587 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001588 list_del_init(&worker->entry);
1589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001591static struct worker *alloc_worker(int node)
Tejun Heoc34056a2010-06-29 10:07:11 +02001592{
1593 struct worker *worker;
1594
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001595 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001596 if (worker) {
1597 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001598 INIT_LIST_HEAD(&worker->scheduled);
Lai Jiangshanda028462014-05-20 17:46:31 +08001599 INIT_LIST_HEAD(&worker->node);
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 /* on creation a worker is in !idle && prep state */
1601 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001602 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001603 return worker;
1604}
1605
1606/**
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001607 * worker_attach_to_pool() - attach a worker to a pool
1608 * @worker: worker to be attached
1609 * @pool: the target pool
1610 *
1611 * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
1612 * cpu-binding of @worker are kept coordinated with the pool across
1613 * cpu-[un]hotplugs.
1614 */
1615static void worker_attach_to_pool(struct worker *worker,
1616 struct worker_pool *pool)
1617{
1618 mutex_lock(&pool->attach_mutex);
1619
1620 /*
1621 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1622 * online CPUs. It'll be re-applied when any of the CPUs come up.
1623 */
1624 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1625
1626 /*
1627 * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
1628 * stable across this function. See the comments above the
1629 * flag definition for details.
1630 */
1631 if (pool->flags & POOL_DISASSOCIATED)
1632 worker->flags |= WORKER_UNBOUND;
1633
1634 list_add_tail(&worker->node, &pool->workers);
1635
1636 mutex_unlock(&pool->attach_mutex);
1637}
1638
1639/**
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001640 * worker_detach_from_pool() - detach a worker from its pool
1641 * @worker: worker which is attached to its pool
1642 * @pool: the pool @worker is attached to
1643 *
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001644 * Undo the attaching which had been done in worker_attach_to_pool(). The
1645 * caller worker shouldn't access to the pool after detached except it has
1646 * other reference to the pool.
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001647 */
1648static void worker_detach_from_pool(struct worker *worker,
1649 struct worker_pool *pool)
1650{
1651 struct completion *detach_completion = NULL;
1652
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001653 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08001654 list_del(&worker->node);
1655 if (list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001656 detach_completion = pool->detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001657 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001658
Lai Jiangshanb62c0752014-06-03 15:32:52 +08001659 /* clear leftover flags without pool->lock after it is detached */
1660 worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
1661
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001662 if (detach_completion)
1663 complete(detach_completion);
1664}
1665
1666/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001667 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001668 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001669 *
Lai Jiangshan051e1852014-07-22 13:03:02 +08001670 * Create and start a new worker which is attached to @pool.
Tejun Heoc34056a2010-06-29 10:07:11 +02001671 *
1672 * CONTEXT:
1673 * Might sleep. Does GFP_KERNEL allocations.
1674 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001675 * Return:
Tejun Heoc34056a2010-06-29 10:07:11 +02001676 * Pointer to the newly created worker.
1677 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001678static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001679{
Tejun Heoc34056a2010-06-29 10:07:11 +02001680 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001681 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001682 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001683
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001684 /* ID is needed to determine kthread name */
1685 id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL);
Tejun Heo822d8402013-03-19 13:45:21 -07001686 if (id < 0)
1687 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001688
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001689 worker = alloc_worker(pool->node);
Tejun Heoc34056a2010-06-29 10:07:11 +02001690 if (!worker)
1691 goto fail;
1692
Tejun Heobd7bdd42012-07-12 14:46:37 -07001693 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001694 worker->id = id;
1695
Tejun Heo29c91e92013-03-12 11:30:03 -07001696 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001697 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1698 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001699 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001700 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1701
Tejun Heof3f90ad2013-04-01 11:23:34 -07001702 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001703 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001704 if (IS_ERR(worker->task))
1705 goto fail;
1706
Oleg Nesterov91151222013-11-14 12:56:18 +01001707 set_user_nice(worker->task, pool->attrs->nice);
1708
1709 /* prevent userland from meddling with cpumask of workqueue workers */
1710 worker->task->flags |= PF_NO_SETAFFINITY;
1711
Lai Jiangshanda028462014-05-20 17:46:31 +08001712 /* successful, attach the worker to the pool */
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001713 worker_attach_to_pool(worker, pool);
Tejun Heo822d8402013-03-19 13:45:21 -07001714
Lai Jiangshan051e1852014-07-22 13:03:02 +08001715 /* start the newly created worker */
1716 spin_lock_irq(&pool->lock);
1717 worker->pool->nr_workers++;
1718 worker_enter_idle(worker);
1719 wake_up_process(worker->task);
1720 spin_unlock_irq(&pool->lock);
1721
Tejun Heoc34056a2010-06-29 10:07:11 +02001722 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001723
Tejun Heoc34056a2010-06-29 10:07:11 +02001724fail:
Lai Jiangshan9625ab12014-05-20 17:46:27 +08001725 if (id >= 0)
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001726 ida_simple_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001727 kfree(worker);
1728 return NULL;
1729}
1730
1731/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001732 * destroy_worker - destroy a workqueue worker
1733 * @worker: worker to be destroyed
1734 *
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001735 * Destroy @worker and adjust @pool stats accordingly. The worker should
1736 * be idle.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001737 *
1738 * CONTEXT:
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001739 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001740 */
1741static void destroy_worker(struct worker *worker)
1742{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001743 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001744
Tejun Heocd549682013-03-13 19:47:39 -07001745 lockdep_assert_held(&pool->lock);
1746
Tejun Heoc34056a2010-06-29 10:07:11 +02001747 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001748 if (WARN_ON(worker->current_work) ||
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001749 WARN_ON(!list_empty(&worker->scheduled)) ||
1750 WARN_ON(!(worker->flags & WORKER_IDLE)))
Tejun Heo6183c002013-03-12 11:29:57 -07001751 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001752
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001753 pool->nr_workers--;
1754 pool->nr_idle--;
Lai Jiangshan5bdfff92014-02-15 22:02:28 +08001755
Tejun Heoc8e55f32010-06-29 10:07:12 +02001756 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001757 worker->flags |= WORKER_DIE;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001758 wake_up_process(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001759}
1760
Tejun Heo63d95a92012-07-12 14:46:37 -07001761static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001762{
Tejun Heo63d95a92012-07-12 14:46:37 -07001763 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001764
Tejun Heod565ed62013-01-24 11:01:33 -08001765 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001766
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001767 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001768 struct worker *worker;
1769 unsigned long expires;
1770
1771 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001772 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001773 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1774
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001775 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001776 mod_timer(&pool->idle_timer, expires);
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001777 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02001778 }
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001779
1780 destroy_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02001781 }
1782
Tejun Heod565ed62013-01-24 11:01:33 -08001783 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001784}
1785
Tejun Heo493a1722013-03-12 11:29:59 -07001786static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001787{
Tejun Heo112202d2013-02-13 19:29:12 -08001788 struct pool_workqueue *pwq = get_work_pwq(work);
1789 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001790
Tejun Heo2e109a22013-03-13 19:47:40 -07001791 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001792
Tejun Heo493008a2013-03-12 11:30:03 -07001793 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001794 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001795
1796 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001797 if (list_empty(&pwq->mayday_node)) {
Lai Jiangshan77668c82014-04-18 11:04:16 -04001798 /*
1799 * If @pwq is for an unbound wq, its base ref may be put at
1800 * any time due to an attribute change. Pin @pwq until the
1801 * rescuer is done with it.
1802 */
1803 get_pwq(pwq);
Tejun Heo493a1722013-03-12 11:29:59 -07001804 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001805 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001806 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001807}
1808
Tejun Heo706026c2013-01-24 11:01:34 -08001809static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001810{
Tejun Heo63d95a92012-07-12 14:46:37 -07001811 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001812 struct work_struct *work;
1813
Tejun Heob2d82902014-12-08 12:39:16 -05001814 spin_lock_irq(&pool->lock);
1815 spin_lock(&wq_mayday_lock); /* for wq->maydays */
Tejun Heoe22bee72010-06-29 10:07:14 +02001816
Tejun Heo63d95a92012-07-12 14:46:37 -07001817 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001818 /*
1819 * We've been trying to create a new worker but
1820 * haven't been successful. We might be hitting an
1821 * allocation deadlock. Send distress signals to
1822 * rescuers.
1823 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001824 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001825 send_mayday(work);
1826 }
1827
Tejun Heob2d82902014-12-08 12:39:16 -05001828 spin_unlock(&wq_mayday_lock);
1829 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001830
Tejun Heo63d95a92012-07-12 14:46:37 -07001831 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001832}
1833
1834/**
1835 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001836 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001837 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001838 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001839 * have at least one idle worker on return from this function. If
1840 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001841 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 * possible allocation deadlock.
1843 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001844 * On return, need_to_create_worker() is guaranteed to be %false and
1845 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001846 *
1847 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001848 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001849 * multiple times. Does GFP_KERNEL allocations. Called only from
1850 * manager.
Tejun Heoe22bee72010-06-29 10:07:14 +02001851 */
Tejun Heo29187a92015-01-16 14:21:16 -05001852static void maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001853__releases(&pool->lock)
1854__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001855{
Tejun Heoe22bee72010-06-29 10:07:14 +02001856restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001857 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001858
Tejun Heoe22bee72010-06-29 10:07:14 +02001859 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001860 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001861
1862 while (true) {
Lai Jiangshan051e1852014-07-22 13:03:02 +08001863 if (create_worker(pool) || !need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 break;
1865
Lai Jiangshane212f362014-06-03 15:32:17 +08001866 schedule_timeout_interruptible(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001867
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 break;
1870 }
1871
Tejun Heo63d95a92012-07-12 14:46:37 -07001872 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001873 spin_lock_irq(&pool->lock);
Lai Jiangshan051e1852014-07-22 13:03:02 +08001874 /*
1875 * This is necessary even after a new worker was just successfully
1876 * created as @pool->lock was dropped and the new worker might have
1877 * already become busy.
1878 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001879 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001880 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001881}
1882
1883/**
Tejun Heoe22bee72010-06-29 10:07:14 +02001884 * manage_workers - manage worker pool
1885 * @worker: self
1886 *
Tejun Heo706026c2013-01-24 11:01:34 -08001887 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001889 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001890 *
1891 * The caller can safely start processing works on false return. On
1892 * true return, it's guaranteed that need_to_create_worker() is false
1893 * and may_start_working() is true.
1894 *
1895 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001896 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001897 * multiple times. Does GFP_KERNEL allocations.
1898 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001899 * Return:
Tejun Heo29187a92015-01-16 14:21:16 -05001900 * %false if the pool doesn't need management and the caller can safely
1901 * start processing works, %true if management function was performed and
1902 * the conditions that the caller verified before calling the function may
1903 * no longer be true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001904 */
1905static bool manage_workers(struct worker *worker)
1906{
Tejun Heo63d95a92012-07-12 14:46:37 -07001907 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001908
Tejun Heobc3a1af2013-03-13 19:47:39 -07001909 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07001910 * Anyone who successfully grabs manager_arb wins the arbitration
1911 * and becomes the manager. mutex_trylock() on pool->manager_arb
1912 * failure while holding pool->lock reliably indicates that someone
1913 * else is managing the pool and the worker which failed trylock
1914 * can proceed to executing work items. This means that anyone
1915 * grabbing manager_arb is responsible for actually performing
1916 * manager duties. If manager_arb is grabbed and released without
1917 * actual management, the pool may stall indefinitely.
Tejun Heobc3a1af2013-03-13 19:47:39 -07001918 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07001919 if (!mutex_trylock(&pool->manager_arb))
Tejun Heo29187a92015-01-16 14:21:16 -05001920 return false;
Tejun Heoe22bee72010-06-29 10:07:14 +02001921
Tejun Heo29187a92015-01-16 14:21:16 -05001922 maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001923
Tejun Heo34a06bd2013-03-12 11:30:00 -07001924 mutex_unlock(&pool->manager_arb);
Tejun Heo29187a92015-01-16 14:21:16 -05001925 return true;
Tejun Heoe22bee72010-06-29 10:07:14 +02001926}
1927
Tejun Heoa62428c2010-06-29 10:07:10 +02001928/**
1929 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001930 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 * @work: work to process
1932 *
1933 * Process @work. This function contains all the logics necessary to
1934 * process a single work including synchronization against and
1935 * interaction with other workers on the same cpu, queueing and
1936 * flushing. As long as context requirement is met, any worker can
1937 * call this function to process a work.
1938 *
1939 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001940 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001941 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001942static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08001943__releases(&pool->lock)
1944__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001945{
Tejun Heo112202d2013-02-13 19:29:12 -08001946 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001947 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001948 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001949 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001950 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001951#ifdef CONFIG_LOCKDEP
1952 /*
1953 * It is permissible to free the struct work_struct from
1954 * inside the function that is called from it, this we need to
1955 * take into account for lockdep too. To avoid bogus "held
1956 * lock freed" warnings as well as problems when looking into
1957 * work->lockdep_map, make a copy and use that here.
1958 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001959 struct lockdep_map lockdep_map;
1960
1961 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001962#endif
Lai Jiangshan807407c2014-06-03 15:33:28 +08001963 /* ensure we're on the correct CPU */
Lai Jiangshan85327af2014-06-03 15:33:28 +08001964 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08001965 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07001966
Tejun Heo7e116292010-06-29 10:07:13 +02001967 /*
1968 * A single work shouldn't be executed concurrently by
1969 * multiple workers on a single cpu. Check whether anyone is
1970 * already processing the work. If so, defer the work to the
1971 * currently executing one.
1972 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001973 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02001974 if (unlikely(collision)) {
1975 move_linked_works(work, &collision->scheduled, NULL);
1976 return;
1977 }
1978
Tejun Heo8930cab2012-08-03 10:30:45 -07001979 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02001980 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001981 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02001982 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08001983 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08001984 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001985 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001986
Tejun Heoa62428c2010-06-29 10:07:10 +02001987 list_del_init(&work->entry);
1988
Tejun Heo649027d2010-06-29 10:07:14 +02001989 /*
Lai Jiangshan228f1d02014-07-22 13:02:00 +08001990 * CPU intensive works don't participate in concurrency management.
1991 * They're the scheduler's responsibility. This takes @worker out
1992 * of concurrency management and the next code block will chain
1993 * execution of the pending work items.
Tejun Heofb0e7be2010-06-29 10:07:15 +02001994 */
1995 if (unlikely(cpu_intensive))
Lai Jiangshan228f1d02014-07-22 13:02:00 +08001996 worker_set_flags(worker, WORKER_CPU_INTENSIVE);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001997
Tejun Heo974271c2012-07-12 14:46:37 -07001998 /*
Lai Jiangshana489a032014-07-22 13:01:59 +08001999 * Wake up another worker if necessary. The condition is always
2000 * false for normal per-cpu workers since nr_running would always
2001 * be >= 1 at this point. This is used to chain execution of the
2002 * pending work items for WORKER_NOT_RUNNING workers such as the
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002003 * UNBOUND and CPU_INTENSIVE ones.
Tejun Heo974271c2012-07-12 14:46:37 -07002004 */
Lai Jiangshana489a032014-07-22 13:01:59 +08002005 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002006 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002007
Tejun Heo8930cab2012-08-03 10:30:45 -07002008 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002009 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002010 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002011 * PENDING and queued state changes happen together while IRQ is
2012 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002013 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002014 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002015
Tejun Heod565ed62013-01-24 11:01:33 -08002016 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002017
Tejun Heo112202d2013-02-13 19:29:12 -08002018 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002019 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002020 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002021 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002022 /*
2023 * While we must be careful to not use "work" after this, the trace
2024 * point will only record its address.
2025 */
2026 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002027 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002028 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002029
2030 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002031 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2032 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002033 current->comm, preempt_count(), task_pid_nr(current),
2034 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002035 debug_show_held_locks(current);
2036 dump_stack();
2037 }
2038
Tejun Heob22ce272013-08-28 17:33:37 -04002039 /*
2040 * The following prevents a kworker from hogging CPU on !PREEMPT
2041 * kernels, where a requeueing work item waiting for something to
2042 * happen could deadlock with stop_machine as such work item could
2043 * indefinitely requeue itself while all other CPUs are trapped in
Joe Lawrence789cbbe2014-10-05 13:24:21 -04002044 * stop_machine. At the same time, report a quiescent RCU state so
2045 * the same condition doesn't freeze RCU.
Tejun Heob22ce272013-08-28 17:33:37 -04002046 */
Joe Lawrence3e28e372014-10-05 13:24:22 -04002047 cond_resched_rcu_qs();
Tejun Heob22ce272013-08-28 17:33:37 -04002048
Tejun Heod565ed62013-01-24 11:01:33 -08002049 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002050
Tejun Heofb0e7be2010-06-29 10:07:15 +02002051 /* clear cpu intensive status */
2052 if (unlikely(cpu_intensive))
2053 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2054
Tejun Heoa62428c2010-06-29 10:07:10 +02002055 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002056 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002057 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002058 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002059 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002060 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002061 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002062}
2063
Tejun Heoaffee4b2010-06-29 10:07:12 +02002064/**
2065 * process_scheduled_works - process scheduled works
2066 * @worker: self
2067 *
2068 * Process all scheduled works. Please note that the scheduled list
2069 * may change while processing a work, so this function repeatedly
2070 * fetches a work from the top and executes it.
2071 *
2072 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002073 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002074 * multiple times.
2075 */
2076static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002078 while (!list_empty(&worker->scheduled)) {
2079 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002081 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Tejun Heo4690c4a2010-06-29 10:07:10 +02002085/**
2086 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002087 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002088 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002089 * The worker thread function. All workers belong to a worker_pool -
2090 * either a per-cpu one or dynamic unbound one. These workers process all
2091 * work items regardless of their specific target workqueue. The only
2092 * exception is work items which belong to workqueues with a rescuer which
2093 * will be explained in rescuer_thread().
Yacine Belkadid185af32013-07-31 14:59:24 -07002094 *
2095 * Return: 0
Tejun Heo4690c4a2010-06-29 10:07:10 +02002096 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002097static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Tejun Heoc34056a2010-06-29 10:07:11 +02002099 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002100 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Tejun Heoe22bee72010-06-29 10:07:14 +02002102 /* tell the scheduler that this is a workqueue worker */
2103 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002104woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002105 spin_lock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002106
Tejun Heoa9ab7752013-03-19 13:45:21 -07002107 /* am I supposed to die? */
2108 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002109 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002110 WARN_ON_ONCE(!list_empty(&worker->entry));
2111 worker->task->flags &= ~PF_WQ_WORKER;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002112
2113 set_task_comm(worker->task, "kworker/dying");
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08002114 ida_simple_remove(&pool->worker_ida, worker->id);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002115 worker_detach_from_pool(worker, pool);
2116 kfree(worker);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002119
Tejun Heoc8e55f32010-06-29 10:07:12 +02002120 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002121recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002122 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002123 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002124 goto sleep;
2125
2126 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002127 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002128 goto recheck;
2129
Tejun Heoc8e55f32010-06-29 10:07:12 +02002130 /*
2131 * ->scheduled list can only be filled while a worker is
2132 * preparing to process a work or actually processing it.
2133 * Make sure nobody diddled with it while I was sleeping.
2134 */
Tejun Heo6183c002013-03-12 11:29:57 -07002135 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002136
Tejun Heoe22bee72010-06-29 10:07:14 +02002137 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002138 * Finish PREP stage. We're guaranteed to have at least one idle
2139 * worker or that someone else has already assumed the manager
2140 * role. This is where @worker starts participating in concurrency
2141 * management if applicable and concurrency management is restored
2142 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002143 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002144 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002145
2146 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002147 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002148 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002149 struct work_struct, entry);
2150
2151 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2152 /* optimization path, not strictly necessary */
2153 process_one_work(worker, work);
2154 if (unlikely(!list_empty(&worker->scheduled)))
2155 process_scheduled_works(worker);
2156 } else {
2157 move_linked_works(work, &worker->scheduled, NULL);
2158 process_scheduled_works(worker);
2159 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002160 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002161
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002162 worker_set_flags(worker, WORKER_PREP);
Tejun Heod313dd82010-07-02 10:03:51 +02002163sleep:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002164 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002165 * pool->lock is held and there's no work to process and no need to
2166 * manage, sleep. Workers are woken up only while holding
2167 * pool->lock or from local cpu, so setting the current state
2168 * before releasing pool->lock is enough to prevent losing any
2169 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002170 */
2171 worker_enter_idle(worker);
2172 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002173 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002174 schedule();
2175 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Tejun Heoe22bee72010-06-29 10:07:14 +02002178/**
2179 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002180 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002181 *
2182 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002183 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002184 *
Tejun Heo706026c2013-01-24 11:01:34 -08002185 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002186 * worker which uses GFP_KERNEL allocation which has slight chance of
2187 * developing into deadlock if some works currently on the same queue
2188 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2189 * the problem rescuer solves.
2190 *
Tejun Heo706026c2013-01-24 11:01:34 -08002191 * When such condition is possible, the pool summons rescuers of all
2192 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002193 * those works so that forward progress can be guaranteed.
2194 *
2195 * This should happen rarely.
Yacine Belkadid185af32013-07-31 14:59:24 -07002196 *
2197 * Return: 0
Tejun Heoe22bee72010-06-29 10:07:14 +02002198 */
Tejun Heo111c2252013-01-17 17:16:24 -08002199static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002200{
Tejun Heo111c2252013-01-17 17:16:24 -08002201 struct worker *rescuer = __rescuer;
2202 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002203 struct list_head *scheduled = &rescuer->scheduled;
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002204 bool should_stop;
Tejun Heoe22bee72010-06-29 10:07:14 +02002205
2206 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002207
2208 /*
2209 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2210 * doesn't participate in concurrency management.
2211 */
2212 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002213repeat:
2214 set_current_state(TASK_INTERRUPTIBLE);
2215
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002216 /*
2217 * By the time the rescuer is requested to stop, the workqueue
2218 * shouldn't have any work pending, but @wq->maydays may still have
2219 * pwq(s) queued. This can happen by non-rescuer workers consuming
2220 * all the work items before the rescuer got to them. Go through
2221 * @wq->maydays processing before acting on should_stop so that the
2222 * list is always empty on exit.
2223 */
2224 should_stop = kthread_should_stop();
Tejun Heoe22bee72010-06-29 10:07:14 +02002225
Tejun Heo493a1722013-03-12 11:29:59 -07002226 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002227 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002228
2229 while (!list_empty(&wq->maydays)) {
2230 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2231 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002232 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002233 struct work_struct *work, *n;
2234
2235 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002236 list_del_init(&pwq->mayday_node);
2237
Tejun Heo2e109a22013-03-13 19:47:40 -07002238 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002239
Lai Jiangshan51697d392014-05-20 17:46:36 +08002240 worker_attach_to_pool(rescuer, pool);
2241
2242 spin_lock_irq(&pool->lock);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002243 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002244
2245 /*
2246 * Slurp in all works issued via this workqueue and
2247 * process'em.
2248 */
Tejun Heo0479c8c2014-12-04 10:14:13 -05002249 WARN_ON_ONCE(!list_empty(scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002250 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002251 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002252 move_linked_works(work, scheduled, &n);
2253
NeilBrown008847f2014-12-08 12:39:16 -05002254 if (!list_empty(scheduled)) {
2255 process_scheduled_works(rescuer);
2256
2257 /*
2258 * The above execution of rescued work items could
2259 * have created more to rescue through
2260 * pwq_activate_first_delayed() or chained
2261 * queueing. Let's put @pwq back on mayday list so
2262 * that such back-to-back work items, which may be
2263 * being used to relieve memory pressure, don't
2264 * incur MAYDAY_INTERVAL delay inbetween.
2265 */
2266 if (need_to_create_worker(pool)) {
2267 spin_lock(&wq_mayday_lock);
2268 get_pwq(pwq);
2269 list_move_tail(&pwq->mayday_node, &wq->maydays);
2270 spin_unlock(&wq_mayday_lock);
2271 }
2272 }
Tejun Heo75769582011-02-14 14:04:46 +01002273
2274 /*
Lai Jiangshan77668c82014-04-18 11:04:16 -04002275 * Put the reference grabbed by send_mayday(). @pool won't
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002276 * go away while we're still attached to it.
Lai Jiangshan77668c82014-04-18 11:04:16 -04002277 */
2278 put_pwq(pwq);
2279
2280 /*
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002281 * Leave this pool. If need_more_worker() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002282 * regular worker; otherwise, we end up with 0 concurrency
2283 * and stalling the execution.
2284 */
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002285 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002286 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002287
Lai Jiangshanb3104102013-02-19 12:17:02 -08002288 rescuer->pool = NULL;
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002289 spin_unlock_irq(&pool->lock);
2290
2291 worker_detach_from_pool(rescuer, pool);
2292
2293 spin_lock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002294 }
2295
Tejun Heo2e109a22013-03-13 19:47:40 -07002296 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002297
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002298 if (should_stop) {
2299 __set_current_state(TASK_RUNNING);
2300 rescuer->task->flags &= ~PF_WQ_WORKER;
2301 return 0;
2302 }
2303
Tejun Heo111c2252013-01-17 17:16:24 -08002304 /* rescuers should never participate in concurrency management */
2305 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002306 schedule();
2307 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002310struct wq_barrier {
2311 struct work_struct work;
2312 struct completion done;
2313};
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);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002361
Tejun Heoaffee4b2010-06-29 10:07:12 +02002362 /*
2363 * If @target is currently being executed, schedule the
2364 * barrier to the worker; otherwise, put it after @target.
2365 */
2366 if (worker)
2367 head = worker->scheduled.next;
2368 else {
2369 unsigned long *bits = work_data_bits(target);
2370
2371 head = target->entry.next;
2372 /* there can already be other linked works, inherit and set */
2373 linked = *bits & WORK_STRUCT_LINKED;
2374 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2375 }
2376
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002377 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002378 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002379 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002380}
2381
Tejun Heo73f53c42010-06-29 10:07:11 +02002382/**
Tejun Heo112202d2013-02-13 19:29:12 -08002383 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002384 * @wq: workqueue being flushed
2385 * @flush_color: new flush color, < 0 for no-op
2386 * @work_color: new work color, < 0 for no-op
2387 *
Tejun Heo112202d2013-02-13 19:29:12 -08002388 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002389 *
Tejun Heo112202d2013-02-13 19:29:12 -08002390 * If @flush_color is non-negative, flush_color on all pwqs should be
2391 * -1. If no pwq has in-flight commands at the specified color, all
2392 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2393 * has in flight commands, its pwq->flush_color is set to
2394 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002395 * wakeup logic is armed and %true is returned.
2396 *
2397 * The caller should have initialized @wq->first_flusher prior to
2398 * calling this function with non-negative @flush_color. If
2399 * @flush_color is negative, no flush color update is done and %false
2400 * is returned.
2401 *
Tejun Heo112202d2013-02-13 19:29:12 -08002402 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002403 * work_color which is previous to @work_color and all will be
2404 * advanced to @work_color.
2405 *
2406 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002407 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002408 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002409 * Return:
Tejun Heo73f53c42010-06-29 10:07:11 +02002410 * %true if @flush_color >= 0 and there's something to flush. %false
2411 * otherwise.
2412 */
Tejun Heo112202d2013-02-13 19:29:12 -08002413static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002414 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Tejun Heo73f53c42010-06-29 10:07:11 +02002416 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002417 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002418
Tejun Heo73f53c42010-06-29 10:07:11 +02002419 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002420 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002421 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002422 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002423
Tejun Heo49e3cf42013-03-12 11:29:58 -07002424 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002425 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002426
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002427 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002428
2429 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002430 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002431
Tejun Heo112202d2013-02-13 19:29:12 -08002432 if (pwq->nr_in_flight[flush_color]) {
2433 pwq->flush_color = flush_color;
2434 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002435 wait = true;
2436 }
2437 }
2438
2439 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002440 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002441 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002442 }
2443
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002444 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002445 }
2446
Tejun Heo112202d2013-02-13 19:29:12 -08002447 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002448 complete(&wq->first_flusher->done);
2449
2450 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002453/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002455 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002457 * This function sleeps until all work items which were queued on entry
2458 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002460void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Tejun Heo73f53c42010-06-29 10:07:11 +02002462 struct wq_flusher this_flusher = {
2463 .list = LIST_HEAD_INIT(this_flusher.list),
2464 .flush_color = -1,
2465 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2466 };
2467 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002468
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002469 lock_map_acquire(&wq->lockdep_map);
2470 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002471
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002472 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002473
2474 /*
2475 * Start-to-wait phase
2476 */
2477 next_color = work_next_color(wq->work_color);
2478
2479 if (next_color != wq->flush_color) {
2480 /*
2481 * Color space is not full. The current work_color
2482 * becomes our flush_color and work_color is advanced
2483 * by one.
2484 */
Tejun Heo6183c002013-03-12 11:29:57 -07002485 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002486 this_flusher.flush_color = wq->work_color;
2487 wq->work_color = next_color;
2488
2489 if (!wq->first_flusher) {
2490 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002491 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002492
2493 wq->first_flusher = &this_flusher;
2494
Tejun Heo112202d2013-02-13 19:29:12 -08002495 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002496 wq->work_color)) {
2497 /* nothing to flush, done */
2498 wq->flush_color = next_color;
2499 wq->first_flusher = NULL;
2500 goto out_unlock;
2501 }
2502 } else {
2503 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002504 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002506 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002507 }
2508 } else {
2509 /*
2510 * Oops, color space is full, wait on overflow queue.
2511 * The next flush completion will assign us
2512 * flush_color and transfer to flusher_queue.
2513 */
2514 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2515 }
2516
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002517 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002518
2519 wait_for_completion(&this_flusher.done);
2520
2521 /*
2522 * Wake-up-and-cascade phase
2523 *
2524 * First flushers are responsible for cascading flushes and
2525 * handling overflow. Non-first flushers can simply return.
2526 */
2527 if (wq->first_flusher != &this_flusher)
2528 return;
2529
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002530 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002531
Tejun Heo4ce48b32010-07-02 10:03:51 +02002532 /* we might have raced, check again with mutex held */
2533 if (wq->first_flusher != &this_flusher)
2534 goto out_unlock;
2535
Tejun Heo73f53c42010-06-29 10:07:11 +02002536 wq->first_flusher = NULL;
2537
Tejun Heo6183c002013-03-12 11:29:57 -07002538 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2539 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002540
2541 while (true) {
2542 struct wq_flusher *next, *tmp;
2543
2544 /* complete all the flushers sharing the current flush color */
2545 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2546 if (next->flush_color != wq->flush_color)
2547 break;
2548 list_del_init(&next->list);
2549 complete(&next->done);
2550 }
2551
Tejun Heo6183c002013-03-12 11:29:57 -07002552 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2553 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002554
2555 /* this flush_color is finished, advance by one */
2556 wq->flush_color = work_next_color(wq->flush_color);
2557
2558 /* one color has been freed, handle overflow queue */
2559 if (!list_empty(&wq->flusher_overflow)) {
2560 /*
2561 * Assign the same color to all overflowed
2562 * flushers, advance work_color and append to
2563 * flusher_queue. This is the start-to-wait
2564 * phase for these overflowed flushers.
2565 */
2566 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2567 tmp->flush_color = wq->work_color;
2568
2569 wq->work_color = work_next_color(wq->work_color);
2570
2571 list_splice_tail_init(&wq->flusher_overflow,
2572 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002573 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002574 }
2575
2576 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002577 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578 break;
2579 }
2580
2581 /*
2582 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002583 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002584 */
Tejun Heo6183c002013-03-12 11:29:57 -07002585 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2586 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002587
2588 list_del_init(&next->list);
2589 wq->first_flusher = next;
2590
Tejun Heo112202d2013-02-13 19:29:12 -08002591 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002592 break;
2593
2594 /*
2595 * Meh... this color is already done, clear first
2596 * flusher and repeat cascading.
2597 */
2598 wq->first_flusher = NULL;
2599 }
2600
2601out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002602 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
Dave Jonesae90dd52006-06-30 01:40:45 -04002604EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002606/**
2607 * drain_workqueue - drain a workqueue
2608 * @wq: workqueue to drain
2609 *
2610 * Wait until the workqueue becomes empty. While draining is in progress,
2611 * only chain queueing is allowed. IOW, only currently pending or running
2612 * work items on @wq can queue further work items on it. @wq is flushed
2613 * repeatedly until it becomes empty. The number of flushing is detemined
2614 * by the depth of chaining and should be relatively short. Whine if it
2615 * takes too long.
2616 */
2617void drain_workqueue(struct workqueue_struct *wq)
2618{
2619 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002620 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002621
2622 /*
2623 * __queue_work() needs to test whether there are drainers, is much
2624 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002625 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002626 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002627 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002628 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002629 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002630 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002631reflush:
2632 flush_workqueue(wq);
2633
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002634 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002635
Tejun Heo49e3cf42013-03-12 11:29:58 -07002636 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002637 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002638
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002639 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002640 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002641 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002642
2643 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002644 continue;
2645
2646 if (++flush_cnt == 10 ||
2647 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002648 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002649 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002650
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002651 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002652 goto reflush;
2653 }
2654
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002655 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002656 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002657 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002658}
2659EXPORT_SYMBOL_GPL(drain_workqueue);
2660
Tejun Heo606a5022012-08-20 14:51:23 -07002661static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002662{
2663 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002664 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002665 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002666
2667 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002668
Tejun Heofa1b54e2013-03-12 11:30:00 -07002669 local_irq_disable();
2670 pool = get_work_pool(work);
2671 if (!pool) {
2672 local_irq_enable();
2673 return false;
2674 }
2675
2676 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002677 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002678 pwq = get_work_pwq(work);
2679 if (pwq) {
2680 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002681 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002682 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002683 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002684 if (!worker)
2685 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002686 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002687 }
Tejun Heobaf59022010-09-16 10:42:16 +02002688
Tejun Heo112202d2013-02-13 19:29:12 -08002689 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002690 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002691
Tejun Heoe1594892011-01-09 23:32:15 +01002692 /*
2693 * If @max_active is 1 or rescuer is in use, flushing another work
2694 * item on the same workqueue may lead to deadlock. Make sure the
2695 * flusher is not running on the same workqueue by verifying write
2696 * access.
2697 */
Tejun Heo493008a2013-03-12 11:30:03 -07002698 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002699 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002700 else
Tejun Heo112202d2013-02-13 19:29:12 -08002701 lock_map_acquire_read(&pwq->wq->lockdep_map);
2702 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002703
Tejun Heobaf59022010-09-16 10:42:16 +02002704 return true;
2705already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002706 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002707 return false;
2708}
2709
Oleg Nesterovdb700892008-07-25 01:47:49 -07002710/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002711 * flush_work - wait for a work to finish executing the last queueing instance
2712 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002713 *
Tejun Heo606a5022012-08-20 14:51:23 -07002714 * Wait until @work has finished execution. @work is guaranteed to be idle
2715 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002716 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002717 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002718 * %true if flush_work() waited for the work to finish execution,
2719 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002720 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002721bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002722{
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002723 struct wq_barrier barr;
2724
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002725 lock_map_acquire(&work->lockdep_map);
2726 lock_map_release(&work->lockdep_map);
2727
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002728 if (start_flush_work(work, &barr)) {
2729 wait_for_completion(&barr.done);
2730 destroy_work_on_stack(&barr.work);
2731 return true;
2732 } else {
2733 return false;
2734 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002735}
2736EXPORT_SYMBOL_GPL(flush_work);
2737
Tejun Heo8603e1b32015-03-05 08:04:13 -05002738struct cwt_wait {
2739 wait_queue_t wait;
2740 struct work_struct *work;
2741};
2742
2743static int cwt_wakefn(wait_queue_t *wait, unsigned mode, int sync, void *key)
2744{
2745 struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait);
2746
2747 if (cwait->work != key)
2748 return 0;
2749 return autoremove_wake_function(wait, mode, sync, key);
2750}
2751
Tejun Heo36e227d2012-08-03 10:30:46 -07002752static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002753{
Tejun Heo8603e1b32015-03-05 08:04:13 -05002754 static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq);
Tejun Heobbb68df2012-08-03 10:30:46 -07002755 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002756 int ret;
2757
2758 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002759 ret = try_to_grab_pending(work, is_dwork, &flags);
2760 /*
Tejun Heo8603e1b32015-03-05 08:04:13 -05002761 * If someone else is already canceling, wait for it to
2762 * finish. flush_work() doesn't work for PREEMPT_NONE
2763 * because we may get scheduled between @work's completion
2764 * and the other canceling task resuming and clearing
2765 * CANCELING - flush_work() will return false immediately
2766 * as @work is no longer busy, try_to_grab_pending() will
2767 * return -ENOENT as @work is still being canceled and the
2768 * other canceling task won't be able to clear CANCELING as
2769 * we're hogging the CPU.
2770 *
2771 * Let's wait for completion using a waitqueue. As this
2772 * may lead to the thundering herd problem, use a custom
2773 * wake function which matches @work along with exclusive
2774 * wait and wakeup.
Tejun Heobbb68df2012-08-03 10:30:46 -07002775 */
Tejun Heo8603e1b32015-03-05 08:04:13 -05002776 if (unlikely(ret == -ENOENT)) {
2777 struct cwt_wait cwait;
2778
2779 init_wait(&cwait.wait);
2780 cwait.wait.func = cwt_wakefn;
2781 cwait.work = work;
2782
2783 prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait,
2784 TASK_UNINTERRUPTIBLE);
2785 if (work_is_canceling(work))
2786 schedule();
2787 finish_wait(&cancel_waitq, &cwait.wait);
2788 }
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002789 } while (unlikely(ret < 0));
2790
Tejun Heobbb68df2012-08-03 10:30:46 -07002791 /* tell other tasks trying to grab @work to back off */
2792 mark_work_canceling(work);
2793 local_irq_restore(flags);
2794
Tejun Heo606a5022012-08-20 14:51:23 -07002795 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002796 clear_work_data(work);
Tejun Heo8603e1b32015-03-05 08:04:13 -05002797
2798 /*
2799 * Paired with prepare_to_wait() above so that either
2800 * waitqueue_active() is visible here or !work_is_canceling() is
2801 * visible there.
2802 */
2803 smp_mb();
2804 if (waitqueue_active(&cancel_waitq))
2805 __wake_up(&cancel_waitq, TASK_NORMAL, 1, work);
2806
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002807 return ret;
2808}
2809
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002810/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002811 * cancel_work_sync - cancel a work and wait for it to finish
2812 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002813 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002814 * Cancel @work and wait for its execution to finish. This function
2815 * can be used even if the work re-queues itself or migrates to
2816 * another workqueue. On return from this function, @work is
2817 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002818 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002819 * cancel_work_sync(&delayed_work->work) must not be used for
2820 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002821 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002822 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002823 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002824 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002825 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002826 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002828bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002829{
Tejun Heo36e227d2012-08-03 10:30:46 -07002830 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002831}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002832EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002833
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002834/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002835 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2836 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002837 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002838 * Delayed timer is cancelled and the pending work is queued for
2839 * immediate execution. Like flush_work(), this function only
2840 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002841 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002842 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002843 * %true if flush_work() waited for the work to finish execution,
2844 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002845 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002846bool flush_delayed_work(struct delayed_work *dwork)
2847{
Tejun Heo8930cab2012-08-03 10:30:45 -07002848 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002849 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002850 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002851 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002852 return flush_work(&dwork->work);
2853}
2854EXPORT_SYMBOL(flush_delayed_work);
2855
2856/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002857 * cancel_delayed_work - cancel a delayed work
2858 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002859 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002860 * Kill off a pending delayed_work.
2861 *
2862 * Return: %true if @dwork was pending and canceled; %false if it wasn't
2863 * pending.
2864 *
2865 * Note:
2866 * The work callback function may still be running on return, unless
2867 * it returns %true and the work doesn't re-arm itself. Explicitly flush or
2868 * use cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002869 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002870 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002871 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002872bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002873{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002874 unsigned long flags;
2875 int ret;
2876
2877 do {
2878 ret = try_to_grab_pending(&dwork->work, true, &flags);
2879 } while (unlikely(ret == -EAGAIN));
2880
2881 if (unlikely(ret < 0))
2882 return false;
2883
Tejun Heo7c3eed52013-01-24 11:01:33 -08002884 set_work_pool_and_clear_pending(&dwork->work,
2885 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002886 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002887 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002888}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002889EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002890
2891/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2893 * @dwork: the delayed work cancel
2894 *
2895 * This is cancel_work_sync() for delayed works.
2896 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002897 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002898 * %true if @dwork was pending, %false otherwise.
2899 */
2900bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002901{
Tejun Heo36e227d2012-08-03 10:30:46 -07002902 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002904EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002906/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002907 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002908 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002909 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002910 * schedule_on_each_cpu() executes @func on each online CPU using the
2911 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002912 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002913 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002914 * Return:
Tejun Heo31ddd872010-10-19 11:14:49 +02002915 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002916 */
David Howells65f27f32006-11-22 14:55:48 +00002917int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002918{
2919 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002920 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002921
Andrew Mortonb6136772006-06-25 05:47:49 -07002922 works = alloc_percpu(struct work_struct);
2923 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002924 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002925
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002926 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002927
Christoph Lameter15316ba2006-01-08 01:00:43 -08002928 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002929 struct work_struct *work = per_cpu_ptr(works, cpu);
2930
2931 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002932 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002933 }
Tejun Heo93981802009-11-17 14:06:20 -08002934
2935 for_each_online_cpu(cpu)
2936 flush_work(per_cpu_ptr(works, cpu));
2937
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002938 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002939 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002940 return 0;
2941}
2942
Alan Sterneef6a7d2010-02-12 17:39:21 +09002943/**
2944 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2945 *
2946 * Forces execution of the kernel-global workqueue and blocks until its
2947 * completion.
2948 *
2949 * Think twice before calling this function! It's very easy to get into
2950 * trouble if you don't take great care. Either of the following situations
2951 * will lead to deadlock:
2952 *
2953 * One of the work items currently on the workqueue needs to acquire
2954 * a lock held by your code or its caller.
2955 *
2956 * Your code is running in the context of a work routine.
2957 *
2958 * They will be detected by lockdep when they occur, but the first might not
2959 * occur very often. It depends on what work items are on the workqueue and
2960 * what locks they need, which you have no control over.
2961 *
2962 * In most situations flushing the entire workqueue is overkill; you merely
2963 * need to know that a particular work item isn't queued and isn't running.
2964 * In such cases you should use cancel_delayed_work_sync() or
2965 * cancel_work_sync() instead.
2966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967void flush_scheduled_work(void)
2968{
Tejun Heod320c032010-06-29 10:07:14 +02002969 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
Dave Jonesae90dd52006-06-30 01:40:45 -04002971EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002974 * execute_in_process_context - reliably execute the routine with user context
2975 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002976 * @ew: guaranteed storage for the execute work structure (must
2977 * be available when the work executes)
2978 *
2979 * Executes the function immediately if process context is available,
2980 * otherwise schedules the function for delayed execution.
2981 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002982 * Return: 0 - function was executed
James Bottomley1fa44ec2006-02-23 12:43:43 -06002983 * 1 - function was scheduled for execution
2984 */
David Howells65f27f32006-11-22 14:55:48 +00002985int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002986{
2987 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002988 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002989 return 0;
2990 }
2991
David Howells65f27f32006-11-22 14:55:48 +00002992 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002993 schedule_work(&ew->work);
2994
2995 return 1;
2996}
2997EXPORT_SYMBOL_GPL(execute_in_process_context);
2998
Tejun Heo226223a2013-03-12 11:30:05 -07002999#ifdef CONFIG_SYSFS
3000/*
3001 * Workqueues with WQ_SYSFS flag set is visible to userland via
3002 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3003 * following attributes.
3004 *
3005 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3006 * max_active RW int : maximum number of in-flight work items
3007 *
3008 * Unbound workqueues have the following extra attributes.
3009 *
3010 * id RO int : the associated pool ID
3011 * nice RW int : nice value of the workers
3012 * cpumask RW mask : bitmask of allowed CPUs for the workers
3013 */
3014struct wq_device {
3015 struct workqueue_struct *wq;
3016 struct device dev;
3017};
3018
3019static struct workqueue_struct *dev_to_wq(struct device *dev)
3020{
3021 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3022
3023 return wq_dev->wq;
3024}
3025
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003026static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
3027 char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003028{
3029 struct workqueue_struct *wq = dev_to_wq(dev);
3030
3031 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3032}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003033static DEVICE_ATTR_RO(per_cpu);
Tejun Heo226223a2013-03-12 11:30:05 -07003034
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003035static ssize_t max_active_show(struct device *dev,
3036 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003037{
3038 struct workqueue_struct *wq = dev_to_wq(dev);
3039
3040 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3041}
3042
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003043static ssize_t max_active_store(struct device *dev,
3044 struct device_attribute *attr, const char *buf,
3045 size_t count)
Tejun Heo226223a2013-03-12 11:30:05 -07003046{
3047 struct workqueue_struct *wq = dev_to_wq(dev);
3048 int val;
3049
3050 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3051 return -EINVAL;
3052
3053 workqueue_set_max_active(wq, val);
3054 return count;
3055}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003056static DEVICE_ATTR_RW(max_active);
Tejun Heo226223a2013-03-12 11:30:05 -07003057
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003058static struct attribute *wq_sysfs_attrs[] = {
3059 &dev_attr_per_cpu.attr,
3060 &dev_attr_max_active.attr,
3061 NULL,
Tejun Heo226223a2013-03-12 11:30:05 -07003062};
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003063ATTRIBUTE_GROUPS(wq_sysfs);
Tejun Heo226223a2013-03-12 11:30:05 -07003064
Tejun Heod55262c2013-04-01 11:23:38 -07003065static ssize_t wq_pool_ids_show(struct device *dev,
3066 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003067{
3068 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003069 const char *delim = "";
3070 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003071
3072 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003073 for_each_node(node) {
3074 written += scnprintf(buf + written, PAGE_SIZE - written,
3075 "%s%d:%d", delim, node,
3076 unbound_pwq_by_node(wq, node)->pool->id);
3077 delim = " ";
3078 }
3079 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003080 rcu_read_unlock_sched();
3081
3082 return written;
3083}
3084
3085static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3086 char *buf)
3087{
3088 struct workqueue_struct *wq = dev_to_wq(dev);
3089 int written;
3090
Tejun Heo6029a912013-04-01 11:23:34 -07003091 mutex_lock(&wq->mutex);
3092 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3093 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003094
3095 return written;
3096}
3097
3098/* prepare workqueue_attrs for sysfs store operations */
3099static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3100{
3101 struct workqueue_attrs *attrs;
3102
3103 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3104 if (!attrs)
3105 return NULL;
3106
Tejun Heo6029a912013-04-01 11:23:34 -07003107 mutex_lock(&wq->mutex);
3108 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3109 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003110 return attrs;
3111}
3112
3113static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3114 const char *buf, size_t count)
3115{
3116 struct workqueue_struct *wq = dev_to_wq(dev);
3117 struct workqueue_attrs *attrs;
3118 int ret;
3119
3120 attrs = wq_sysfs_prep_attrs(wq);
3121 if (!attrs)
3122 return -ENOMEM;
3123
3124 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
Dongsheng Yang14481842014-02-11 15:34:52 +08003125 attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
Tejun Heo226223a2013-03-12 11:30:05 -07003126 ret = apply_workqueue_attrs(wq, attrs);
3127 else
3128 ret = -EINVAL;
3129
3130 free_workqueue_attrs(attrs);
3131 return ret ?: count;
3132}
3133
3134static ssize_t wq_cpumask_show(struct device *dev,
3135 struct device_attribute *attr, char *buf)
3136{
3137 struct workqueue_struct *wq = dev_to_wq(dev);
3138 int written;
3139
Tejun Heo6029a912013-04-01 11:23:34 -07003140 mutex_lock(&wq->mutex);
Tejun Heodfbcbf42015-02-13 14:37:37 -08003141 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
3142 cpumask_pr_args(wq->unbound_attrs->cpumask));
Tejun Heo6029a912013-04-01 11:23:34 -07003143 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003144 return written;
3145}
3146
3147static ssize_t wq_cpumask_store(struct device *dev,
3148 struct device_attribute *attr,
3149 const char *buf, size_t count)
3150{
3151 struct workqueue_struct *wq = dev_to_wq(dev);
3152 struct workqueue_attrs *attrs;
3153 int ret;
3154
3155 attrs = wq_sysfs_prep_attrs(wq);
3156 if (!attrs)
3157 return -ENOMEM;
3158
3159 ret = cpumask_parse(buf, attrs->cpumask);
3160 if (!ret)
3161 ret = apply_workqueue_attrs(wq, attrs);
3162
3163 free_workqueue_attrs(attrs);
3164 return ret ?: count;
3165}
3166
Tejun Heod55262c2013-04-01 11:23:38 -07003167static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3168 char *buf)
3169{
3170 struct workqueue_struct *wq = dev_to_wq(dev);
3171 int written;
3172
3173 mutex_lock(&wq->mutex);
3174 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3175 !wq->unbound_attrs->no_numa);
3176 mutex_unlock(&wq->mutex);
3177
3178 return written;
3179}
3180
3181static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3182 const char *buf, size_t count)
3183{
3184 struct workqueue_struct *wq = dev_to_wq(dev);
3185 struct workqueue_attrs *attrs;
3186 int v, ret;
3187
3188 attrs = wq_sysfs_prep_attrs(wq);
3189 if (!attrs)
3190 return -ENOMEM;
3191
3192 ret = -EINVAL;
3193 if (sscanf(buf, "%d", &v) == 1) {
3194 attrs->no_numa = !v;
3195 ret = apply_workqueue_attrs(wq, attrs);
3196 }
3197
3198 free_workqueue_attrs(attrs);
3199 return ret ?: count;
3200}
3201
Tejun Heo226223a2013-03-12 11:30:05 -07003202static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003203 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003204 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3205 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003206 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003207 __ATTR_NULL,
3208};
3209
3210static struct bus_type wq_subsys = {
3211 .name = "workqueue",
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003212 .dev_groups = wq_sysfs_groups,
Tejun Heo226223a2013-03-12 11:30:05 -07003213};
3214
3215static int __init wq_sysfs_init(void)
3216{
3217 return subsys_virtual_register(&wq_subsys, NULL);
3218}
3219core_initcall(wq_sysfs_init);
3220
3221static void wq_device_release(struct device *dev)
3222{
3223 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3224
3225 kfree(wq_dev);
3226}
3227
3228/**
3229 * workqueue_sysfs_register - make a workqueue visible in sysfs
3230 * @wq: the workqueue to register
3231 *
3232 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3233 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3234 * which is the preferred method.
3235 *
3236 * Workqueue user should use this function directly iff it wants to apply
3237 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3238 * apply_workqueue_attrs() may race against userland updating the
3239 * attributes.
3240 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003241 * Return: 0 on success, -errno on failure.
Tejun Heo226223a2013-03-12 11:30:05 -07003242 */
3243int workqueue_sysfs_register(struct workqueue_struct *wq)
3244{
3245 struct wq_device *wq_dev;
3246 int ret;
3247
3248 /*
3249 * Adjusting max_active or creating new pwqs by applyting
3250 * attributes breaks ordering guarantee. Disallow exposing ordered
3251 * workqueues.
3252 */
3253 if (WARN_ON(wq->flags & __WQ_ORDERED))
3254 return -EINVAL;
3255
3256 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3257 if (!wq_dev)
3258 return -ENOMEM;
3259
3260 wq_dev->wq = wq;
3261 wq_dev->dev.bus = &wq_subsys;
3262 wq_dev->dev.init_name = wq->name;
3263 wq_dev->dev.release = wq_device_release;
3264
3265 /*
3266 * unbound_attrs are created separately. Suppress uevent until
3267 * everything is ready.
3268 */
3269 dev_set_uevent_suppress(&wq_dev->dev, true);
3270
3271 ret = device_register(&wq_dev->dev);
3272 if (ret) {
3273 kfree(wq_dev);
3274 wq->wq_dev = NULL;
3275 return ret;
3276 }
3277
3278 if (wq->flags & WQ_UNBOUND) {
3279 struct device_attribute *attr;
3280
3281 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3282 ret = device_create_file(&wq_dev->dev, attr);
3283 if (ret) {
3284 device_unregister(&wq_dev->dev);
3285 wq->wq_dev = NULL;
3286 return ret;
3287 }
3288 }
3289 }
3290
Maxime Bizonbddbceb2014-06-23 16:35:35 +02003291 dev_set_uevent_suppress(&wq_dev->dev, false);
Tejun Heo226223a2013-03-12 11:30:05 -07003292 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3293 return 0;
3294}
3295
3296/**
3297 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3298 * @wq: the workqueue to unregister
3299 *
3300 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3301 */
3302static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3303{
3304 struct wq_device *wq_dev = wq->wq_dev;
3305
3306 if (!wq->wq_dev)
3307 return;
3308
3309 wq->wq_dev = NULL;
3310 device_unregister(&wq_dev->dev);
3311}
3312#else /* CONFIG_SYSFS */
3313static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3314#endif /* CONFIG_SYSFS */
3315
Tejun Heo7a4e3442013-03-12 11:30:00 -07003316/**
3317 * free_workqueue_attrs - free a workqueue_attrs
3318 * @attrs: workqueue_attrs to free
3319 *
3320 * Undo alloc_workqueue_attrs().
3321 */
3322void free_workqueue_attrs(struct workqueue_attrs *attrs)
3323{
3324 if (attrs) {
3325 free_cpumask_var(attrs->cpumask);
3326 kfree(attrs);
3327 }
3328}
3329
3330/**
3331 * alloc_workqueue_attrs - allocate a workqueue_attrs
3332 * @gfp_mask: allocation mask to use
3333 *
3334 * Allocate a new workqueue_attrs, initialize with default settings and
Yacine Belkadid185af32013-07-31 14:59:24 -07003335 * return it.
3336 *
3337 * Return: The allocated new workqueue_attr on success. %NULL on failure.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003338 */
3339struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3340{
3341 struct workqueue_attrs *attrs;
3342
3343 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3344 if (!attrs)
3345 goto fail;
3346 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3347 goto fail;
3348
Tejun Heo13e2e552013-04-01 11:23:31 -07003349 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003350 return attrs;
3351fail:
3352 free_workqueue_attrs(attrs);
3353 return NULL;
3354}
3355
Tejun Heo29c91e92013-03-12 11:30:03 -07003356static void copy_workqueue_attrs(struct workqueue_attrs *to,
3357 const struct workqueue_attrs *from)
3358{
3359 to->nice = from->nice;
3360 cpumask_copy(to->cpumask, from->cpumask);
Shaohua Li2865a8f2013-08-01 09:56:36 +08003361 /*
3362 * Unlike hash and equality test, this function doesn't ignore
3363 * ->no_numa as it is used for both pool and wq attrs. Instead,
3364 * get_unbound_pool() explicitly clears ->no_numa after copying.
3365 */
3366 to->no_numa = from->no_numa;
Tejun Heo29c91e92013-03-12 11:30:03 -07003367}
3368
Tejun Heo29c91e92013-03-12 11:30:03 -07003369/* hash value of the content of @attr */
3370static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3371{
3372 u32 hash = 0;
3373
3374 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003375 hash = jhash(cpumask_bits(attrs->cpumask),
3376 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003377 return hash;
3378}
3379
3380/* content equality test */
3381static bool wqattrs_equal(const struct workqueue_attrs *a,
3382 const struct workqueue_attrs *b)
3383{
3384 if (a->nice != b->nice)
3385 return false;
3386 if (!cpumask_equal(a->cpumask, b->cpumask))
3387 return false;
3388 return true;
3389}
3390
Tejun Heo7a4e3442013-03-12 11:30:00 -07003391/**
3392 * init_worker_pool - initialize a newly zalloc'd worker_pool
3393 * @pool: worker_pool to initialize
3394 *
3395 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Yacine Belkadid185af32013-07-31 14:59:24 -07003396 *
3397 * Return: 0 on success, -errno on failure. Even on failure, all fields
Tejun Heo29c91e92013-03-12 11:30:03 -07003398 * inside @pool proper are initialized and put_unbound_pool() can be called
3399 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003400 */
3401static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003402{
3403 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003404 pool->id = -1;
3405 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003406 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003407 pool->flags |= POOL_DISASSOCIATED;
3408 INIT_LIST_HEAD(&pool->worklist);
3409 INIT_LIST_HEAD(&pool->idle_list);
3410 hash_init(pool->busy_hash);
3411
3412 init_timer_deferrable(&pool->idle_timer);
3413 pool->idle_timer.function = idle_worker_timeout;
3414 pool->idle_timer.data = (unsigned long)pool;
3415
3416 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3417 (unsigned long)pool);
3418
3419 mutex_init(&pool->manager_arb);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003420 mutex_init(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003421 INIT_LIST_HEAD(&pool->workers);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003422
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003423 ida_init(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003424 INIT_HLIST_NODE(&pool->hash_node);
3425 pool->refcnt = 1;
3426
3427 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003428 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3429 if (!pool->attrs)
3430 return -ENOMEM;
3431 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003432}
3433
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003434static void rcu_free_wq(struct rcu_head *rcu)
3435{
3436 struct workqueue_struct *wq =
3437 container_of(rcu, struct workqueue_struct, rcu);
3438
3439 if (!(wq->flags & WQ_UNBOUND))
3440 free_percpu(wq->cpu_pwqs);
3441 else
3442 free_workqueue_attrs(wq->unbound_attrs);
3443
3444 kfree(wq->rescuer);
3445 kfree(wq);
3446}
3447
Tejun Heo29c91e92013-03-12 11:30:03 -07003448static void rcu_free_pool(struct rcu_head *rcu)
3449{
3450 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3451
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003452 ida_destroy(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003453 free_workqueue_attrs(pool->attrs);
3454 kfree(pool);
3455}
3456
3457/**
3458 * put_unbound_pool - put a worker_pool
3459 * @pool: worker_pool to put
3460 *
3461 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003462 * safe manner. get_unbound_pool() calls this function on its failure path
3463 * and this function should be able to release pools which went through,
3464 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003465 *
3466 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003467 */
3468static void put_unbound_pool(struct worker_pool *pool)
3469{
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003470 DECLARE_COMPLETION_ONSTACK(detach_completion);
Tejun Heo29c91e92013-03-12 11:30:03 -07003471 struct worker *worker;
3472
Tejun Heoa892cac2013-04-01 11:23:32 -07003473 lockdep_assert_held(&wq_pool_mutex);
3474
3475 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003476 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003477
3478 /* sanity checks */
Lai Jiangshan61d0fbb2014-06-03 15:31:45 +08003479 if (WARN_ON(!(pool->cpu < 0)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003480 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003481 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003482
3483 /* release id and unhash */
3484 if (pool->id >= 0)
3485 idr_remove(&worker_pool_idr, pool->id);
3486 hash_del(&pool->hash_node);
3487
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003488 /*
3489 * Become the manager and destroy all workers. Grabbing
3490 * manager_arb prevents @pool's workers from blocking on
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003491 * attach_mutex.
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003492 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003493 mutex_lock(&pool->manager_arb);
Tejun Heo29c91e92013-03-12 11:30:03 -07003494
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003495 spin_lock_irq(&pool->lock);
Lai Jiangshan1037de32014-05-22 16:44:07 +08003496 while ((worker = first_idle_worker(pool)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003497 destroy_worker(worker);
3498 WARN_ON(pool->nr_workers || pool->nr_idle);
Tejun Heo29c91e92013-03-12 11:30:03 -07003499 spin_unlock_irq(&pool->lock);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003500
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003501 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003502 if (!list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003503 pool->detach_completion = &detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003504 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003505
3506 if (pool->detach_completion)
3507 wait_for_completion(pool->detach_completion);
3508
Tejun Heo29c91e92013-03-12 11:30:03 -07003509 mutex_unlock(&pool->manager_arb);
3510
3511 /* shut down the timers */
3512 del_timer_sync(&pool->idle_timer);
3513 del_timer_sync(&pool->mayday_timer);
3514
3515 /* sched-RCU protected to allow dereferences from get_work_pool() */
3516 call_rcu_sched(&pool->rcu, rcu_free_pool);
3517}
3518
3519/**
3520 * get_unbound_pool - get a worker_pool with the specified attributes
3521 * @attrs: the attributes of the worker_pool to get
3522 *
3523 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3524 * reference count and return it. If there already is a matching
3525 * worker_pool, it will be used; otherwise, this function attempts to
Yacine Belkadid185af32013-07-31 14:59:24 -07003526 * create a new one.
Tejun Heoa892cac2013-04-01 11:23:32 -07003527 *
3528 * Should be called with wq_pool_mutex held.
Yacine Belkadid185af32013-07-31 14:59:24 -07003529 *
3530 * Return: On success, a worker_pool with the same attributes as @attrs.
3531 * On failure, %NULL.
Tejun Heo29c91e92013-03-12 11:30:03 -07003532 */
3533static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3534{
Tejun Heo29c91e92013-03-12 11:30:03 -07003535 u32 hash = wqattrs_hash(attrs);
3536 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003537 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003538
Tejun Heoa892cac2013-04-01 11:23:32 -07003539 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003540
3541 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003542 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3543 if (wqattrs_equal(pool->attrs, attrs)) {
3544 pool->refcnt++;
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003545 return pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003546 }
3547 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003548
3549 /* nope, create a new one */
3550 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3551 if (!pool || init_worker_pool(pool) < 0)
3552 goto fail;
3553
Tejun Heo8864b4e2013-03-12 11:30:04 -07003554 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003555 copy_workqueue_attrs(pool->attrs, attrs);
3556
Shaohua Li2865a8f2013-08-01 09:56:36 +08003557 /*
3558 * no_numa isn't a worker_pool attribute, always clear it. See
3559 * 'struct workqueue_attrs' comments for detail.
3560 */
3561 pool->attrs->no_numa = false;
3562
Tejun Heof3f90ad2013-04-01 11:23:34 -07003563 /* if cpumask is contained inside a NUMA node, we belong to that node */
3564 if (wq_numa_enabled) {
3565 for_each_node(node) {
3566 if (cpumask_subset(pool->attrs->cpumask,
3567 wq_numa_possible_cpumask[node])) {
3568 pool->node = node;
3569 break;
3570 }
3571 }
3572 }
3573
Tejun Heo29c91e92013-03-12 11:30:03 -07003574 if (worker_pool_assign_id(pool) < 0)
3575 goto fail;
3576
3577 /* create and start the initial worker */
Lai Jiangshan051e1852014-07-22 13:03:02 +08003578 if (!create_worker(pool))
Tejun Heo29c91e92013-03-12 11:30:03 -07003579 goto fail;
3580
Tejun Heo29c91e92013-03-12 11:30:03 -07003581 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003582 hash_add(unbound_pool_hash, &pool->hash_node, hash);
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003583
Tejun Heo29c91e92013-03-12 11:30:03 -07003584 return pool;
3585fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003586 if (pool)
3587 put_unbound_pool(pool);
3588 return NULL;
3589}
3590
Tejun Heo8864b4e2013-03-12 11:30:04 -07003591static void rcu_free_pwq(struct rcu_head *rcu)
3592{
3593 kmem_cache_free(pwq_cache,
3594 container_of(rcu, struct pool_workqueue, rcu));
3595}
3596
3597/*
3598 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3599 * and needs to be destroyed.
3600 */
3601static void pwq_unbound_release_workfn(struct work_struct *work)
3602{
3603 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3604 unbound_release_work);
3605 struct workqueue_struct *wq = pwq->wq;
3606 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003607 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003608
3609 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3610 return;
3611
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003612 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003613 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003614 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003615 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003616
Tejun Heoa892cac2013-04-01 11:23:32 -07003617 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003618 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003619 mutex_unlock(&wq_pool_mutex);
3620
Tejun Heo8864b4e2013-03-12 11:30:04 -07003621 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3622
3623 /*
3624 * If we're the last pwq going away, @wq is already dead and no one
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003625 * is gonna access it anymore. Schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003626 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003627 if (is_last)
3628 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003629}
3630
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003631/**
Tejun Heo699ce092013-03-13 16:51:35 -07003632 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003633 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003634 *
Tejun Heo699ce092013-03-13 16:51:35 -07003635 * If @pwq isn't freezing, set @pwq->max_active to the associated
3636 * workqueue's saved_max_active and activate delayed work items
3637 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003638 */
Tejun Heo699ce092013-03-13 16:51:35 -07003639static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003640{
Tejun Heo699ce092013-03-13 16:51:35 -07003641 struct workqueue_struct *wq = pwq->wq;
3642 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003643
Tejun Heo699ce092013-03-13 16:51:35 -07003644 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003645 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003646
3647 /* fast exit for non-freezable wqs */
3648 if (!freezable && pwq->max_active == wq->saved_max_active)
3649 return;
3650
Lai Jiangshana357fc02013-03-25 16:57:19 -07003651 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003652
Lai Jiangshan74b414e2014-05-22 19:01:16 +08003653 /*
3654 * During [un]freezing, the caller is responsible for ensuring that
3655 * this function is called at least once after @workqueue_freezing
3656 * is updated and visible.
3657 */
3658 if (!freezable || !workqueue_freezing) {
Tejun Heo699ce092013-03-13 16:51:35 -07003659 pwq->max_active = wq->saved_max_active;
3660
3661 while (!list_empty(&pwq->delayed_works) &&
3662 pwq->nr_active < pwq->max_active)
3663 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003664
3665 /*
3666 * Need to kick a worker after thawed or an unbound wq's
3667 * max_active is bumped. It's a slow path. Do it always.
3668 */
3669 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003670 } else {
3671 pwq->max_active = 0;
3672 }
3673
Lai Jiangshana357fc02013-03-25 16:57:19 -07003674 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003675}
3676
Tejun Heoe50aba92013-04-01 11:23:35 -07003677/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003678static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3679 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003680{
3681 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3682
Tejun Heoe50aba92013-04-01 11:23:35 -07003683 memset(pwq, 0, sizeof(*pwq));
3684
Tejun Heod2c1d402013-03-12 11:30:04 -07003685 pwq->pool = pool;
3686 pwq->wq = wq;
3687 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003688 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003689 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003690 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003691 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003692 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003693}
Tejun Heod2c1d402013-03-12 11:30:04 -07003694
Tejun Heof147f292013-04-01 11:23:35 -07003695/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003696static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003697{
3698 struct workqueue_struct *wq = pwq->wq;
3699
3700 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003701
Tejun Heo1befcf32013-04-01 11:23:35 -07003702 /* may be called multiple times, ignore if already linked */
3703 if (!list_empty(&pwq->pwqs_node))
3704 return;
3705
Lai Jiangshan29b1cb42014-07-22 13:04:27 +08003706 /* set the matching work_color */
Tejun Heo75ccf592013-03-12 11:30:04 -07003707 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003708
3709 /* sync max_active to the current setting */
3710 pwq_adjust_max_active(pwq);
3711
3712 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003713 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003714}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003715
Tejun Heof147f292013-04-01 11:23:35 -07003716/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3717static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3718 const struct workqueue_attrs *attrs)
3719{
3720 struct worker_pool *pool;
3721 struct pool_workqueue *pwq;
3722
3723 lockdep_assert_held(&wq_pool_mutex);
3724
3725 pool = get_unbound_pool(attrs);
3726 if (!pool)
3727 return NULL;
3728
Tejun Heoe50aba92013-04-01 11:23:35 -07003729 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003730 if (!pwq) {
3731 put_unbound_pool(pool);
3732 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003733 }
Tejun Heo6029a912013-04-01 11:23:34 -07003734
Tejun Heof147f292013-04-01 11:23:35 -07003735 init_pwq(pwq, wq, pool);
3736 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003737}
3738
Tejun Heo4c16bd32013-04-01 11:23:36 -07003739/* undo alloc_unbound_pwq(), used only in the error path */
3740static void free_unbound_pwq(struct pool_workqueue *pwq)
3741{
3742 lockdep_assert_held(&wq_pool_mutex);
3743
3744 if (pwq) {
3745 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003746 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003747 }
3748}
3749
3750/**
3751 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3752 * @attrs: the wq_attrs of interest
3753 * @node: the target NUMA node
3754 * @cpu_going_down: if >= 0, the CPU to consider as offline
3755 * @cpumask: outarg, the resulting cpumask
3756 *
3757 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3758 * @cpu_going_down is >= 0, that cpu is considered offline during
Yacine Belkadid185af32013-07-31 14:59:24 -07003759 * calculation. The result is stored in @cpumask.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003760 *
3761 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3762 * enabled and @node has online CPUs requested by @attrs, the returned
3763 * cpumask is the intersection of the possible CPUs of @node and
3764 * @attrs->cpumask.
3765 *
3766 * The caller is responsible for ensuring that the cpumask of @node stays
3767 * stable.
Yacine Belkadid185af32013-07-31 14:59:24 -07003768 *
3769 * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
3770 * %false if equal.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003771 */
3772static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3773 int cpu_going_down, cpumask_t *cpumask)
3774{
Tejun Heod55262c2013-04-01 11:23:38 -07003775 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003776 goto use_dfl;
3777
3778 /* does @node have any online CPUs @attrs wants? */
3779 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3780 if (cpu_going_down >= 0)
3781 cpumask_clear_cpu(cpu_going_down, cpumask);
3782
3783 if (cpumask_empty(cpumask))
3784 goto use_dfl;
3785
3786 /* yeap, return possible CPUs in @node that @attrs wants */
3787 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3788 return !cpumask_equal(cpumask, attrs->cpumask);
3789
3790use_dfl:
3791 cpumask_copy(cpumask, attrs->cpumask);
3792 return false;
3793}
3794
Tejun Heo1befcf32013-04-01 11:23:35 -07003795/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3796static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3797 int node,
3798 struct pool_workqueue *pwq)
3799{
3800 struct pool_workqueue *old_pwq;
3801
3802 lockdep_assert_held(&wq->mutex);
3803
3804 /* link_pwq() can handle duplicate calls */
3805 link_pwq(pwq);
3806
3807 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3808 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3809 return old_pwq;
3810}
3811
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003812/**
3813 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3814 * @wq: the target workqueue
3815 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3816 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003817 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3818 * machines, this function maps a separate pwq to each NUMA node with
3819 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3820 * NUMA node it was issued on. Older pwqs are released as in-flight work
3821 * items finish. Note that a work item which repeatedly requeues itself
3822 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003823 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003824 * Performs GFP_KERNEL allocations.
3825 *
3826 * Return: 0 on success and -errno on failure.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003827 */
3828int apply_workqueue_attrs(struct workqueue_struct *wq,
3829 const struct workqueue_attrs *attrs)
3830{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003831 struct workqueue_attrs *new_attrs, *tmp_attrs;
3832 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003833 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003834
Tejun Heo8719dce2013-03-12 11:30:04 -07003835 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003836 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3837 return -EINVAL;
3838
Tejun Heo8719dce2013-03-12 11:30:04 -07003839 /* creating multiple pwqs breaks ordering guarantee */
3840 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3841 return -EINVAL;
3842
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08003843 pwq_tbl = kzalloc(nr_node_ids * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003844 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003845 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3846 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003847 goto enomem;
3848
Tejun Heo4c16bd32013-04-01 11:23:36 -07003849 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003850 copy_workqueue_attrs(new_attrs, attrs);
3851 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3852
Tejun Heo4c16bd32013-04-01 11:23:36 -07003853 /*
3854 * We may create multiple pwqs with differing cpumasks. Make a
3855 * copy of @new_attrs which will be modified and used to obtain
3856 * pools.
3857 */
3858 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003859
Tejun Heo4c16bd32013-04-01 11:23:36 -07003860 /*
3861 * CPUs should stay stable across pwq creations and installations.
3862 * Pin CPUs, determine the target cpumask for each node and create
3863 * pwqs accordingly.
3864 */
3865 get_online_cpus();
3866
3867 mutex_lock(&wq_pool_mutex);
3868
3869 /*
3870 * If something goes wrong during CPU up/down, we'll fall back to
3871 * the default pwq covering whole @attrs->cpumask. Always create
3872 * it even if we don't use it immediately.
3873 */
3874 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3875 if (!dfl_pwq)
3876 goto enomem_pwq;
3877
3878 for_each_node(node) {
3879 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3880 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3881 if (!pwq_tbl[node])
3882 goto enomem_pwq;
3883 } else {
3884 dfl_pwq->refcnt++;
3885 pwq_tbl[node] = dfl_pwq;
3886 }
3887 }
3888
3889 mutex_unlock(&wq_pool_mutex);
3890
3891 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003892 mutex_lock(&wq->mutex);
3893
Tejun Heof147f292013-04-01 11:23:35 -07003894 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003895
3896 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003897 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003898 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3899
3900 /* @dfl_pwq might not have been used, ensure it's linked */
3901 link_pwq(dfl_pwq);
3902 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003903
3904 mutex_unlock(&wq->mutex);
3905
Tejun Heo4c16bd32013-04-01 11:23:36 -07003906 /* put the old pwqs */
3907 for_each_node(node)
3908 put_pwq_unlocked(pwq_tbl[node]);
3909 put_pwq_unlocked(dfl_pwq);
3910
3911 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003912 ret = 0;
3913 /* fall through */
3914out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003915 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003916 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003917 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003918 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003919
Tejun Heo4c16bd32013-04-01 11:23:36 -07003920enomem_pwq:
3921 free_unbound_pwq(dfl_pwq);
3922 for_each_node(node)
3923 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3924 free_unbound_pwq(pwq_tbl[node]);
3925 mutex_unlock(&wq_pool_mutex);
3926 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003927enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003928 ret = -ENOMEM;
3929 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003930}
3931
Tejun Heo4c16bd32013-04-01 11:23:36 -07003932/**
3933 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3934 * @wq: the target workqueue
3935 * @cpu: the CPU coming up or going down
3936 * @online: whether @cpu is coming up or going down
3937 *
3938 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3939 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3940 * @wq accordingly.
3941 *
3942 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3943 * falls back to @wq->dfl_pwq which may not be optimal but is always
3944 * correct.
3945 *
3946 * Note that when the last allowed CPU of a NUMA node goes offline for a
3947 * workqueue with a cpumask spanning multiple nodes, the workers which were
3948 * already executing the work items for the workqueue will lose their CPU
3949 * affinity and may execute on any CPU. This is similar to how per-cpu
3950 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3951 * affinity, it's the user's responsibility to flush the work item from
3952 * CPU_DOWN_PREPARE.
3953 */
3954static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3955 bool online)
3956{
3957 int node = cpu_to_node(cpu);
3958 int cpu_off = online ? -1 : cpu;
3959 struct pool_workqueue *old_pwq = NULL, *pwq;
3960 struct workqueue_attrs *target_attrs;
3961 cpumask_t *cpumask;
3962
3963 lockdep_assert_held(&wq_pool_mutex);
3964
3965 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3966 return;
3967
3968 /*
3969 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3970 * Let's use a preallocated one. The following buf is protected by
3971 * CPU hotplug exclusion.
3972 */
3973 target_attrs = wq_update_unbound_numa_attrs_buf;
3974 cpumask = target_attrs->cpumask;
3975
3976 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07003977 if (wq->unbound_attrs->no_numa)
3978 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003979
3980 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3981 pwq = unbound_pwq_by_node(wq, node);
3982
3983 /*
3984 * Let's determine what needs to be done. If the target cpumask is
3985 * different from wq's, we need to compare it to @pwq's and create
3986 * a new one if they don't match. If the target cpumask equals
Daeseok Youn534a3fb2014-04-18 09:08:14 +09003987 * wq's, the default pwq should be used.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003988 */
3989 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3990 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
3991 goto out_unlock;
3992 } else {
Daeseok Youn534a3fb2014-04-18 09:08:14 +09003993 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003994 }
3995
3996 mutex_unlock(&wq->mutex);
3997
3998 /* create a new pwq */
3999 pwq = alloc_unbound_pwq(wq, target_attrs);
4000 if (!pwq) {
Fabian Frederick2d916032014-05-12 13:59:35 -04004001 pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4002 wq->name);
Daeseok Youn77f300b2014-04-16 14:32:29 +09004003 mutex_lock(&wq->mutex);
4004 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004005 }
4006
4007 /*
4008 * Install the new pwq. As this function is called only from CPU
4009 * hotplug callbacks and applying a new attrs is wrapped with
4010 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4011 * inbetween.
4012 */
4013 mutex_lock(&wq->mutex);
4014 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4015 goto out_unlock;
4016
4017use_dfl_pwq:
4018 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4019 get_pwq(wq->dfl_pwq);
4020 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4021 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4022out_unlock:
4023 mutex_unlock(&wq->mutex);
4024 put_pwq_unlocked(old_pwq);
4025}
4026
Tejun Heo30cdf242013-03-12 11:29:57 -07004027static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004029 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo8a2b7532013-09-05 12:30:04 -04004030 int cpu, ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004031
Tejun Heo30cdf242013-03-12 11:29:57 -07004032 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004033 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4034 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004035 return -ENOMEM;
4036
4037 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004038 struct pool_workqueue *pwq =
4039 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004040 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004041 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004042
Tejun Heof147f292013-04-01 11:23:35 -07004043 init_pwq(pwq, wq, &cpu_pools[highpri]);
4044
4045 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004046 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004047 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004048 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004049 return 0;
Tejun Heo8a2b7532013-09-05 12:30:04 -04004050 } else if (wq->flags & __WQ_ORDERED) {
4051 ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]);
4052 /* there should only be single pwq for ordering guarantee */
4053 WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node ||
4054 wq->pwqs.prev != &wq->dfl_pwq->pwqs_node),
4055 "ordering guarantee broken for workqueue %s\n", wq->name);
4056 return ret;
Tejun Heo30cdf242013-03-12 11:29:57 -07004057 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004058 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004059 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004060}
4061
Tejun Heof3421792010-07-02 10:03:51 +02004062static int wq_clamp_max_active(int max_active, unsigned int flags,
4063 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004064{
Tejun Heof3421792010-07-02 10:03:51 +02004065 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4066
4067 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004068 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4069 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004070
Tejun Heof3421792010-07-02 10:03:51 +02004071 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004072}
4073
Tejun Heob196be82012-01-10 15:11:35 -08004074struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004075 unsigned int flags,
4076 int max_active,
4077 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004078 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004079{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004080 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004081 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004082 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004083 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004084
Viresh Kumarcee22a12013-04-08 16:45:40 +05304085 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4086 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4087 flags |= WQ_UNBOUND;
4088
Tejun Heoecf68812013-04-01 11:23:34 -07004089 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004090 if (flags & WQ_UNBOUND)
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08004091 tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004092
4093 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004094 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004095 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004096
Tejun Heo6029a912013-04-01 11:23:34 -07004097 if (flags & WQ_UNBOUND) {
4098 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4099 if (!wq->unbound_attrs)
4100 goto err_free_wq;
4101 }
4102
Tejun Heoecf68812013-04-01 11:23:34 -07004103 va_start(args, lock_name);
4104 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004105 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004106
Tejun Heod320c032010-06-29 10:07:14 +02004107 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004108 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004109
Tejun Heob196be82012-01-10 15:11:35 -08004110 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004111 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004112 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004113 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004114 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004115 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004116 INIT_LIST_HEAD(&wq->flusher_queue);
4117 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004118 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004119
Johannes Bergeb13ba82008-01-16 09:51:58 +01004120 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004121 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004122
Tejun Heo30cdf242013-03-12 11:29:57 -07004123 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004124 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004125
Tejun Heo493008a2013-03-12 11:30:03 -07004126 /*
4127 * Workqueues which may be used during memory reclaim should
4128 * have a rescuer to guarantee forward progress.
4129 */
4130 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004131 struct worker *rescuer;
4132
Lai Jiangshanf7537df2014-07-15 17:24:15 +08004133 rescuer = alloc_worker(NUMA_NO_NODE);
Tejun Heoe22bee72010-06-29 10:07:14 +02004134 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004135 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004136
Tejun Heo111c2252013-01-17 17:16:24 -08004137 rescuer->rescue_wq = wq;
4138 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004139 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004140 if (IS_ERR(rescuer->task)) {
4141 kfree(rescuer);
4142 goto err_destroy;
4143 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004144
Tejun Heod2c1d402013-03-12 11:30:04 -07004145 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004146 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004147 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004148 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004149
Tejun Heo226223a2013-03-12 11:30:05 -07004150 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4151 goto err_destroy;
4152
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004153 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004154 * wq_pool_mutex protects global freeze state and workqueues list.
4155 * Grab it, adjust max_active and add the new @wq to workqueues
4156 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004157 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004158 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004159
Lai Jiangshana357fc02013-03-25 16:57:19 -07004160 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004161 for_each_pwq(pwq, wq)
4162 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004163 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004164
Tejun Heoe2dca7a2015-03-09 09:22:28 -04004165 list_add_tail_rcu(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004166
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004167 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004168
Oleg Nesterov3af244332007-05-09 02:34:09 -07004169 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004170
4171err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004172 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004173 kfree(wq);
4174 return NULL;
4175err_destroy:
4176 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004177 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004178}
Tejun Heod320c032010-06-29 10:07:14 +02004179EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004180
4181/**
4182 * destroy_workqueue - safely terminate a workqueue
4183 * @wq: target workqueue
4184 *
4185 * Safely destroy a workqueue. All work currently pending will be done first.
4186 */
4187void destroy_workqueue(struct workqueue_struct *wq)
4188{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004189 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004190 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004191
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004192 /* drain it before proceeding with destruction */
4193 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004194
Tejun Heo6183c002013-03-12 11:29:57 -07004195 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004196 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004197 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004198 int i;
4199
Tejun Heo76af4d92013-03-12 11:30:00 -07004200 for (i = 0; i < WORK_NR_COLORS; i++) {
4201 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004202 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004203 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004204 }
4205 }
4206
Lai Jiangshan5c529592013-04-04 10:05:38 +08004207 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004208 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004209 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004210 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004211 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004212 }
Tejun Heo6183c002013-03-12 11:29:57 -07004213 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004214 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004215
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004216 /*
4217 * wq list is used to freeze wq, remove from list after
4218 * flushing is complete in case freeze races us.
4219 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004220 mutex_lock(&wq_pool_mutex);
Tejun Heoe2dca7a2015-03-09 09:22:28 -04004221 list_del_rcu(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004222 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004223
Tejun Heo226223a2013-03-12 11:30:05 -07004224 workqueue_sysfs_unregister(wq);
4225
Tejun Heoe2dca7a2015-03-09 09:22:28 -04004226 if (wq->rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02004227 kthread_stop(wq->rescuer->task);
Tejun Heoe22bee72010-06-29 10:07:14 +02004228
Tejun Heo8864b4e2013-03-12 11:30:04 -07004229 if (!(wq->flags & WQ_UNBOUND)) {
4230 /*
4231 * The base ref is never dropped on per-cpu pwqs. Directly
Tejun Heoe2dca7a2015-03-09 09:22:28 -04004232 * schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004233 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04004234 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07004235 } else {
4236 /*
4237 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004238 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4239 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004240 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004241 for_each_node(node) {
4242 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4243 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4244 put_pwq_unlocked(pwq);
4245 }
4246
4247 /*
4248 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4249 * put. Don't access it afterwards.
4250 */
4251 pwq = wq->dfl_pwq;
4252 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004253 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004254 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004255}
4256EXPORT_SYMBOL_GPL(destroy_workqueue);
4257
Tejun Heodcd989c2010-06-29 10:07:14 +02004258/**
4259 * workqueue_set_max_active - adjust max_active of a workqueue
4260 * @wq: target workqueue
4261 * @max_active: new max_active value.
4262 *
4263 * Set max_active of @wq to @max_active.
4264 *
4265 * CONTEXT:
4266 * Don't call from IRQ context.
4267 */
4268void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4269{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004270 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004271
Tejun Heo8719dce2013-03-12 11:30:04 -07004272 /* disallow meddling with max_active for ordered workqueues */
4273 if (WARN_ON(wq->flags & __WQ_ORDERED))
4274 return;
4275
Tejun Heof3421792010-07-02 10:03:51 +02004276 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004277
Lai Jiangshana357fc02013-03-25 16:57:19 -07004278 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004279
4280 wq->saved_max_active = max_active;
4281
Tejun Heo699ce092013-03-13 16:51:35 -07004282 for_each_pwq(pwq, wq)
4283 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004284
Lai Jiangshana357fc02013-03-25 16:57:19 -07004285 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004286}
4287EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4288
4289/**
Tejun Heoe62676162013-03-12 17:41:37 -07004290 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4291 *
4292 * Determine whether %current is a workqueue rescuer. Can be used from
4293 * work functions to determine whether it's being run off the rescuer task.
Yacine Belkadid185af32013-07-31 14:59:24 -07004294 *
4295 * Return: %true if %current is a workqueue rescuer. %false otherwise.
Tejun Heoe62676162013-03-12 17:41:37 -07004296 */
4297bool current_is_workqueue_rescuer(void)
4298{
4299 struct worker *worker = current_wq_worker();
4300
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004301 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07004302}
4303
4304/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004305 * workqueue_congested - test whether a workqueue is congested
4306 * @cpu: CPU in question
4307 * @wq: target workqueue
4308 *
4309 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4310 * no synchronization around this function and the test result is
4311 * unreliable and only useful as advisory hints or for debugging.
4312 *
Tejun Heod3251852013-05-10 11:10:17 -07004313 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4314 * Note that both per-cpu and unbound workqueues may be associated with
4315 * multiple pool_workqueues which have separate congested states. A
4316 * workqueue being congested on one CPU doesn't mean the workqueue is also
4317 * contested on other CPUs / NUMA nodes.
4318 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004319 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004320 * %true if congested, %false otherwise.
4321 */
Tejun Heod84ff052013-03-12 11:29:59 -07004322bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004323{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004324 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004325 bool ret;
4326
Lai Jiangshan88109452013-03-20 03:28:10 +08004327 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004328
Tejun Heod3251852013-05-10 11:10:17 -07004329 if (cpu == WORK_CPU_UNBOUND)
4330 cpu = smp_processor_id();
4331
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004332 if (!(wq->flags & WQ_UNBOUND))
4333 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4334 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004335 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004336
Tejun Heo76af4d92013-03-12 11:30:00 -07004337 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004338 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004339
4340 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004341}
4342EXPORT_SYMBOL_GPL(workqueue_congested);
4343
4344/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004345 * work_busy - test whether a work is currently pending or running
4346 * @work: the work to be tested
4347 *
4348 * Test whether @work is currently pending or running. There is no
4349 * synchronization around this function and the test result is
4350 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004351 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004352 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004353 * OR'd bitmask of WORK_BUSY_* bits.
4354 */
4355unsigned int work_busy(struct work_struct *work)
4356{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004357 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004358 unsigned long flags;
4359 unsigned int ret = 0;
4360
Tejun Heodcd989c2010-06-29 10:07:14 +02004361 if (work_pending(work))
4362 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004363
Tejun Heofa1b54e2013-03-12 11:30:00 -07004364 local_irq_save(flags);
4365 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004366 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004367 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004368 if (find_worker_executing_work(pool, work))
4369 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004370 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004371 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004372 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004373
4374 return ret;
4375}
4376EXPORT_SYMBOL_GPL(work_busy);
4377
Tejun Heo3d1cb202013-04-30 15:27:22 -07004378/**
4379 * set_worker_desc - set description for the current work item
4380 * @fmt: printf-style format string
4381 * @...: arguments for the format string
4382 *
4383 * This function can be called by a running work function to describe what
4384 * the work item is about. If the worker task gets dumped, this
4385 * information will be printed out together to help debugging. The
4386 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4387 */
4388void set_worker_desc(const char *fmt, ...)
4389{
4390 struct worker *worker = current_wq_worker();
4391 va_list args;
4392
4393 if (worker) {
4394 va_start(args, fmt);
4395 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4396 va_end(args);
4397 worker->desc_valid = true;
4398 }
4399}
4400
4401/**
4402 * print_worker_info - print out worker information and description
4403 * @log_lvl: the log level to use when printing
4404 * @task: target task
4405 *
4406 * If @task is a worker and currently executing a work item, print out the
4407 * name of the workqueue being serviced and worker description set with
4408 * set_worker_desc() by the currently executing work item.
4409 *
4410 * This function can be safely called on any task as long as the
4411 * task_struct itself is accessible. While safe, this function isn't
4412 * synchronized and may print out mixups or garbages of limited length.
4413 */
4414void print_worker_info(const char *log_lvl, struct task_struct *task)
4415{
4416 work_func_t *fn = NULL;
4417 char name[WQ_NAME_LEN] = { };
4418 char desc[WORKER_DESC_LEN] = { };
4419 struct pool_workqueue *pwq = NULL;
4420 struct workqueue_struct *wq = NULL;
4421 bool desc_valid = false;
4422 struct worker *worker;
4423
4424 if (!(task->flags & PF_WQ_WORKER))
4425 return;
4426
4427 /*
4428 * This function is called without any synchronization and @task
4429 * could be in any state. Be careful with dereferences.
4430 */
4431 worker = probe_kthread_data(task);
4432
4433 /*
4434 * Carefully copy the associated workqueue's workfn and name. Keep
4435 * the original last '\0' in case the original contains garbage.
4436 */
4437 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4438 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4439 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4440 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4441
4442 /* copy worker description */
4443 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4444 if (desc_valid)
4445 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4446
4447 if (fn || name[0] || desc[0]) {
4448 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4449 if (desc[0])
4450 pr_cont(" (%s)", desc);
4451 pr_cont("\n");
4452 }
4453}
4454
Tejun Heodb7bccf2010-06-29 10:07:12 +02004455/*
4456 * CPU hotplug.
4457 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004458 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004459 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004460 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004461 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004462 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004463 * blocked draining impractical.
4464 *
Tejun Heo24647572013-01-24 11:01:33 -08004465 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004466 * running as an unbound one and allowing it to be reattached later if the
4467 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004468 */
4469
Tejun Heo706026c2013-01-24 11:01:34 -08004470static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004471{
Tejun Heo38db41d2013-01-24 11:01:34 -08004472 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004473 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004474 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004475
Tejun Heof02ae732013-03-12 11:30:03 -07004476 for_each_cpu_worker_pool(pool, cpu) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004477 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004478 spin_lock_irq(&pool->lock);
4479
4480 /*
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004481 * We've blocked all attach/detach operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004482 * unbound and set DISASSOCIATED. Before this, all workers
4483 * except for the ones which are still executing works from
4484 * before the last CPU down must be on the cpu. After
4485 * this, they may become diasporas.
4486 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004487 for_each_pool_worker(worker, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004488 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004489
Tejun Heo24647572013-01-24 11:01:33 -08004490 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004491
Tejun Heo94cf58b2013-01-24 11:01:33 -08004492 spin_unlock_irq(&pool->lock);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004493 mutex_unlock(&pool->attach_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004494
Lai Jiangshaneb283422013-03-08 15:18:28 -08004495 /*
4496 * Call schedule() so that we cross rq->lock and thus can
4497 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4498 * This is necessary as scheduler callbacks may be invoked
4499 * from other cpus.
4500 */
4501 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004502
Lai Jiangshaneb283422013-03-08 15:18:28 -08004503 /*
4504 * Sched callbacks are disabled now. Zap nr_running.
4505 * After this, nr_running stays zero and need_more_worker()
4506 * and keep_working() are always true as long as the
4507 * worklist is not empty. This pool now behaves as an
4508 * unbound (in terms of concurrency management) pool which
4509 * are served by workers tied to the pool.
4510 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004511 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004512
4513 /*
4514 * With concurrency management just turned off, a busy
4515 * worker blocking could lead to lengthy stalls. Kick off
4516 * unbound chain execution of currently pending work items.
4517 */
4518 spin_lock_irq(&pool->lock);
4519 wake_up_worker(pool);
4520 spin_unlock_irq(&pool->lock);
4521 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004522}
4523
Tejun Heobd7c0892013-03-19 13:45:21 -07004524/**
4525 * rebind_workers - rebind all workers of a pool to the associated CPU
4526 * @pool: pool of interest
4527 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004528 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004529 */
4530static void rebind_workers(struct worker_pool *pool)
4531{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004532 struct worker *worker;
Tejun Heobd7c0892013-03-19 13:45:21 -07004533
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004534 lockdep_assert_held(&pool->attach_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004535
Tejun Heoa9ab7752013-03-19 13:45:21 -07004536 /*
4537 * Restore CPU affinity of all workers. As all idle workers should
4538 * be on the run-queue of the associated CPU before any local
4539 * wake-ups for concurrency management happen, restore CPU affinty
4540 * of all workers first and then clear UNBOUND. As we're called
4541 * from CPU_ONLINE, the following shouldn't fail.
4542 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004543 for_each_pool_worker(worker, pool)
Tejun Heoa9ab7752013-03-19 13:45:21 -07004544 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4545 pool->attrs->cpumask) < 0);
4546
4547 spin_lock_irq(&pool->lock);
Lai Jiangshan3de5e882014-06-03 15:33:27 +08004548 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004549
Lai Jiangshanda028462014-05-20 17:46:31 +08004550 for_each_pool_worker(worker, pool) {
Tejun Heoa9ab7752013-03-19 13:45:21 -07004551 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004552
4553 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004554 * A bound idle worker should actually be on the runqueue
4555 * of the associated CPU for local wake-ups targeting it to
4556 * work. Kick all idle workers so that they migrate to the
4557 * associated CPU. Doing this in the same loop as
4558 * replacing UNBOUND with REBOUND is safe as no worker will
4559 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004560 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004561 if (worker_flags & WORKER_IDLE)
4562 wake_up_process(worker->task);
4563
4564 /*
4565 * We want to clear UNBOUND but can't directly call
4566 * worker_clr_flags() or adjust nr_running. Atomically
4567 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4568 * @worker will clear REBOUND using worker_clr_flags() when
4569 * it initiates the next execution cycle thus restoring
4570 * concurrency management. Note that when or whether
4571 * @worker clears REBOUND doesn't affect correctness.
4572 *
4573 * ACCESS_ONCE() is necessary because @worker->flags may be
4574 * tested without holding any lock in
4575 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4576 * fail incorrectly leading to premature concurrency
4577 * management operations.
4578 */
4579 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4580 worker_flags |= WORKER_REBOUND;
4581 worker_flags &= ~WORKER_UNBOUND;
4582 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004583 }
4584
Tejun Heoa9ab7752013-03-19 13:45:21 -07004585 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004586}
4587
Tejun Heo7dbc7252013-03-19 13:45:21 -07004588/**
4589 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4590 * @pool: unbound pool of interest
4591 * @cpu: the CPU which is coming up
4592 *
4593 * An unbound pool may end up with a cpumask which doesn't have any online
4594 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4595 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4596 * online CPU before, cpus_allowed of all its workers should be restored.
4597 */
4598static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4599{
4600 static cpumask_t cpumask;
4601 struct worker *worker;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004602
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004603 lockdep_assert_held(&pool->attach_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004604
4605 /* is @cpu allowed for @pool? */
4606 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4607 return;
4608
4609 /* is @cpu the only online CPU? */
4610 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4611 if (cpumask_weight(&cpumask) != 1)
4612 return;
4613
4614 /* as we're called from CPU_ONLINE, the following shouldn't fail */
Lai Jiangshanda028462014-05-20 17:46:31 +08004615 for_each_pool_worker(worker, pool)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004616 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4617 pool->attrs->cpumask) < 0);
4618}
4619
Tejun Heo8db25e72012-07-17 12:39:28 -07004620/*
4621 * Workqueues should be brought up before normal priority CPU notifiers.
4622 * This will be registered high priority CPU notifier.
4623 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004624static int workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004625 unsigned long action,
4626 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004627{
Tejun Heod84ff052013-03-12 11:29:59 -07004628 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004629 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004630 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004631 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Tejun Heo8db25e72012-07-17 12:39:28 -07004633 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004635 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004636 if (pool->nr_workers)
4637 continue;
Lai Jiangshan051e1852014-07-22 13:03:02 +08004638 if (!create_worker(pool))
Tejun Heo3ce63372012-07-17 12:39:27 -07004639 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004641 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004642
Tejun Heo65758202012-07-17 12:39:26 -07004643 case CPU_DOWN_FAILED:
4644 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004645 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004646
4647 for_each_pool(pool, pi) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004648 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004649
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004650 if (pool->cpu == cpu)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004651 rebind_workers(pool);
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004652 else if (pool->cpu < 0)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004653 restore_unbound_workers_cpumask(pool, cpu);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004654
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004655 mutex_unlock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004656 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004657
Tejun Heo4c16bd32013-04-01 11:23:36 -07004658 /* update NUMA affinity of unbound workqueues */
4659 list_for_each_entry(wq, &workqueues, list)
4660 wq_update_unbound_numa(wq, cpu, true);
4661
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004662 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004663 break;
Tejun Heo65758202012-07-17 12:39:26 -07004664 }
4665 return NOTIFY_OK;
4666}
4667
4668/*
4669 * Workqueues should be brought down after normal priority CPU notifiers.
4670 * This will be registered as low priority CPU notifier.
4671 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004672static int workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004673 unsigned long action,
4674 void *hcpu)
4675{
Tejun Heod84ff052013-03-12 11:29:59 -07004676 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004677 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004678 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004679
Tejun Heo65758202012-07-17 12:39:26 -07004680 switch (action & ~CPU_TASKS_FROZEN) {
4681 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004682 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004683 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004684 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004685
4686 /* update NUMA affinity of unbound workqueues */
4687 mutex_lock(&wq_pool_mutex);
4688 list_for_each_entry(wq, &workqueues, list)
4689 wq_update_unbound_numa(wq, cpu, false);
4690 mutex_unlock(&wq_pool_mutex);
4691
4692 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004693 flush_work(&unbind_work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004694 destroy_work_on_stack(&unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004695 break;
Tejun Heo65758202012-07-17 12:39:26 -07004696 }
4697 return NOTIFY_OK;
4698}
4699
Rusty Russell2d3854a2008-11-05 13:39:10 +11004700#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004701
Rusty Russell2d3854a2008-11-05 13:39:10 +11004702struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004703 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004704 long (*fn)(void *);
4705 void *arg;
4706 long ret;
4707};
4708
Tejun Heoed48ece2012-09-18 12:48:43 -07004709static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004710{
Tejun Heoed48ece2012-09-18 12:48:43 -07004711 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4712
Rusty Russell2d3854a2008-11-05 13:39:10 +11004713 wfc->ret = wfc->fn(wfc->arg);
4714}
4715
4716/**
4717 * work_on_cpu - run a function in user context on a particular cpu
4718 * @cpu: the cpu to run on
4719 * @fn: the function to run
4720 * @arg: the function arg
4721 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004722 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004723 * The caller must not hold any locks which would prevent @fn from completing.
Yacine Belkadid185af32013-07-31 14:59:24 -07004724 *
4725 * Return: The value @fn returns.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004726 */
Tejun Heod84ff052013-03-12 11:29:59 -07004727long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004728{
Tejun Heoed48ece2012-09-18 12:48:43 -07004729 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004730
Tejun Heoed48ece2012-09-18 12:48:43 -07004731 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4732 schedule_work_on(cpu, &wfc.work);
Bjorn Helgaas12997d12013-11-18 11:00:29 -07004733 flush_work(&wfc.work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004734 destroy_work_on_stack(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004735 return wfc.ret;
4736}
4737EXPORT_SYMBOL_GPL(work_on_cpu);
4738#endif /* CONFIG_SMP */
4739
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004740#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304741
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004742/**
4743 * freeze_workqueues_begin - begin freezing workqueues
4744 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004745 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004746 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004747 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004748 *
4749 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004750 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004751 */
4752void freeze_workqueues_begin(void)
4753{
Tejun Heo24b8a842013-03-12 11:29:58 -07004754 struct workqueue_struct *wq;
4755 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004756
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004757 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004758
Tejun Heo6183c002013-03-12 11:29:57 -07004759 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004760 workqueue_freezing = true;
4761
Tejun Heo24b8a842013-03-12 11:29:58 -07004762 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004763 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004764 for_each_pwq(pwq, wq)
4765 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004766 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004767 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004768
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004769 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004771
4772/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004773 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004774 *
4775 * Check whether freezing is complete. This function must be called
4776 * between freeze_workqueues_begin() and thaw_workqueues().
4777 *
4778 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004779 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004780 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004781 * Return:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004782 * %true if some freezable workqueues are still busy. %false if freezing
4783 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004784 */
4785bool freeze_workqueues_busy(void)
4786{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004787 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004788 struct workqueue_struct *wq;
4789 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004790
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004791 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004792
Tejun Heo6183c002013-03-12 11:29:57 -07004793 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004794
Tejun Heo24b8a842013-03-12 11:29:58 -07004795 list_for_each_entry(wq, &workqueues, list) {
4796 if (!(wq->flags & WQ_FREEZABLE))
4797 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004798 /*
4799 * nr_active is monotonically decreasing. It's safe
4800 * to peek without lock.
4801 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004802 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004803 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004804 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004805 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004806 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004807 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004808 goto out_unlock;
4809 }
4810 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004811 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004812 }
4813out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004814 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004815 return busy;
4816}
4817
4818/**
4819 * thaw_workqueues - thaw workqueues
4820 *
4821 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004822 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004823 *
4824 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004825 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004826 */
4827void thaw_workqueues(void)
4828{
Tejun Heo24b8a842013-03-12 11:29:58 -07004829 struct workqueue_struct *wq;
4830 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004831
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004832 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004833
4834 if (!workqueue_freezing)
4835 goto out_unlock;
4836
Lai Jiangshan74b414e2014-05-22 19:01:16 +08004837 workqueue_freezing = false;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004838
Tejun Heo24b8a842013-03-12 11:29:58 -07004839 /* restore max_active and repopulate worklist */
4840 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004841 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004842 for_each_pwq(pwq, wq)
4843 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004844 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004845 }
4846
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004847out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004848 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004849}
4850#endif /* CONFIG_FREEZER */
4851
Tejun Heobce90382013-04-01 11:23:32 -07004852static void __init wq_numa_init(void)
4853{
4854 cpumask_var_t *tbl;
4855 int node, cpu;
4856
Tejun Heobce90382013-04-01 11:23:32 -07004857 if (num_possible_nodes() <= 1)
4858 return;
4859
Tejun Heod55262c2013-04-01 11:23:38 -07004860 if (wq_disable_numa) {
4861 pr_info("workqueue: NUMA affinity support disabled\n");
4862 return;
4863 }
4864
Tejun Heo4c16bd32013-04-01 11:23:36 -07004865 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4866 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4867
Tejun Heobce90382013-04-01 11:23:32 -07004868 /*
4869 * We want masks of possible CPUs of each node which isn't readily
4870 * available. Build one from cpu_to_node() which should have been
4871 * fully initialized by now.
4872 */
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08004873 tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL);
Tejun Heobce90382013-04-01 11:23:32 -07004874 BUG_ON(!tbl);
4875
4876 for_each_node(node)
Yasuaki Ishimatsu5a6024f2014-07-07 09:56:48 -04004877 BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
Tejun Heo1be0c252013-05-15 14:24:24 -07004878 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07004879
4880 for_each_possible_cpu(cpu) {
4881 node = cpu_to_node(cpu);
4882 if (WARN_ON(node == NUMA_NO_NODE)) {
4883 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4884 /* happens iff arch is bonkers, let's just proceed */
4885 return;
4886 }
4887 cpumask_set_cpu(cpu, tbl[node]);
4888 }
4889
4890 wq_numa_possible_cpumask = tbl;
4891 wq_numa_enabled = true;
4892}
4893
Suresh Siddha6ee05782010-07-30 14:57:37 -07004894static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004896 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4897 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004898
Tejun Heoe904e6c2013-03-12 11:29:57 -07004899 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4900
4901 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4902
Tejun Heo65758202012-07-17 12:39:26 -07004903 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004904 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004905
Tejun Heobce90382013-04-01 11:23:32 -07004906 wq_numa_init();
4907
Tejun Heo706026c2013-01-24 11:01:34 -08004908 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004909 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004910 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004911
Tejun Heo7a4e3442013-03-12 11:30:00 -07004912 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004913 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004914 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004915 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004916 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004917 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004918 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004919
Tejun Heo9daf9e62013-01-24 11:01:33 -08004920 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004921 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004922 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004923 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004924 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004925 }
4926
Tejun Heoe22bee72010-06-29 10:07:14 +02004927 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004928 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004929 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004930
Tejun Heof02ae732013-03-12 11:30:03 -07004931 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004932 pool->flags &= ~POOL_DISASSOCIATED;
Lai Jiangshan051e1852014-07-22 13:03:02 +08004933 BUG_ON(!create_worker(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07004934 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004935 }
4936
Tejun Heo8a2b7532013-09-05 12:30:04 -04004937 /* create default unbound and ordered wq attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -07004938 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4939 struct workqueue_attrs *attrs;
4940
4941 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004942 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004943 unbound_std_wq_attrs[i] = attrs;
Tejun Heo8a2b7532013-09-05 12:30:04 -04004944
4945 /*
4946 * An ordered wq should have only one pwq as ordering is
4947 * guaranteed by max_active which is enforced by pwqs.
4948 * Turn off NUMA so that dfl_pwq is used for all nodes.
4949 */
4950 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4951 attrs->nice = std_nice[i];
4952 attrs->no_numa = true;
4953 ordered_wq_attrs[i] = attrs;
Tejun Heo29c91e92013-03-12 11:30:03 -07004954 }
4955
Tejun Heod320c032010-06-29 10:07:14 +02004956 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004957 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004958 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004959 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4960 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004961 system_freezable_wq = alloc_workqueue("events_freezable",
4962 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05304963 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
4964 WQ_POWER_EFFICIENT, 0);
4965 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
4966 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
4967 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004968 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05304969 !system_unbound_wq || !system_freezable_wq ||
4970 !system_power_efficient_wq ||
4971 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004974early_initcall(init_workqueues);