blob: 10f655ec8de6ebd5d222043a5414c1bc95518d32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
Libinb11895c2013-08-21 08:50:39 +080019 * automatically managed. There are two worker pools for each CPU (one for
20 * normal work items and the other for high priority ones) and some extra
21 * pools for workqueues which are not bound to any specific CPU - the
22 * number of these backing pools is dynamic.
Tejun Heoc54fce62010-09-10 16:51:36 +020023 *
24 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Paul Gortmaker9984de12011-05-23 14:51:41 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/sched.h>
30#include <linux/init.h>
31#include <linux/signal.h>
32#include <linux/completion.h>
33#include <linux/workqueue.h>
34#include <linux/slab.h>
35#include <linux/cpu.h>
36#include <linux/notifier.h>
37#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060038#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070039#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080040#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080041#include <linux/kallsyms.h>
42#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070043#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020044#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070045#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050046#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070047#include <linux/rculist.h>
Tejun Heobce90382013-04-01 11:23:32 -070048#include <linux/nodemask.h>
Tejun Heo4c16bd32013-04-01 11:23:36 -070049#include <linux/moduleparam.h>
Tejun Heo3d1cb202013-04-30 15:27:22 -070050#include <linux/uaccess.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020051
Tejun Heoea138442013-01-18 14:05:55 -080052#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tejun Heoc8e55f32010-06-29 10:07:12 +020054enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 /*
Tejun Heo24647572013-01-24 11:01:33 -080056 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070057 *
Tejun Heo24647572013-01-24 11:01:33 -080058 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070059 * While associated (!DISASSOCIATED), all workers are bound to the
60 * CPU and none has %WORKER_UNBOUND set and concurrency management
61 * is in effect.
62 *
63 * While DISASSOCIATED, the cpu may be offline and all workers have
64 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080065 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070067 * Note that DISASSOCIATED should be flipped only while holding
68 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080069 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070070 */
Tejun Heo11ebea52012-07-12 14:46:37 -070071 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080072 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080073 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020074
Tejun Heoc8e55f32010-06-29 10:07:12 +020075 /* worker flags */
76 WORKER_STARTED = 1 << 0, /* started */
77 WORKER_DIE = 1 << 1, /* die die die */
78 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020079 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020080 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020081 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070082 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020083
Tejun Heoa9ab7752013-03-19 13:45:21 -070084 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
85 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020086
Tejun Heoe34cdddb2013-01-24 11:01:33 -080087 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070088
Tejun Heo29c91e92013-03-12 11:30:03 -070089 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020090 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091
Tejun Heoe22bee72010-06-29 10:07:14 +020092 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
93 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
94
Tejun Heo3233cdb2011-02-16 18:10:19 +010095 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
96 /* call for help after 10ms
97 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020098 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
99 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +0200100
101 /*
102 * Rescue workers are used only on emergencies and shared by
103 * all cpus. Give -20.
104 */
105 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700106 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoecf68812013-04-01 11:23:34 -0700107
108 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 * Structure fields follow one of the following exclusion rules.
113 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200114 * I: Modifiable by initialization/destruction paths and read-only for
115 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200116 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 * P: Preemption protected. Disabling preemption is enough and should
118 * only be modified and accessed from the local cpu.
119 *
Tejun Heod565ed62013-01-24 11:01:33 -0800120 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200121 *
Tejun Heod565ed62013-01-24 11:01:33 -0800122 * X: During normal operation, modification requires pool->lock and should
123 * be done only from local cpu. Either disabling preemption on local
124 * cpu or grabbing pool->lock is enough for read access. If
125 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200126 *
Tejun Heo822d8402013-03-19 13:45:21 -0700127 * MG: pool->manager_mutex and pool->lock protected. Writes require both
128 * locks. Reads can happen under either lock.
129 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700130 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700131 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700132 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700133 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700134 * WQ: wq->mutex protected.
135 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700136 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700137 *
138 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200139 */
140
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800141/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200142
Tejun Heobd7bdd42012-07-12 14:46:37 -0700143struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800144 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700145 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700146 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800147 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700148 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149
150 struct list_head worklist; /* L: list of pending works */
151 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700152
153 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700154 int nr_idle; /* L: currently idle ones */
155
156 struct list_head idle_list; /* X: list of idle workers */
157 struct timer_list idle_timer; /* L: worker idle timeout */
158 struct timer_list mayday_timer; /* L: SOS timer for workers */
159
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700160 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800161 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
162 /* L: hash of busy workers */
163
Tejun Heobc3a1af2013-03-13 19:47:39 -0700164 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700165 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700166 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700167 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800168
Tejun Heo7a4e3442013-03-12 11:30:00 -0700169 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700170 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
171 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700172
Tejun Heoe19e3972013-01-24 11:39:44 -0800173 /*
174 * The current concurrency level. As it's likely to be accessed
175 * from other CPUs during try_to_wake_up(), put it in a separate
176 * cacheline.
177 */
178 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700179
180 /*
181 * Destruction of pool is sched-RCU protected to allow dereferences
182 * from get_work_pool().
183 */
184 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200185} ____cacheline_aligned_in_smp;
186
187/*
Tejun Heo112202d2013-02-13 19:29:12 -0800188 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
189 * of work_struct->data are used for flags and the remaining high bits
190 * point to the pwq; thus, pwqs need to be aligned at two's power of the
191 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Tejun Heo112202d2013-02-13 19:29:12 -0800193struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700194 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200195 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200196 int work_color; /* L: current color */
197 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700198 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200199 int nr_in_flight[WORK_NR_COLORS];
200 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200201 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200202 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200203 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700204 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700205 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700206
207 /*
208 * Release of unbound pwq is punted to system_wq. See put_pwq()
209 * and pwq_unbound_release_workfn() for details. pool_workqueue
210 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700211 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700212 */
213 struct work_struct unbound_release_work;
214 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700215} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200218 * Structure used to wait for workqueue flush.
219 */
220struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700221 struct list_head list; /* WQ: list of flushers */
222 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200223 struct completion done; /* flush completion */
224};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo226223a2013-03-12 11:30:05 -0700226struct wq_device;
227
Tejun Heo73f53c42010-06-29 10:07:11 +0200228/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700229 * The externally visible workqueue. It relays the issued work items to
230 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700233 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700234 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200235
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700236 struct mutex mutex; /* protects this wq */
237 int work_color; /* WQ: current work color */
238 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800239 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700240 struct wq_flusher *first_flusher; /* WQ: first flusher */
241 struct list_head flusher_queue; /* WQ: flush waiters */
242 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200243
Tejun Heo2e109a22013-03-13 19:47:40 -0700244 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200245 struct worker *rescuer; /* I: rescue worker */
246
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700247 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700248 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700249
Tejun Heo6029a912013-04-01 11:23:34 -0700250 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
Tejun Heo4c16bd32013-04-01 11:23:36 -0700251 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700252
Tejun Heo226223a2013-03-12 11:30:05 -0700253#ifdef CONFIG_SYSFS
254 struct wq_device *wq_dev; /* I: for sysfs interface */
255#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700256#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200257 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700259 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700260
261 /* hot fields used during command issue, aligned to cacheline */
262 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
263 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700264 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Tejun Heoe904e6c2013-03-12 11:29:57 -0700267static struct kmem_cache *pwq_cache;
268
Tejun Heobce90382013-04-01 11:23:32 -0700269static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
270static cpumask_var_t *wq_numa_possible_cpumask;
271 /* possible CPUs of each node */
272
Tejun Heod55262c2013-04-01 11:23:38 -0700273static bool wq_disable_numa;
274module_param_named(disable_numa, wq_disable_numa, bool, 0444);
275
Viresh Kumarcee22a12013-04-08 16:45:40 +0530276/* see the comment above the definition of WQ_POWER_EFFICIENT */
277#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
278static bool wq_power_efficient = true;
279#else
280static bool wq_power_efficient;
281#endif
282
283module_param_named(power_efficient, wq_power_efficient, bool, 0444);
284
Tejun Heobce90382013-04-01 11:23:32 -0700285static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
286
Tejun Heo4c16bd32013-04-01 11:23:36 -0700287/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
288static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
289
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700290static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700291static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700292
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700293static LIST_HEAD(workqueues); /* PL: list of all workqueues */
294static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700295
296/* the per-cpu worker pools */
297static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
298 cpu_worker_pools);
299
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700300static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700301
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700302/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700303static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
304
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700305/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700306static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
307
Tejun Heod320c032010-06-29 10:07:14 +0200308struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400309EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300310struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900311EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300312struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200313EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300314struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200315EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300316struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100317EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530318struct workqueue_struct *system_power_efficient_wq __read_mostly;
319EXPORT_SYMBOL_GPL(system_power_efficient_wq);
320struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
321EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200322
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700323static int worker_thread(void *__worker);
324static void copy_workqueue_attrs(struct workqueue_attrs *to,
325 const struct workqueue_attrs *from);
326
Tejun Heo97bd2342010-10-05 10:41:14 +0200327#define CREATE_TRACE_POINTS
328#include <trace/events/workqueue.h>
329
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700330#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700331 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700332 lockdep_is_held(&wq_pool_mutex), \
333 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700334
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700335#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700336 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700337 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700338 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700339
Tejun Heo822d8402013-03-19 13:45:21 -0700340#ifdef CONFIG_LOCKDEP
341#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800342 WARN_ONCE(debug_locks && \
343 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700344 !lockdep_is_held(&(pool)->lock), \
345 "pool->manager_mutex or ->lock should be held")
346#else
347#define assert_manager_or_pool_lock(pool) do { } while (0)
348#endif
349
Tejun Heof02ae732013-03-12 11:30:03 -0700350#define for_each_cpu_worker_pool(pool, cpu) \
351 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
352 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700353 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700354
Tejun Heo49e3cf42013-03-12 11:29:58 -0700355/**
Tejun Heo17116962013-03-12 11:29:58 -0700356 * for_each_pool - iterate through all worker_pools in the system
357 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700358 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700359 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700360 * This must be called either with wq_pool_mutex held or sched RCU read
361 * locked. If the pool needs to be used beyond the locking in effect, the
362 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700363 *
364 * The if/else clause exists only for the lockdep assertion and can be
365 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700366 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700367#define for_each_pool(pool, pi) \
368 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700369 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700370 else
Tejun Heo17116962013-03-12 11:29:58 -0700371
372/**
Tejun Heo822d8402013-03-19 13:45:21 -0700373 * for_each_pool_worker - iterate through all workers of a worker_pool
374 * @worker: iteration cursor
375 * @wi: integer used for iteration
376 * @pool: worker_pool to iterate workers of
377 *
378 * This must be called with either @pool->manager_mutex or ->lock held.
379 *
380 * The if/else clause exists only for the lockdep assertion and can be
381 * ignored.
382 */
383#define for_each_pool_worker(worker, wi, pool) \
384 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
385 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
386 else
387
388/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700389 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
390 * @pwq: iteration cursor
391 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700392 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700393 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700394 * If the pwq needs to be used beyond the locking in effect, the caller is
395 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700396 *
397 * The if/else clause exists only for the lockdep assertion and can be
398 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700399 */
400#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700401 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700402 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700403 else
Tejun Heof3421792010-07-02 10:03:51 +0200404
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900405#ifdef CONFIG_DEBUG_OBJECTS_WORK
406
407static struct debug_obj_descr work_debug_descr;
408
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100409static void *work_debug_hint(void *addr)
410{
411 return ((struct work_struct *) addr)->func;
412}
413
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900414/*
415 * fixup_init is called when:
416 * - an active object is initialized
417 */
418static int work_fixup_init(void *addr, enum debug_obj_state state)
419{
420 struct work_struct *work = addr;
421
422 switch (state) {
423 case ODEBUG_STATE_ACTIVE:
424 cancel_work_sync(work);
425 debug_object_init(work, &work_debug_descr);
426 return 1;
427 default:
428 return 0;
429 }
430}
431
432/*
433 * fixup_activate is called when:
434 * - an active object is activated
435 * - an unknown object is activated (might be a statically initialized object)
436 */
437static int work_fixup_activate(void *addr, enum debug_obj_state state)
438{
439 struct work_struct *work = addr;
440
441 switch (state) {
442
443 case ODEBUG_STATE_NOTAVAILABLE:
444 /*
445 * This is not really a fixup. The work struct was
446 * statically initialized. We just make sure that it
447 * is tracked in the object tracker.
448 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200449 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900450 debug_object_init(work, &work_debug_descr);
451 debug_object_activate(work, &work_debug_descr);
452 return 0;
453 }
454 WARN_ON_ONCE(1);
455 return 0;
456
457 case ODEBUG_STATE_ACTIVE:
458 WARN_ON(1);
459
460 default:
461 return 0;
462 }
463}
464
465/*
466 * fixup_free is called when:
467 * - an active object is freed
468 */
469static int work_fixup_free(void *addr, enum debug_obj_state state)
470{
471 struct work_struct *work = addr;
472
473 switch (state) {
474 case ODEBUG_STATE_ACTIVE:
475 cancel_work_sync(work);
476 debug_object_free(work, &work_debug_descr);
477 return 1;
478 default:
479 return 0;
480 }
481}
482
483static struct debug_obj_descr work_debug_descr = {
484 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100485 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900486 .fixup_init = work_fixup_init,
487 .fixup_activate = work_fixup_activate,
488 .fixup_free = work_fixup_free,
489};
490
491static inline void debug_work_activate(struct work_struct *work)
492{
493 debug_object_activate(work, &work_debug_descr);
494}
495
496static inline void debug_work_deactivate(struct work_struct *work)
497{
498 debug_object_deactivate(work, &work_debug_descr);
499}
500
501void __init_work(struct work_struct *work, int onstack)
502{
503 if (onstack)
504 debug_object_init_on_stack(work, &work_debug_descr);
505 else
506 debug_object_init(work, &work_debug_descr);
507}
508EXPORT_SYMBOL_GPL(__init_work);
509
510void destroy_work_on_stack(struct work_struct *work)
511{
512 debug_object_free(work, &work_debug_descr);
513}
514EXPORT_SYMBOL_GPL(destroy_work_on_stack);
515
516#else
517static inline void debug_work_activate(struct work_struct *work) { }
518static inline void debug_work_deactivate(struct work_struct *work) { }
519#endif
520
Tejun Heo9daf9e62013-01-24 11:01:33 -0800521/* allocate ID and assign it to @pool */
522static int worker_pool_assign_id(struct worker_pool *pool)
523{
524 int ret;
525
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700526 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700527
Tejun Heoe68035f2013-03-13 14:59:38 -0700528 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700529 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700530 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700531 return 0;
532 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800533 return ret;
534}
535
Tejun Heo76af4d92013-03-12 11:30:00 -0700536/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700537 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
538 * @wq: the target workqueue
539 * @node: the node ID
540 *
541 * This must be called either with pwq_lock held or sched RCU read locked.
542 * If the pwq needs to be used beyond the locking in effect, the caller is
543 * responsible for guaranteeing that the pwq stays online.
544 */
545static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
546 int node)
547{
548 assert_rcu_or_wq_mutex(wq);
549 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
550}
551
Tejun Heo73f53c42010-06-29 10:07:11 +0200552static unsigned int work_color_to_flags(int color)
553{
554 return color << WORK_STRUCT_COLOR_SHIFT;
555}
556
557static int get_work_color(struct work_struct *work)
558{
559 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
560 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
561}
562
563static int work_next_color(int color)
564{
565 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700566}
567
David Howells4594bf12006-12-07 11:33:26 +0000568/*
Tejun Heo112202d2013-02-13 19:29:12 -0800569 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
570 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800571 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572 *
Tejun Heo112202d2013-02-13 19:29:12 -0800573 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
574 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700575 * work->data. These functions should only be called while the work is
576 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577 *
Tejun Heo112202d2013-02-13 19:29:12 -0800578 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800579 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800580 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800581 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700582 *
583 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
584 * canceled. While being canceled, a work item may have its PENDING set
585 * but stay off timer and worklist for arbitrarily long and nobody should
586 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000587 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200588static inline void set_work_data(struct work_struct *work, unsigned long data,
589 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000590{
Tejun Heo6183c002013-03-12 11:29:57 -0700591 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000593}
David Howells365970a2006-11-22 14:54:49 +0000594
Tejun Heo112202d2013-02-13 19:29:12 -0800595static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200597{
Tejun Heo112202d2013-02-13 19:29:12 -0800598 set_work_data(work, (unsigned long)pwq,
599 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200600}
601
Lai Jiangshan4468a002013-02-06 18:04:53 -0800602static void set_work_pool_and_keep_pending(struct work_struct *work,
603 int pool_id)
604{
605 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
606 WORK_STRUCT_PENDING);
607}
608
Tejun Heo7c3eed52013-01-24 11:01:33 -0800609static void set_work_pool_and_clear_pending(struct work_struct *work,
610 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000611{
Tejun Heo23657bb2012-08-13 17:08:19 -0700612 /*
613 * The following wmb is paired with the implied mb in
614 * test_and_set_bit(PENDING) and ensures all updates to @work made
615 * here are visible to and precede any updates by the next PENDING
616 * owner.
617 */
618 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800619 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200620}
621
622static void clear_work_data(struct work_struct *work)
623{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800624 smp_wmb(); /* see set_work_pool_and_clear_pending() */
625 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200626}
627
Tejun Heo112202d2013-02-13 19:29:12 -0800628static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200629{
Tejun Heoe1201532010-07-22 14:14:25 +0200630 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200631
Tejun Heo112202d2013-02-13 19:29:12 -0800632 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200633 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
634 else
635 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200636}
637
Tejun Heo7c3eed52013-01-24 11:01:33 -0800638/**
639 * get_work_pool - return the worker_pool a given work was associated with
640 * @work: the work item of interest
641 *
642 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700643 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700644 * Pools are created and destroyed under wq_pool_mutex, and allows read
645 * access under sched-RCU read lock. As such, this function should be
646 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700647 *
648 * All fields of the returned pool are accessible as long as the above
649 * mentioned locking is in effect. If the returned pool needs to be used
650 * beyond the critical section, the caller is responsible for ensuring the
651 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800652 */
653static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200654{
Tejun Heoe1201532010-07-22 14:14:25 +0200655 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800656 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200657
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700658 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700659
Tejun Heo112202d2013-02-13 19:29:12 -0800660 if (data & WORK_STRUCT_PWQ)
661 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200663
Tejun Heo7c3eed52013-01-24 11:01:33 -0800664 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
665 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200666 return NULL;
667
Tejun Heofa1b54e2013-03-12 11:30:00 -0700668 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800669}
670
671/**
672 * get_work_pool_id - return the worker pool ID a given work is associated with
673 * @work: the work item of interest
674 *
675 * Return the worker_pool ID @work was last associated with.
676 * %WORK_OFFQ_POOL_NONE if none.
677 */
678static int get_work_pool_id(struct work_struct *work)
679{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800680 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800681
Tejun Heo112202d2013-02-13 19:29:12 -0800682 if (data & WORK_STRUCT_PWQ)
683 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800684 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
685
686 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800687}
688
Tejun Heobbb68df2012-08-03 10:30:46 -0700689static void mark_work_canceling(struct work_struct *work)
690{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800691 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700692
Tejun Heo7c3eed52013-01-24 11:01:33 -0800693 pool_id <<= WORK_OFFQ_POOL_SHIFT;
694 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700695}
696
697static bool work_is_canceling(struct work_struct *work)
698{
699 unsigned long data = atomic_long_read(&work->data);
700
Tejun Heo112202d2013-02-13 19:29:12 -0800701 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700702}
703
David Howells365970a2006-11-22 14:54:49 +0000704/*
Tejun Heo32704762012-07-13 22:16:45 -0700705 * Policy functions. These define the policies on how the global worker
706 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800707 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000708 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200709
Tejun Heo63d95a92012-07-12 14:46:37 -0700710static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000711{
Tejun Heoe19e3972013-01-24 11:39:44 -0800712 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000713}
714
Tejun Heoe22bee72010-06-29 10:07:14 +0200715/*
716 * Need to wake up a worker? Called from anything but currently
717 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700718 *
719 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800720 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700721 * worklist isn't empty.
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 Heo63d95a92012-07-12 14:46:37 -0700725 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000726}
727
Tejun Heoe22bee72010-06-29 10:07:14 +0200728/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700729static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200730{
Tejun Heo63d95a92012-07-12 14:46:37 -0700731 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200732}
733
734/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700735static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200736{
Tejun Heoe19e3972013-01-24 11:39:44 -0800737 return !list_empty(&pool->worklist) &&
738 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200739}
740
741/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700742static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200743{
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200745}
746
747/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700748static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200749{
Tejun Heo63d95a92012-07-12 14:46:37 -0700750 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700751 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200752}
753
754/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700755static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200756{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700757 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700758 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
759 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200760
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700761 /*
762 * nr_idle and idle_list may disagree if idle rebinding is in
763 * progress. Never return %true if idle_list is empty.
764 */
765 if (list_empty(&pool->idle_list))
766 return false;
767
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
769}
770
771/*
772 * Wake up functions.
773 */
774
Tejun Heo7e116292010-06-29 10:07:13 +0200775/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700776static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200777{
Tejun Heo63d95a92012-07-12 14:46:37 -0700778 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200779 return NULL;
780
Tejun Heo63d95a92012-07-12 14:46:37 -0700781 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200782}
783
784/**
785 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700786 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200787 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700788 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200789 *
790 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800791 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200792 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700793static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200794{
Tejun Heo63d95a92012-07-12 14:46:37 -0700795 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200796
797 if (likely(worker))
798 wake_up_process(worker->task);
799}
800
Tejun Heo4690c4a2010-06-29 10:07:10 +0200801/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200802 * wq_worker_waking_up - a worker is waking up
803 * @task: task waking up
804 * @cpu: CPU @task is waking up to
805 *
806 * This function is called during try_to_wake_up() when a worker is
807 * being awoken.
808 *
809 * CONTEXT:
810 * spin_lock_irq(rq->lock)
811 */
Tejun Heod84ff052013-03-12 11:29:59 -0700812void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200813{
814 struct worker *worker = kthread_data(task);
815
Joonsoo Kim36576002012-10-26 23:03:49 +0900816 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800817 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800818 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900819 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200820}
821
822/**
823 * wq_worker_sleeping - a worker is going to sleep
824 * @task: task going to sleep
825 * @cpu: CPU in question, must be the current CPU number
826 *
827 * This function is called during schedule() when a busy worker is
828 * going to sleep. Worker on the same cpu can be woken up by
829 * returning pointer to its task.
830 *
831 * CONTEXT:
832 * spin_lock_irq(rq->lock)
833 *
834 * RETURNS:
835 * Worker task on @cpu to wake up, %NULL if none.
836 */
Tejun Heod84ff052013-03-12 11:29:59 -0700837struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200838{
839 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800840 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200841
Tejun Heo111c2252013-01-17 17:16:24 -0800842 /*
843 * Rescuers, which may not have all the fields set up like normal
844 * workers, also reach here, let's not access anything before
845 * checking NOT_RUNNING.
846 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500847 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200848 return NULL;
849
Tejun Heo111c2252013-01-17 17:16:24 -0800850 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800851
Tejun Heoe22bee72010-06-29 10:07:14 +0200852 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700853 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
854 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200855
856 /*
857 * The counterpart of the following dec_and_test, implied mb,
858 * worklist not empty test sequence is in insert_work().
859 * Please read comment there.
860 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700861 * NOT_RUNNING is clear. This means that we're bound to and
862 * running on the local cpu w/ rq lock held and preemption
863 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800864 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700865 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200866 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800867 if (atomic_dec_and_test(&pool->nr_running) &&
868 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700869 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200870 return to_wakeup ? to_wakeup->task : NULL;
871}
872
873/**
874 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200875 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200876 * @flags: flags to set
877 * @wakeup: wakeup an idle worker if necessary
878 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200879 * Set @flags in @worker->flags and adjust nr_running accordingly. If
880 * nr_running becomes zero and @wakeup is %true, an idle worker is
881 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200882 *
Tejun Heocb444762010-07-02 10:03:50 +0200883 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800884 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200885 */
886static inline void worker_set_flags(struct worker *worker, unsigned int flags,
887 bool wakeup)
888{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700889 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200890
Tejun Heocb444762010-07-02 10:03:50 +0200891 WARN_ON_ONCE(worker->task != current);
892
Tejun Heoe22bee72010-06-29 10:07:14 +0200893 /*
894 * If transitioning into NOT_RUNNING, adjust nr_running and
895 * wake up an idle worker as necessary if requested by
896 * @wakeup.
897 */
898 if ((flags & WORKER_NOT_RUNNING) &&
899 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200900 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800901 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700902 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700903 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200904 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800905 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200906 }
907
Tejun Heod302f012010-06-29 10:07:13 +0200908 worker->flags |= flags;
909}
910
911/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200912 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200913 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200914 * @flags: flags to clear
915 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200916 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200917 *
Tejun Heocb444762010-07-02 10:03:50 +0200918 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800919 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200920 */
921static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
922{
Tejun Heo63d95a92012-07-12 14:46:37 -0700923 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200924 unsigned int oflags = worker->flags;
925
Tejun Heocb444762010-07-02 10:03:50 +0200926 WARN_ON_ONCE(worker->task != current);
927
Tejun Heod302f012010-06-29 10:07:13 +0200928 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200929
Tejun Heo42c025f2011-01-11 15:58:49 +0100930 /*
931 * If transitioning out of NOT_RUNNING, increment nr_running. Note
932 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
933 * of multiple flags, not a single flag.
934 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200935 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
936 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800937 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200938}
939
940/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200941 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800942 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200943 * @work: work to find worker for
944 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800945 * Find a worker which is executing @work on @pool by searching
946 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800947 * to match, its current execution should match the address of @work and
948 * its work function. This is to avoid unwanted dependency between
949 * unrelated work executions through a work item being recycled while still
950 * being executed.
951 *
952 * This is a bit tricky. A work item may be freed once its execution
953 * starts and nothing prevents the freed area from being recycled for
954 * another work item. If the same work item address ends up being reused
955 * before the original execution finishes, workqueue will identify the
956 * recycled work item as currently executing and make it wait until the
957 * current execution finishes, introducing an unwanted dependency.
958 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700959 * This function checks the work item address and work function to avoid
960 * false positives. Note that this isn't complete as one may construct a
961 * work function which can introduce dependency onto itself through a
962 * recycled work item. Well, if somebody wants to shoot oneself in the
963 * foot that badly, there's only so much we can do, and if such deadlock
964 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200965 *
966 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800967 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200968 *
969 * RETURNS:
970 * Pointer to worker which is executing @work if found, NULL
971 * otherwise.
972 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800973static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200974 struct work_struct *work)
975{
Sasha Levin42f85702012-12-17 10:01:23 -0500976 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500977
Sasha Levinb67bfe02013-02-27 17:06:00 -0800978 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800979 (unsigned long)work)
980 if (worker->current_work == work &&
981 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500982 return worker;
983
984 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200985}
986
987/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700988 * move_linked_works - move linked works to a list
989 * @work: start of series of works to be scheduled
990 * @head: target list to append @work to
991 * @nextp: out paramter for nested worklist walking
992 *
993 * Schedule linked works starting from @work to @head. Work series to
994 * be scheduled starts at @work and includes any consecutive work with
995 * WORK_STRUCT_LINKED set in its predecessor.
996 *
997 * If @nextp is not NULL, it's updated to point to the next work of
998 * the last scheduled work. This allows move_linked_works() to be
999 * nested inside outer list_for_each_entry_safe().
1000 *
1001 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001002 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001003 */
1004static void move_linked_works(struct work_struct *work, struct list_head *head,
1005 struct work_struct **nextp)
1006{
1007 struct work_struct *n;
1008
1009 /*
1010 * Linked worklist will always end before the end of the list,
1011 * use NULL for list head.
1012 */
1013 list_for_each_entry_safe_from(work, n, NULL, entry) {
1014 list_move_tail(&work->entry, head);
1015 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1016 break;
1017 }
1018
1019 /*
1020 * If we're already inside safe list traversal and have moved
1021 * multiple works to the scheduled queue, the next position
1022 * needs to be updated.
1023 */
1024 if (nextp)
1025 *nextp = n;
1026}
1027
Tejun Heo8864b4e2013-03-12 11:30:04 -07001028/**
1029 * get_pwq - get an extra reference on the specified pool_workqueue
1030 * @pwq: pool_workqueue to get
1031 *
1032 * Obtain an extra reference on @pwq. The caller should guarantee that
1033 * @pwq has positive refcnt and be holding the matching pool->lock.
1034 */
1035static void get_pwq(struct pool_workqueue *pwq)
1036{
1037 lockdep_assert_held(&pwq->pool->lock);
1038 WARN_ON_ONCE(pwq->refcnt <= 0);
1039 pwq->refcnt++;
1040}
1041
1042/**
1043 * put_pwq - put a pool_workqueue reference
1044 * @pwq: pool_workqueue to put
1045 *
1046 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1047 * destruction. The caller should be holding the matching pool->lock.
1048 */
1049static void put_pwq(struct pool_workqueue *pwq)
1050{
1051 lockdep_assert_held(&pwq->pool->lock);
1052 if (likely(--pwq->refcnt))
1053 return;
1054 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1055 return;
1056 /*
1057 * @pwq can't be released under pool->lock, bounce to
1058 * pwq_unbound_release_workfn(). This never recurses on the same
1059 * pool->lock as this path is taken only for unbound workqueues and
1060 * the release work item is scheduled on a per-cpu workqueue. To
1061 * avoid lockdep warning, unbound pool->locks are given lockdep
1062 * subclass of 1 in get_unbound_pool().
1063 */
1064 schedule_work(&pwq->unbound_release_work);
1065}
1066
Tejun Heodce90d42013-04-01 11:23:35 -07001067/**
1068 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1069 * @pwq: pool_workqueue to put (can be %NULL)
1070 *
1071 * put_pwq() with locking. This function also allows %NULL @pwq.
1072 */
1073static void put_pwq_unlocked(struct pool_workqueue *pwq)
1074{
1075 if (pwq) {
1076 /*
1077 * As both pwqs and pools are sched-RCU protected, the
1078 * following lock operations are safe.
1079 */
1080 spin_lock_irq(&pwq->pool->lock);
1081 put_pwq(pwq);
1082 spin_unlock_irq(&pwq->pool->lock);
1083 }
1084}
1085
Tejun Heo112202d2013-02-13 19:29:12 -08001086static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001087{
Tejun Heo112202d2013-02-13 19:29:12 -08001088 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001089
1090 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001091 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001093 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001094}
1095
Tejun Heo112202d2013-02-13 19:29:12 -08001096static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001097{
Tejun Heo112202d2013-02-13 19:29:12 -08001098 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001099 struct work_struct, entry);
1100
Tejun Heo112202d2013-02-13 19:29:12 -08001101 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001102}
1103
Tejun Heobf4ede02012-08-03 10:30:46 -07001104/**
Tejun Heo112202d2013-02-13 19:29:12 -08001105 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1106 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001107 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001108 *
1109 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001110 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001111 *
1112 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001113 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001114 */
Tejun Heo112202d2013-02-13 19:29:12 -08001115static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001116{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001117 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001118 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001119 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001120
Tejun Heo112202d2013-02-13 19:29:12 -08001121 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001122
Tejun Heo112202d2013-02-13 19:29:12 -08001123 pwq->nr_active--;
1124 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001125 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001126 if (pwq->nr_active < pwq->max_active)
1127 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001128 }
1129
1130 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001131 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001132 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001133
1134 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001135 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001136 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001137
Tejun Heo112202d2013-02-13 19:29:12 -08001138 /* this pwq is done, clear flush_color */
1139 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001140
1141 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001142 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001143 * will handle the rest.
1144 */
Tejun Heo112202d2013-02-13 19:29:12 -08001145 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1146 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001147out_put:
1148 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001149}
1150
Tejun Heo36e227d2012-08-03 10:30:46 -07001151/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001152 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001153 * @work: work item to steal
1154 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001155 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001156 *
1157 * Try to grab PENDING bit of @work. This function can handle @work in any
1158 * stable state - idle, on timer or on worklist. Return values are
1159 *
1160 * 1 if @work was pending and we successfully stole PENDING
1161 * 0 if @work was idle and we claimed PENDING
1162 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001163 * -ENOENT if someone else is canceling @work, this state may persist
1164 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001165 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001166 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001167 * interrupted while holding PENDING and @work off queue, irq must be
1168 * disabled on entry. This, combined with delayed_work->timer being
1169 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001170 *
1171 * On successful return, >= 0, irq is disabled and the caller is
1172 * responsible for releasing it using local_irq_restore(*@flags).
1173 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001174 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001175 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001176static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1177 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001178{
Tejun Heod565ed62013-01-24 11:01:33 -08001179 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001180 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001181
Tejun Heobbb68df2012-08-03 10:30:46 -07001182 local_irq_save(*flags);
1183
Tejun Heo36e227d2012-08-03 10:30:46 -07001184 /* try to steal the timer if it exists */
1185 if (is_dwork) {
1186 struct delayed_work *dwork = to_delayed_work(work);
1187
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001188 /*
1189 * dwork->timer is irqsafe. If del_timer() fails, it's
1190 * guaranteed that the timer is not queued anywhere and not
1191 * running on the local CPU.
1192 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001193 if (likely(del_timer(&dwork->timer)))
1194 return 1;
1195 }
1196
1197 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001198 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1199 return 0;
1200
1201 /*
1202 * The queueing is in progress, or it is already queued. Try to
1203 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1204 */
Tejun Heod565ed62013-01-24 11:01:33 -08001205 pool = get_work_pool(work);
1206 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001207 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001208
Tejun Heod565ed62013-01-24 11:01:33 -08001209 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001210 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001211 * work->data is guaranteed to point to pwq only while the work
1212 * item is queued on pwq->wq, and both updating work->data to point
1213 * to pwq on queueing and to pool on dequeueing are done under
1214 * pwq->pool->lock. This in turn guarantees that, if work->data
1215 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001216 * item is currently queued on that pool.
1217 */
Tejun Heo112202d2013-02-13 19:29:12 -08001218 pwq = get_work_pwq(work);
1219 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001220 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001221
Tejun Heo16062832013-02-06 18:04:53 -08001222 /*
1223 * A delayed work item cannot be grabbed directly because
1224 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001225 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001226 * management later on and cause stall. Make sure the work
1227 * item is activated before grabbing.
1228 */
1229 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001230 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001231
Tejun Heo16062832013-02-06 18:04:53 -08001232 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001233 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001234
Tejun Heo112202d2013-02-13 19:29:12 -08001235 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001236 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001237
Tejun Heo16062832013-02-06 18:04:53 -08001238 spin_unlock(&pool->lock);
1239 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001240 }
Tejun Heod565ed62013-01-24 11:01:33 -08001241 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001242fail:
1243 local_irq_restore(*flags);
1244 if (work_is_canceling(work))
1245 return -ENOENT;
1246 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001247 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001248}
1249
1250/**
Tejun Heo706026c2013-01-24 11:01:34 -08001251 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001252 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001253 * @work: work to insert
1254 * @head: insertion point
1255 * @extra_flags: extra WORK_STRUCT_* flags to set
1256 *
Tejun Heo112202d2013-02-13 19:29:12 -08001257 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001258 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001259 *
1260 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001261 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001262 */
Tejun Heo112202d2013-02-13 19:29:12 -08001263static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1264 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001265{
Tejun Heo112202d2013-02-13 19:29:12 -08001266 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001267
Tejun Heo4690c4a2010-06-29 10:07:10 +02001268 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001269 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001270 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001271 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001272
1273 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001274 * Ensure either wq_worker_sleeping() sees the above
1275 * list_add_tail() or we see zero nr_running to avoid workers lying
1276 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001277 */
1278 smp_mb();
1279
Tejun Heo63d95a92012-07-12 14:46:37 -07001280 if (__need_more_worker(pool))
1281 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001282}
1283
Tejun Heoc8efcc22010-12-20 19:32:04 +01001284/*
1285 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001286 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001287 */
1288static bool is_chained_work(struct workqueue_struct *wq)
1289{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001290 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001291
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001292 worker = current_wq_worker();
1293 /*
1294 * Return %true iff I'm a worker execuing a work item on @wq. If
1295 * I'm @worker, it's safe to dereference it without locking.
1296 */
Tejun Heo112202d2013-02-13 19:29:12 -08001297 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001298}
1299
Tejun Heod84ff052013-03-12 11:29:59 -07001300static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct work_struct *work)
1302{
Tejun Heo112202d2013-02-13 19:29:12 -08001303 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001304 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001305 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001306 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001307 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001308
1309 /*
1310 * While a work item is PENDING && off queue, a task trying to
1311 * steal the PENDING will busy-loop waiting for it to either get
1312 * queued or lose PENDING. Grabbing PENDING and queueing should
1313 * happen with IRQ disabled.
1314 */
1315 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001317 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001318
Tejun Heoc8efcc22010-12-20 19:32:04 +01001319 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001320 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001321 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001322 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001323retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001324 if (req_cpu == WORK_CPU_UNBOUND)
1325 cpu = raw_smp_processor_id();
1326
Tejun Heoc9178082013-03-12 11:30:04 -07001327 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001328 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001329 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001330 else
1331 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001332
Tejun Heoc9178082013-03-12 11:30:04 -07001333 /*
1334 * If @work was previously on a different pool, it might still be
1335 * running there, in which case the work needs to be queued on that
1336 * pool to guarantee non-reentrancy.
1337 */
1338 last_pool = get_work_pool(work);
1339 if (last_pool && last_pool != pwq->pool) {
1340 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001341
Tejun Heoc9178082013-03-12 11:30:04 -07001342 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001343
Tejun Heoc9178082013-03-12 11:30:04 -07001344 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001345
Tejun Heoc9178082013-03-12 11:30:04 -07001346 if (worker && worker->current_pwq->wq == wq) {
1347 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001348 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001349 /* meh... not running there, queue here */
1350 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001351 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001352 }
Tejun Heof3421792010-07-02 10:03:51 +02001353 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001354 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001355 }
1356
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001357 /*
1358 * pwq is determined and locked. For unbound pools, we could have
1359 * raced with pwq release and it could already be dead. If its
1360 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001361 * without another pwq replacing it in the numa_pwq_tbl or while
1362 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001363 * make forward-progress.
1364 */
1365 if (unlikely(!pwq->refcnt)) {
1366 if (wq->flags & WQ_UNBOUND) {
1367 spin_unlock(&pwq->pool->lock);
1368 cpu_relax();
1369 goto retry;
1370 }
1371 /* oops */
1372 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1373 wq->name, cpu);
1374 }
1375
Tejun Heo112202d2013-02-13 19:29:12 -08001376 /* pwq determined, queue */
1377 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001378
Dan Carpenterf5b25522012-04-13 22:06:58 +03001379 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001380 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001381 return;
1382 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001383
Tejun Heo112202d2013-02-13 19:29:12 -08001384 pwq->nr_in_flight[pwq->work_color]++;
1385 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001386
Tejun Heo112202d2013-02-13 19:29:12 -08001387 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001388 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001389 pwq->nr_active++;
1390 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001391 } else {
1392 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001393 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001394 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001395
Tejun Heo112202d2013-02-13 19:29:12 -08001396 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001397
Tejun Heo112202d2013-02-13 19:29:12 -08001398 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001401/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001402 * queue_work_on - queue work on specific cpu
1403 * @cpu: CPU number to execute work on
1404 * @wq: workqueue to use
1405 * @work: work to queue
1406 *
Tejun Heod4283e92012-08-03 10:30:44 -07001407 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001408 *
1409 * We queue the work to a specific CPU, the caller must ensure it
1410 * can't go away.
1411 */
Tejun Heod4283e92012-08-03 10:30:44 -07001412bool queue_work_on(int cpu, struct workqueue_struct *wq,
1413 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001414{
Tejun Heod4283e92012-08-03 10:30:44 -07001415 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001416 unsigned long flags;
1417
1418 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001419
Tejun Heo22df02b2010-06-29 10:07:10 +02001420 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001421 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001422 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001423 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001424
1425 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001426 return ret;
1427}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001428EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001429
Tejun Heod8e794d2012-08-03 10:30:45 -07001430void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
David Howells52bad642006-11-22 14:54:01 +00001432 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001434 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001435 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001437EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001439static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1440 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001442 struct timer_list *timer = &dwork->timer;
1443 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001445 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1446 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001447 WARN_ON_ONCE(timer_pending(timer));
1448 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001449
Tejun Heo8852aac2012-12-01 16:23:42 -08001450 /*
1451 * If @delay is 0, queue @dwork->work immediately. This is for
1452 * both optimization and correctness. The earliest @timer can
1453 * expire is on the closest next tick and delayed_work users depend
1454 * on that there's no such delay when @delay is 0.
1455 */
1456 if (!delay) {
1457 __queue_work(cpu, wq, &dwork->work);
1458 return;
1459 }
1460
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001461 timer_stats_timer_set_start_info(&dwork->timer);
1462
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001463 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001464 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001465 timer->expires = jiffies + delay;
1466
1467 if (unlikely(cpu != WORK_CPU_UNBOUND))
1468 add_timer_on(timer, cpu);
1469 else
1470 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001473/**
1474 * queue_delayed_work_on - queue work on specific CPU after delay
1475 * @cpu: CPU number to execute work on
1476 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001477 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001478 * @delay: number of jiffies to wait before queueing
1479 *
Tejun Heo715f1302012-08-03 10:30:46 -07001480 * Returns %false if @work was already on a queue, %true otherwise. If
1481 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1482 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001483 */
Tejun Heod4283e92012-08-03 10:30:44 -07001484bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1485 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001486{
David Howells52bad642006-11-22 14:54:01 +00001487 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001488 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001489 unsigned long flags;
1490
1491 /* read the comment in __queue_work() */
1492 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001493
Tejun Heo22df02b2010-06-29 10:07:10 +02001494 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001495 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001496 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001497 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001498
1499 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001500 return ret;
1501}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001502EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tejun Heoc8e55f32010-06-29 10:07:12 +02001504/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001505 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1506 * @cpu: CPU number to execute work on
1507 * @wq: workqueue to use
1508 * @dwork: work to queue
1509 * @delay: number of jiffies to wait before queueing
1510 *
1511 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1512 * modify @dwork's timer so that it expires after @delay. If @delay is
1513 * zero, @work is guaranteed to be scheduled immediately regardless of its
1514 * current state.
1515 *
1516 * Returns %false if @dwork was idle and queued, %true if @dwork was
1517 * pending and its timer was modified.
1518 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001519 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001520 * See try_to_grab_pending() for details.
1521 */
1522bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1523 struct delayed_work *dwork, unsigned long delay)
1524{
1525 unsigned long flags;
1526 int ret;
1527
1528 do {
1529 ret = try_to_grab_pending(&dwork->work, true, &flags);
1530 } while (unlikely(ret == -EAGAIN));
1531
1532 if (likely(ret >= 0)) {
1533 __queue_delayed_work(cpu, wq, dwork, delay);
1534 local_irq_restore(flags);
1535 }
1536
1537 /* -ENOENT from try_to_grab_pending() becomes %true */
1538 return ret;
1539}
1540EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1541
1542/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001543 * worker_enter_idle - enter idle state
1544 * @worker: worker which is entering idle state
1545 *
1546 * @worker is entering idle state. Update stats and idle timer if
1547 * necessary.
1548 *
1549 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001550 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001551 */
1552static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001554 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Tejun Heo6183c002013-03-12 11:29:57 -07001556 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1557 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1558 (worker->hentry.next || worker->hentry.pprev)))
1559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Tejun Heocb444762010-07-02 10:03:50 +02001561 /* can't use worker_set_flags(), also called from start_worker() */
1562 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001563 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001565
Tejun Heoc8e55f32010-06-29 10:07:12 +02001566 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001567 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001568
Tejun Heo628c78e2012-07-17 12:39:27 -07001569 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1570 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001571
Tejun Heo544ecf32012-05-14 15:04:50 -07001572 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001573 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001574 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001575 * nr_running, the warning may trigger spuriously. Check iff
1576 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001577 */
Tejun Heo24647572013-01-24 11:01:33 -08001578 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001579 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001580 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Tejun Heoc8e55f32010-06-29 10:07:12 +02001583/**
1584 * worker_leave_idle - leave idle state
1585 * @worker: worker which is leaving idle state
1586 *
1587 * @worker is leaving idle state. Update stats.
1588 *
1589 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001590 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001591 */
1592static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001594 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Tejun Heo6183c002013-03-12 11:29:57 -07001596 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1597 return;
Tejun Heod302f012010-06-29 10:07:13 +02001598 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001599 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001600 list_del_init(&worker->entry);
1601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Tejun Heoe22bee72010-06-29 10:07:14 +02001603/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001604 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1605 * @pool: target worker_pool
1606 *
1607 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 *
1609 * Works which are scheduled while the cpu is online must at least be
1610 * scheduled to a worker which is bound to the cpu so that if they are
1611 * flushed from cpu callbacks while cpu is going down, they are
1612 * guaranteed to execute on the cpu.
1613 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001614 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001615 * themselves to the target cpu and may race with cpu going down or
1616 * coming online. kthread_bind() can't be used because it may put the
1617 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001618 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 * [dis]associated in the meantime.
1620 *
Tejun Heo706026c2013-01-24 11:01:34 -08001621 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001622 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001623 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1624 * enters idle state or fetches works without dropping lock, it can
1625 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001626 *
1627 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001628 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001629 * held.
1630 *
1631 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001632 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 * bound), %false if offline.
1634 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001635static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001636__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001637{
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 while (true) {
1639 /*
1640 * The following call may fail, succeed or succeed
1641 * without actually migrating the task to the cpu if
1642 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001643 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 */
Tejun Heo24647572013-01-24 11:01:33 -08001645 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001646 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001647
Tejun Heod565ed62013-01-24 11:01:33 -08001648 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001649 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001650 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001651 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001652 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001653 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001654 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001655
Tejun Heo5035b202011-04-29 18:08:37 +02001656 /*
1657 * We've raced with CPU hot[un]plug. Give it a breather
1658 * and retry migration. cond_resched() is required here;
1659 * otherwise, we might deadlock against cpu_stop trying to
1660 * bring down the CPU on non-preemptive kernel.
1661 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001662 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001663 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001664 }
1665}
1666
Tejun Heoc34056a2010-06-29 10:07:11 +02001667static struct worker *alloc_worker(void)
1668{
1669 struct worker *worker;
1670
1671 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001672 if (worker) {
1673 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001674 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001675 /* on creation a worker is in !idle && prep state */
1676 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001677 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001678 return worker;
1679}
1680
1681/**
1682 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001683 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001684 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001685 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001686 * can be started by calling start_worker() or destroyed using
1687 * destroy_worker().
1688 *
1689 * CONTEXT:
1690 * Might sleep. Does GFP_KERNEL allocations.
1691 *
1692 * RETURNS:
1693 * Pointer to the newly created worker.
1694 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001695static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001696{
Tejun Heoc34056a2010-06-29 10:07:11 +02001697 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001698 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001699 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001700
Tejun Heocd549682013-03-13 19:47:39 -07001701 lockdep_assert_held(&pool->manager_mutex);
1702
Tejun Heo822d8402013-03-19 13:45:21 -07001703 /*
1704 * ID is needed to determine kthread name. Allocate ID first
1705 * without installing the pointer.
1706 */
1707 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001708 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001709
1710 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1711
Tejun Heod565ed62013-01-24 11:01:33 -08001712 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001713 idr_preload_end();
1714 if (id < 0)
1715 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001716
1717 worker = alloc_worker();
1718 if (!worker)
1719 goto fail;
1720
Tejun Heobd7bdd42012-07-12 14:46:37 -07001721 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001722 worker->id = id;
1723
Tejun Heo29c91e92013-03-12 11:30:03 -07001724 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001725 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1726 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001727 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001728 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1729
Tejun Heof3f90ad2013-04-01 11:23:34 -07001730 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001731 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001732 if (IS_ERR(worker->task))
1733 goto fail;
1734
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001735 /*
1736 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1737 * online CPUs. It'll be re-applied when any of the CPUs come up.
1738 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001739 set_user_nice(worker->task, pool->attrs->nice);
1740 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001741
Tejun Heo14a40ff2013-03-19 13:45:20 -07001742 /* prevent userland from meddling with cpumask of workqueue workers */
1743 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001744
1745 /*
1746 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1747 * remains stable across this function. See the comments above the
1748 * flag definition for details.
1749 */
1750 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001751 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001752
Tejun Heo822d8402013-03-19 13:45:21 -07001753 /* successful, commit the pointer to idr */
1754 spin_lock_irq(&pool->lock);
1755 idr_replace(&pool->worker_idr, worker, worker->id);
1756 spin_unlock_irq(&pool->lock);
1757
Tejun Heoc34056a2010-06-29 10:07:11 +02001758 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001759
Tejun Heoc34056a2010-06-29 10:07:11 +02001760fail:
1761 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001762 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001763 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001764 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 }
1766 kfree(worker);
1767 return NULL;
1768}
1769
1770/**
1771 * start_worker - start a newly created worker
1772 * @worker: worker to start
1773 *
Tejun Heo706026c2013-01-24 11:01:34 -08001774 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001775 *
1776 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001777 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001778 */
1779static void start_worker(struct worker *worker)
1780{
Tejun Heocb444762010-07-02 10:03:50 +02001781 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001782 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001784 wake_up_process(worker->task);
1785}
1786
1787/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001788 * create_and_start_worker - create and start a worker for a pool
1789 * @pool: the target pool
1790 *
Tejun Heocd549682013-03-13 19:47:39 -07001791 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001792 */
1793static int create_and_start_worker(struct worker_pool *pool)
1794{
1795 struct worker *worker;
1796
Tejun Heocd549682013-03-13 19:47:39 -07001797 mutex_lock(&pool->manager_mutex);
1798
Tejun Heoebf44d12013-03-13 19:47:39 -07001799 worker = create_worker(pool);
1800 if (worker) {
1801 spin_lock_irq(&pool->lock);
1802 start_worker(worker);
1803 spin_unlock_irq(&pool->lock);
1804 }
1805
Tejun Heocd549682013-03-13 19:47:39 -07001806 mutex_unlock(&pool->manager_mutex);
1807
Tejun Heoebf44d12013-03-13 19:47:39 -07001808 return worker ? 0 : -ENOMEM;
1809}
1810
1811/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 * destroy_worker - destroy a workqueue worker
1813 * @worker: worker to be destroyed
1814 *
Tejun Heo706026c2013-01-24 11:01:34 -08001815 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001816 *
1817 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001818 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001819 */
1820static void destroy_worker(struct worker *worker)
1821{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001822 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001823
Tejun Heocd549682013-03-13 19:47:39 -07001824 lockdep_assert_held(&pool->manager_mutex);
1825 lockdep_assert_held(&pool->lock);
1826
Tejun Heoc34056a2010-06-29 10:07:11 +02001827 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001828 if (WARN_ON(worker->current_work) ||
1829 WARN_ON(!list_empty(&worker->scheduled)))
1830 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001831
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001833 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001834 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001835 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836
1837 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001838 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839
Tejun Heo822d8402013-03-19 13:45:21 -07001840 idr_remove(&pool->worker_idr, worker->id);
1841
Tejun Heod565ed62013-01-24 11:01:33 -08001842 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001843
Tejun Heoc34056a2010-06-29 10:07:11 +02001844 kthread_stop(worker->task);
1845 kfree(worker);
1846
Tejun Heod565ed62013-01-24 11:01:33 -08001847 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001848}
1849
Tejun Heo63d95a92012-07-12 14:46:37 -07001850static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001851{
Tejun Heo63d95a92012-07-12 14:46:37 -07001852 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001853
Tejun Heod565ed62013-01-24 11:01:33 -08001854 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001855
Tejun Heo63d95a92012-07-12 14:46:37 -07001856 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001857 struct worker *worker;
1858 unsigned long expires;
1859
1860 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001861 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001862 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1863
1864 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001865 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001866 else {
1867 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001868 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001869 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001870 }
1871 }
1872
Tejun Heod565ed62013-01-24 11:01:33 -08001873 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001874}
1875
Tejun Heo493a1722013-03-12 11:29:59 -07001876static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001877{
Tejun Heo112202d2013-02-13 19:29:12 -08001878 struct pool_workqueue *pwq = get_work_pwq(work);
1879 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001880
Tejun Heo2e109a22013-03-13 19:47:40 -07001881 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001882
Tejun Heo493008a2013-03-12 11:30:03 -07001883 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001884 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001885
1886 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001887 if (list_empty(&pwq->mayday_node)) {
1888 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001889 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001890 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001891}
1892
Tejun Heo706026c2013-01-24 11:01:34 -08001893static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001894{
Tejun Heo63d95a92012-07-12 14:46:37 -07001895 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001896 struct work_struct *work;
1897
Tejun Heo2e109a22013-03-13 19:47:40 -07001898 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001899 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001900
Tejun Heo63d95a92012-07-12 14:46:37 -07001901 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 /*
1903 * We've been trying to create a new worker but
1904 * haven't been successful. We might be hitting an
1905 * allocation deadlock. Send distress signals to
1906 * rescuers.
1907 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 send_mayday(work);
1910 }
1911
Tejun Heo493a1722013-03-12 11:29:59 -07001912 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001913 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001914
Tejun Heo63d95a92012-07-12 14:46:37 -07001915 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001916}
1917
1918/**
1919 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 * have at least one idle worker on return from this function. If
1924 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001925 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001926 * possible allocation deadlock.
1927 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001928 * On return, need_to_create_worker() is guaranteed to be %false and
1929 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 *
1931 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001932 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001933 * multiple times. Does GFP_KERNEL allocations. Called only from
1934 * manager.
1935 *
1936 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001937 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 * otherwise.
1939 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001940static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001941__releases(&pool->lock)
1942__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001943{
Tejun Heo63d95a92012-07-12 14:46:37 -07001944 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001945 return false;
1946restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001947 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001948
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001951
1952 while (true) {
1953 struct worker *worker;
1954
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001955 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001956 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001957 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001958 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001959 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001960 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1961 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001962 return true;
1963 }
1964
Tejun Heo63d95a92012-07-12 14:46:37 -07001965 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 break;
1967
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 __set_current_state(TASK_INTERRUPTIBLE);
1969 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001970
Tejun Heo63d95a92012-07-12 14:46:37 -07001971 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 break;
1973 }
1974
Tejun Heo63d95a92012-07-12 14:46:37 -07001975 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001976 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001977 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 goto restart;
1979 return true;
1980}
1981
1982/**
1983 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001984 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001986 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 * IDLE_WORKER_TIMEOUT.
1988 *
1989 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001990 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 * multiple times. Called only from manager.
1992 *
1993 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001994 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 * otherwise.
1996 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001997static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001998{
1999 bool ret = false;
2000
Tejun Heo63d95a92012-07-12 14:46:37 -07002001 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 struct worker *worker;
2003 unsigned long expires;
2004
Tejun Heo63d95a92012-07-12 14:46:37 -07002005 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002006 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2007
2008 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002009 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 break;
2011 }
2012
2013 destroy_worker(worker);
2014 ret = true;
2015 }
2016
2017 return ret;
2018}
2019
2020/**
2021 * manage_workers - manage worker pool
2022 * @worker: self
2023 *
Tejun Heo706026c2013-01-24 11:01:34 -08002024 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002026 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 *
2028 * The caller can safely start processing works on false return. On
2029 * true return, it's guaranteed that need_to_create_worker() is false
2030 * and may_start_working() is true.
2031 *
2032 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002033 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002034 * multiple times. Does GFP_KERNEL allocations.
2035 *
2036 * RETURNS:
Libin2d498db2013-08-21 08:50:40 +08002037 * %false if the pool don't need management and the caller can safely start
2038 * processing works, %true indicates that the function released pool->lock
2039 * and reacquired it to perform some management function and that the
2040 * conditions that the caller verified while holding the lock before
2041 * calling the function might no longer be true.
Tejun Heoe22bee72010-06-29 10:07:14 +02002042 */
2043static bool manage_workers(struct worker *worker)
2044{
Tejun Heo63d95a92012-07-12 14:46:37 -07002045 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002046 bool ret = false;
2047
Tejun Heobc3a1af2013-03-13 19:47:39 -07002048 /*
2049 * Managership is governed by two mutexes - manager_arb and
2050 * manager_mutex. manager_arb handles arbitration of manager role.
2051 * Anyone who successfully grabs manager_arb wins the arbitration
2052 * and becomes the manager. mutex_trylock() on pool->manager_arb
2053 * failure while holding pool->lock reliably indicates that someone
2054 * else is managing the pool and the worker which failed trylock
2055 * can proceed to executing work items. This means that anyone
2056 * grabbing manager_arb is responsible for actually performing
2057 * manager duties. If manager_arb is grabbed and released without
2058 * actual management, the pool may stall indefinitely.
2059 *
2060 * manager_mutex is used for exclusion of actual management
2061 * operations. The holder of manager_mutex can be sure that none
2062 * of management operations, including creation and destruction of
2063 * workers, won't take place until the mutex is released. Because
2064 * manager_mutex doesn't interfere with manager role arbitration,
2065 * it is guaranteed that the pool's management, while may be
2066 * delayed, won't be disturbed by someone else grabbing
2067 * manager_mutex.
2068 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002069 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002070 return ret;
2071
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002072 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002073 * With manager arbitration won, manager_mutex would be free in
2074 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002075 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002076 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002077 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002078 mutex_lock(&pool->manager_mutex);
Joonsoo Kim8f174b12013-05-01 00:07:00 +09002079 spin_lock_irq(&pool->lock);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002080 ret = true;
2081 }
2082
Tejun Heo11ebea52012-07-12 14:46:37 -07002083 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002084
2085 /*
2086 * Destroy and then create so that may_start_working() is true
2087 * on return.
2088 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002089 ret |= maybe_destroy_workers(pool);
2090 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002091
Tejun Heobc3a1af2013-03-13 19:47:39 -07002092 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002093 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002094 return ret;
2095}
2096
Tejun Heoa62428c2010-06-29 10:07:10 +02002097/**
2098 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002099 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002100 * @work: work to process
2101 *
2102 * Process @work. This function contains all the logics necessary to
2103 * process a single work including synchronization against and
2104 * interaction with other workers on the same cpu, queueing and
2105 * flushing. As long as context requirement is met, any worker can
2106 * call this function to process a work.
2107 *
2108 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002109 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002110 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002111static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002112__releases(&pool->lock)
2113__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002114{
Tejun Heo112202d2013-02-13 19:29:12 -08002115 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002116 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002117 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002118 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002119 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002120#ifdef CONFIG_LOCKDEP
2121 /*
2122 * It is permissible to free the struct work_struct from
2123 * inside the function that is called from it, this we need to
2124 * take into account for lockdep too. To avoid bogus "held
2125 * lock freed" warnings as well as problems when looking into
2126 * work->lockdep_map, make a copy and use that here.
2127 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002128 struct lockdep_map lockdep_map;
2129
2130 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002131#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002132 /*
2133 * Ensure we're on the correct CPU. DISASSOCIATED test is
2134 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002135 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002136 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002137 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002138 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002139 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002140
Tejun Heo7e116292010-06-29 10:07:13 +02002141 /*
2142 * A single work shouldn't be executed concurrently by
2143 * multiple workers on a single cpu. Check whether anyone is
2144 * already processing the work. If so, defer the work to the
2145 * currently executing one.
2146 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002147 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002148 if (unlikely(collision)) {
2149 move_linked_works(work, &collision->scheduled, NULL);
2150 return;
2151 }
2152
Tejun Heo8930cab2012-08-03 10:30:45 -07002153 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002154 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002155 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002156 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002157 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002158 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002159 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002160
Tejun Heoa62428c2010-06-29 10:07:10 +02002161 list_del_init(&work->entry);
2162
Tejun Heo649027d2010-06-29 10:07:14 +02002163 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002164 * CPU intensive works don't participate in concurrency
2165 * management. They're the scheduler's responsibility.
2166 */
2167 if (unlikely(cpu_intensive))
2168 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2169
Tejun Heo974271c2012-07-12 14:46:37 -07002170 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002171 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002172 * executed ASAP. Wake up another worker if necessary.
2173 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002174 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2175 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002176
Tejun Heo8930cab2012-08-03 10:30:45 -07002177 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002178 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002179 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002180 * PENDING and queued state changes happen together while IRQ is
2181 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002182 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002183 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002184
Tejun Heod565ed62013-01-24 11:01:33 -08002185 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002186
Tejun Heo112202d2013-02-13 19:29:12 -08002187 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002188 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002189 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002190 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002191 /*
2192 * While we must be careful to not use "work" after this, the trace
2193 * point will only record its address.
2194 */
2195 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002196 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002197 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002198
2199 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002200 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2201 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002202 current->comm, preempt_count(), task_pid_nr(current),
2203 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002204 debug_show_held_locks(current);
2205 dump_stack();
2206 }
2207
Tejun Heod565ed62013-01-24 11:01:33 -08002208 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002209
Tejun Heofb0e7be2010-06-29 10:07:15 +02002210 /* clear cpu intensive status */
2211 if (unlikely(cpu_intensive))
2212 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2213
Tejun Heoa62428c2010-06-29 10:07:10 +02002214 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002215 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002216 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002217 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002218 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002219 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002220 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002221}
2222
Tejun Heoaffee4b2010-06-29 10:07:12 +02002223/**
2224 * process_scheduled_works - process scheduled works
2225 * @worker: self
2226 *
2227 * Process all scheduled works. Please note that the scheduled list
2228 * may change while processing a work, so this function repeatedly
2229 * fetches a work from the top and executes it.
2230 *
2231 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002232 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002233 * multiple times.
2234 */
2235static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002237 while (!list_empty(&worker->scheduled)) {
2238 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002240 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Tejun Heo4690c4a2010-06-29 10:07:10 +02002244/**
2245 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002246 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002247 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002248 * The worker thread function. All workers belong to a worker_pool -
2249 * either a per-cpu one or dynamic unbound one. These workers process all
2250 * work items regardless of their specific target workqueue. The only
2251 * exception is work items which belong to workqueues with a rescuer which
2252 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002253 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002254static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Tejun Heoc34056a2010-06-29 10:07:11 +02002256 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002257 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Tejun Heoe22bee72010-06-29 10:07:14 +02002259 /* tell the scheduler that this is a workqueue worker */
2260 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002261woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002262 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Tejun Heoa9ab7752013-03-19 13:45:21 -07002264 /* am I supposed to die? */
2265 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002266 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002267 WARN_ON_ONCE(!list_empty(&worker->entry));
2268 worker->task->flags &= ~PF_WQ_WORKER;
2269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271
Tejun Heoc8e55f32010-06-29 10:07:12 +02002272 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002273recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002274 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002275 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002276 goto sleep;
2277
2278 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002279 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002280 goto recheck;
2281
Tejun Heoc8e55f32010-06-29 10:07:12 +02002282 /*
2283 * ->scheduled list can only be filled while a worker is
2284 * preparing to process a work or actually processing it.
2285 * Make sure nobody diddled with it while I was sleeping.
2286 */
Tejun Heo6183c002013-03-12 11:29:57 -07002287 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002288
Tejun Heoe22bee72010-06-29 10:07:14 +02002289 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002290 * Finish PREP stage. We're guaranteed to have at least one idle
2291 * worker or that someone else has already assumed the manager
2292 * role. This is where @worker starts participating in concurrency
2293 * management if applicable and concurrency management is restored
2294 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002295 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002296 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002297
2298 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002299 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002300 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002301 struct work_struct, entry);
2302
2303 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2304 /* optimization path, not strictly necessary */
2305 process_one_work(worker, work);
2306 if (unlikely(!list_empty(&worker->scheduled)))
2307 process_scheduled_works(worker);
2308 } else {
2309 move_linked_works(work, &worker->scheduled, NULL);
2310 process_scheduled_works(worker);
2311 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002312 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002313
Tejun Heoe22bee72010-06-29 10:07:14 +02002314 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002315sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002316 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002317 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002318
Tejun Heoc8e55f32010-06-29 10:07:12 +02002319 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002320 * pool->lock is held and there's no work to process and no need to
2321 * manage, sleep. Workers are woken up only while holding
2322 * pool->lock or from local cpu, so setting the current state
2323 * before releasing pool->lock is enough to prevent losing any
2324 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002325 */
2326 worker_enter_idle(worker);
2327 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002328 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002329 schedule();
2330 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Tejun Heoe22bee72010-06-29 10:07:14 +02002333/**
2334 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002335 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 *
2337 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002338 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002339 *
Tejun Heo706026c2013-01-24 11:01:34 -08002340 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 * worker which uses GFP_KERNEL allocation which has slight chance of
2342 * developing into deadlock if some works currently on the same queue
2343 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2344 * the problem rescuer solves.
2345 *
Tejun Heo706026c2013-01-24 11:01:34 -08002346 * When such condition is possible, the pool summons rescuers of all
2347 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002348 * those works so that forward progress can be guaranteed.
2349 *
2350 * This should happen rarely.
2351 */
Tejun Heo111c2252013-01-17 17:16:24 -08002352static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002353{
Tejun Heo111c2252013-01-17 17:16:24 -08002354 struct worker *rescuer = __rescuer;
2355 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002356 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002357
2358 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002359
2360 /*
2361 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2362 * doesn't participate in concurrency management.
2363 */
2364 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002365repeat:
2366 set_current_state(TASK_INTERRUPTIBLE);
2367
Mike Galbraith412d32e2012-11-28 07:17:18 +01002368 if (kthread_should_stop()) {
2369 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002370 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002371 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002372 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002373
Tejun Heo493a1722013-03-12 11:29:59 -07002374 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002375 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002376
2377 while (!list_empty(&wq->maydays)) {
2378 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2379 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002380 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002381 struct work_struct *work, *n;
2382
2383 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002384 list_del_init(&pwq->mayday_node);
2385
Tejun Heo2e109a22013-03-13 19:47:40 -07002386 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002387
2388 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002389 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002390 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002391
2392 /*
2393 * Slurp in all works issued via this workqueue and
2394 * process'em.
2395 */
Tejun Heo6183c002013-03-12 11:29:57 -07002396 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002397 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002398 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002399 move_linked_works(work, scheduled, &n);
2400
2401 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002402
2403 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002404 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002405 * regular worker; otherwise, we end up with 0 concurrency
2406 * and stalling the execution.
2407 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002408 if (keep_working(pool))
2409 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002410
Lai Jiangshanb3104102013-02-19 12:17:02 -08002411 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002412 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002413 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 }
2415
Tejun Heo2e109a22013-03-13 19:47:40 -07002416 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002417
Tejun Heo111c2252013-01-17 17:16:24 -08002418 /* rescuers should never participate in concurrency management */
2419 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002420 schedule();
2421 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002424struct wq_barrier {
2425 struct work_struct work;
2426 struct completion done;
2427};
2428
2429static void wq_barrier_func(struct work_struct *work)
2430{
2431 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2432 complete(&barr->done);
2433}
2434
Tejun Heo4690c4a2010-06-29 10:07:10 +02002435/**
2436 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002437 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002438 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002439 * @target: target work to attach @barr to
2440 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002441 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002442 * @barr is linked to @target such that @barr is completed only after
2443 * @target finishes execution. Please note that the ordering
2444 * guarantee is observed only with respect to @target and on the local
2445 * cpu.
2446 *
2447 * Currently, a queued barrier can't be canceled. This is because
2448 * try_to_grab_pending() can't determine whether the work to be
2449 * grabbed is at the head of the queue and thus can't clear LINKED
2450 * flag of the previous work while there must be a valid next work
2451 * after a work with LINKED flag set.
2452 *
2453 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002454 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002455 *
2456 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002457 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002458 */
Tejun Heo112202d2013-02-13 19:29:12 -08002459static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002460 struct wq_barrier *barr,
2461 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002462{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002463 struct list_head *head;
2464 unsigned int linked = 0;
2465
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002466 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002467 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002468 * as we know for sure that this will not trigger any of the
2469 * checks and call back into the fixup functions where we
2470 * might deadlock.
2471 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002472 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002473 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002474 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002475
Tejun Heoaffee4b2010-06-29 10:07:12 +02002476 /*
2477 * If @target is currently being executed, schedule the
2478 * barrier to the worker; otherwise, put it after @target.
2479 */
2480 if (worker)
2481 head = worker->scheduled.next;
2482 else {
2483 unsigned long *bits = work_data_bits(target);
2484
2485 head = target->entry.next;
2486 /* there can already be other linked works, inherit and set */
2487 linked = *bits & WORK_STRUCT_LINKED;
2488 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2489 }
2490
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002491 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002492 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002493 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002494}
2495
Tejun Heo73f53c42010-06-29 10:07:11 +02002496/**
Tejun Heo112202d2013-02-13 19:29:12 -08002497 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002498 * @wq: workqueue being flushed
2499 * @flush_color: new flush color, < 0 for no-op
2500 * @work_color: new work color, < 0 for no-op
2501 *
Tejun Heo112202d2013-02-13 19:29:12 -08002502 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002503 *
Tejun Heo112202d2013-02-13 19:29:12 -08002504 * If @flush_color is non-negative, flush_color on all pwqs should be
2505 * -1. If no pwq has in-flight commands at the specified color, all
2506 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2507 * has in flight commands, its pwq->flush_color is set to
2508 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002509 * wakeup logic is armed and %true is returned.
2510 *
2511 * The caller should have initialized @wq->first_flusher prior to
2512 * calling this function with non-negative @flush_color. If
2513 * @flush_color is negative, no flush color update is done and %false
2514 * is returned.
2515 *
Tejun Heo112202d2013-02-13 19:29:12 -08002516 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002517 * work_color which is previous to @work_color and all will be
2518 * advanced to @work_color.
2519 *
2520 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002521 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 *
2523 * RETURNS:
2524 * %true if @flush_color >= 0 and there's something to flush. %false
2525 * otherwise.
2526 */
Tejun Heo112202d2013-02-13 19:29:12 -08002527static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002528 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Tejun Heo73f53c42010-06-29 10:07:11 +02002530 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002531 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002532
Tejun Heo73f53c42010-06-29 10:07:11 +02002533 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002534 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002535 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002536 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002537
Tejun Heo49e3cf42013-03-12 11:29:58 -07002538 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002539 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002540
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002541 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002542
2543 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002544 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002545
Tejun Heo112202d2013-02-13 19:29:12 -08002546 if (pwq->nr_in_flight[flush_color]) {
2547 pwq->flush_color = flush_color;
2548 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002549 wait = true;
2550 }
2551 }
2552
2553 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002554 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002555 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 }
2557
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002558 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 }
2560
Tejun Heo112202d2013-02-13 19:29:12 -08002561 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002562 complete(&wq->first_flusher->done);
2563
2564 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002567/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002569 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002571 * This function sleeps until all work items which were queued on entry
2572 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002574void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Tejun Heo73f53c42010-06-29 10:07:11 +02002576 struct wq_flusher this_flusher = {
2577 .list = LIST_HEAD_INIT(this_flusher.list),
2578 .flush_color = -1,
2579 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2580 };
2581 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002582
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002583 lock_map_acquire(&wq->lockdep_map);
2584 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002585
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002586 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002587
2588 /*
2589 * Start-to-wait phase
2590 */
2591 next_color = work_next_color(wq->work_color);
2592
2593 if (next_color != wq->flush_color) {
2594 /*
2595 * Color space is not full. The current work_color
2596 * becomes our flush_color and work_color is advanced
2597 * by one.
2598 */
Tejun Heo6183c002013-03-12 11:29:57 -07002599 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002600 this_flusher.flush_color = wq->work_color;
2601 wq->work_color = next_color;
2602
2603 if (!wq->first_flusher) {
2604 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002605 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606
2607 wq->first_flusher = &this_flusher;
2608
Tejun Heo112202d2013-02-13 19:29:12 -08002609 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002610 wq->work_color)) {
2611 /* nothing to flush, done */
2612 wq->flush_color = next_color;
2613 wq->first_flusher = NULL;
2614 goto out_unlock;
2615 }
2616 } else {
2617 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002618 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002619 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002620 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002621 }
2622 } else {
2623 /*
2624 * Oops, color space is full, wait on overflow queue.
2625 * The next flush completion will assign us
2626 * flush_color and transfer to flusher_queue.
2627 */
2628 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2629 }
2630
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002631 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002632
2633 wait_for_completion(&this_flusher.done);
2634
2635 /*
2636 * Wake-up-and-cascade phase
2637 *
2638 * First flushers are responsible for cascading flushes and
2639 * handling overflow. Non-first flushers can simply return.
2640 */
2641 if (wq->first_flusher != &this_flusher)
2642 return;
2643
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002644 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002645
Tejun Heo4ce48b32010-07-02 10:03:51 +02002646 /* we might have raced, check again with mutex held */
2647 if (wq->first_flusher != &this_flusher)
2648 goto out_unlock;
2649
Tejun Heo73f53c42010-06-29 10:07:11 +02002650 wq->first_flusher = NULL;
2651
Tejun Heo6183c002013-03-12 11:29:57 -07002652 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2653 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002654
2655 while (true) {
2656 struct wq_flusher *next, *tmp;
2657
2658 /* complete all the flushers sharing the current flush color */
2659 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2660 if (next->flush_color != wq->flush_color)
2661 break;
2662 list_del_init(&next->list);
2663 complete(&next->done);
2664 }
2665
Tejun Heo6183c002013-03-12 11:29:57 -07002666 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2667 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002668
2669 /* this flush_color is finished, advance by one */
2670 wq->flush_color = work_next_color(wq->flush_color);
2671
2672 /* one color has been freed, handle overflow queue */
2673 if (!list_empty(&wq->flusher_overflow)) {
2674 /*
2675 * Assign the same color to all overflowed
2676 * flushers, advance work_color and append to
2677 * flusher_queue. This is the start-to-wait
2678 * phase for these overflowed flushers.
2679 */
2680 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2681 tmp->flush_color = wq->work_color;
2682
2683 wq->work_color = work_next_color(wq->work_color);
2684
2685 list_splice_tail_init(&wq->flusher_overflow,
2686 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002687 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002688 }
2689
2690 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002691 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002692 break;
2693 }
2694
2695 /*
2696 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002697 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002698 */
Tejun Heo6183c002013-03-12 11:29:57 -07002699 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2700 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002701
2702 list_del_init(&next->list);
2703 wq->first_flusher = next;
2704
Tejun Heo112202d2013-02-13 19:29:12 -08002705 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002706 break;
2707
2708 /*
2709 * Meh... this color is already done, clear first
2710 * flusher and repeat cascading.
2711 */
2712 wq->first_flusher = NULL;
2713 }
2714
2715out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002716 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
Dave Jonesae90dd52006-06-30 01:40:45 -04002718EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720/**
2721 * drain_workqueue - drain a workqueue
2722 * @wq: workqueue to drain
2723 *
2724 * Wait until the workqueue becomes empty. While draining is in progress,
2725 * only chain queueing is allowed. IOW, only currently pending or running
2726 * work items on @wq can queue further work items on it. @wq is flushed
2727 * repeatedly until it becomes empty. The number of flushing is detemined
2728 * by the depth of chaining and should be relatively short. Whine if it
2729 * takes too long.
2730 */
2731void drain_workqueue(struct workqueue_struct *wq)
2732{
2733 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002734 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002735
2736 /*
2737 * __queue_work() needs to test whether there are drainers, is much
2738 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002739 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002740 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002741 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002742 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002743 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002744 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002745reflush:
2746 flush_workqueue(wq);
2747
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002748 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002749
Tejun Heo49e3cf42013-03-12 11:29:58 -07002750 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002751 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002752
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002753 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002754 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002755 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002756
2757 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002758 continue;
2759
2760 if (++flush_cnt == 10 ||
2761 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002762 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002763 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002764
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002765 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002766 goto reflush;
2767 }
2768
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002769 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002770 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002771 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002772}
2773EXPORT_SYMBOL_GPL(drain_workqueue);
2774
Tejun Heo606a5022012-08-20 14:51:23 -07002775static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002776{
2777 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002778 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002779 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002780
2781 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002782
Tejun Heofa1b54e2013-03-12 11:30:00 -07002783 local_irq_disable();
2784 pool = get_work_pool(work);
2785 if (!pool) {
2786 local_irq_enable();
2787 return false;
2788 }
2789
2790 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002791 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002792 pwq = get_work_pwq(work);
2793 if (pwq) {
2794 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002795 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002796 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002797 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002798 if (!worker)
2799 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002800 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002801 }
Tejun Heobaf59022010-09-16 10:42:16 +02002802
Tejun Heo112202d2013-02-13 19:29:12 -08002803 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002804 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002805
Tejun Heoe1594892011-01-09 23:32:15 +01002806 /*
2807 * If @max_active is 1 or rescuer is in use, flushing another work
2808 * item on the same workqueue may lead to deadlock. Make sure the
2809 * flusher is not running on the same workqueue by verifying write
2810 * access.
2811 */
Tejun Heo493008a2013-03-12 11:30:03 -07002812 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002813 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002814 else
Tejun Heo112202d2013-02-13 19:29:12 -08002815 lock_map_acquire_read(&pwq->wq->lockdep_map);
2816 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002817
Tejun Heobaf59022010-09-16 10:42:16 +02002818 return true;
2819already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002820 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002821 return false;
2822}
2823
Oleg Nesterovdb700892008-07-25 01:47:49 -07002824/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002825 * flush_work - wait for a work to finish executing the last queueing instance
2826 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002827 *
Tejun Heo606a5022012-08-20 14:51:23 -07002828 * Wait until @work has finished execution. @work is guaranteed to be idle
2829 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002830 *
2831 * RETURNS:
2832 * %true if flush_work() waited for the work to finish execution,
2833 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002834 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002835bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002836{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002837 struct wq_barrier barr;
2838
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002839 lock_map_acquire(&work->lockdep_map);
2840 lock_map_release(&work->lockdep_map);
2841
Tejun Heo606a5022012-08-20 14:51:23 -07002842 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002843 wait_for_completion(&barr.done);
2844 destroy_work_on_stack(&barr.work);
2845 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002846 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002847 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002848 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002849}
2850EXPORT_SYMBOL_GPL(flush_work);
2851
Tejun Heo36e227d2012-08-03 10:30:46 -07002852static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002853{
Tejun Heobbb68df2012-08-03 10:30:46 -07002854 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002855 int ret;
2856
2857 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002858 ret = try_to_grab_pending(work, is_dwork, &flags);
2859 /*
2860 * If someone else is canceling, wait for the same event it
2861 * would be waiting for before retrying.
2862 */
2863 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002864 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002865 } while (unlikely(ret < 0));
2866
Tejun Heobbb68df2012-08-03 10:30:46 -07002867 /* tell other tasks trying to grab @work to back off */
2868 mark_work_canceling(work);
2869 local_irq_restore(flags);
2870
Tejun Heo606a5022012-08-20 14:51:23 -07002871 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002872 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002873 return ret;
2874}
2875
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002876/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002877 * cancel_work_sync - cancel a work and wait for it to finish
2878 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002879 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002880 * Cancel @work and wait for its execution to finish. This function
2881 * can be used even if the work re-queues itself or migrates to
2882 * another workqueue. On return from this function, @work is
2883 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002884 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002885 * cancel_work_sync(&delayed_work->work) must not be used for
2886 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002887 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002889 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002890 *
2891 * RETURNS:
2892 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002893 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002894bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002895{
Tejun Heo36e227d2012-08-03 10:30:46 -07002896 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002897}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002898EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002899
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2902 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 * Delayed timer is cancelled and the pending work is queued for
2905 * immediate execution. Like flush_work(), this function only
2906 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002907 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002908 * RETURNS:
2909 * %true if flush_work() waited for the work to finish execution,
2910 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002912bool flush_delayed_work(struct delayed_work *dwork)
2913{
Tejun Heo8930cab2012-08-03 10:30:45 -07002914 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002915 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002916 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002917 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002918 return flush_work(&dwork->work);
2919}
2920EXPORT_SYMBOL(flush_delayed_work);
2921
2922/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002923 * cancel_delayed_work - cancel a delayed work
2924 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002925 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002926 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2927 * and canceled; %false if wasn't pending. Note that the work callback
2928 * function may still be running on return, unless it returns %true and the
2929 * work doesn't re-arm itself. Explicitly flush or use
2930 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002931 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002932 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002933 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002934bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002935{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002936 unsigned long flags;
2937 int ret;
2938
2939 do {
2940 ret = try_to_grab_pending(&dwork->work, true, &flags);
2941 } while (unlikely(ret == -EAGAIN));
2942
2943 if (unlikely(ret < 0))
2944 return false;
2945
Tejun Heo7c3eed52013-01-24 11:01:33 -08002946 set_work_pool_and_clear_pending(&dwork->work,
2947 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002948 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002949 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002950}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002951EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002952
2953/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002954 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2955 * @dwork: the delayed work cancel
2956 *
2957 * This is cancel_work_sync() for delayed works.
2958 *
2959 * RETURNS:
2960 * %true if @dwork was pending, %false otherwise.
2961 */
2962bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002963{
Tejun Heo36e227d2012-08-03 10:30:46 -07002964 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002965}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002966EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002968/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002969 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002970 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002971 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002972 * schedule_on_each_cpu() executes @func on each online CPU using the
2973 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002974 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002975 *
2976 * RETURNS:
2977 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002978 */
David Howells65f27f32006-11-22 14:55:48 +00002979int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002980{
2981 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002982 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002983
Andrew Mortonb6136772006-06-25 05:47:49 -07002984 works = alloc_percpu(struct work_struct);
2985 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002986 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002987
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002988 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002989
Christoph Lameter15316ba2006-01-08 01:00:43 -08002990 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002991 struct work_struct *work = per_cpu_ptr(works, cpu);
2992
2993 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002994 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002995 }
Tejun Heo93981802009-11-17 14:06:20 -08002996
2997 for_each_online_cpu(cpu)
2998 flush_work(per_cpu_ptr(works, cpu));
2999
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003000 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003001 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003002 return 0;
3003}
3004
Alan Sterneef6a7d2010-02-12 17:39:21 +09003005/**
3006 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3007 *
3008 * Forces execution of the kernel-global workqueue and blocks until its
3009 * completion.
3010 *
3011 * Think twice before calling this function! It's very easy to get into
3012 * trouble if you don't take great care. Either of the following situations
3013 * will lead to deadlock:
3014 *
3015 * One of the work items currently on the workqueue needs to acquire
3016 * a lock held by your code or its caller.
3017 *
3018 * Your code is running in the context of a work routine.
3019 *
3020 * They will be detected by lockdep when they occur, but the first might not
3021 * occur very often. It depends on what work items are on the workqueue and
3022 * what locks they need, which you have no control over.
3023 *
3024 * In most situations flushing the entire workqueue is overkill; you merely
3025 * need to know that a particular work item isn't queued and isn't running.
3026 * In such cases you should use cancel_delayed_work_sync() or
3027 * cancel_work_sync() instead.
3028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029void flush_scheduled_work(void)
3030{
Tejun Heod320c032010-06-29 10:07:14 +02003031 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
Dave Jonesae90dd52006-06-30 01:40:45 -04003033EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003036 * execute_in_process_context - reliably execute the routine with user context
3037 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003038 * @ew: guaranteed storage for the execute work structure (must
3039 * be available when the work executes)
3040 *
3041 * Executes the function immediately if process context is available,
3042 * otherwise schedules the function for delayed execution.
3043 *
3044 * Returns: 0 - function was executed
3045 * 1 - function was scheduled for execution
3046 */
David Howells65f27f32006-11-22 14:55:48 +00003047int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003048{
3049 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003050 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003051 return 0;
3052 }
3053
David Howells65f27f32006-11-22 14:55:48 +00003054 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003055 schedule_work(&ew->work);
3056
3057 return 1;
3058}
3059EXPORT_SYMBOL_GPL(execute_in_process_context);
3060
Tejun Heo226223a2013-03-12 11:30:05 -07003061#ifdef CONFIG_SYSFS
3062/*
3063 * Workqueues with WQ_SYSFS flag set is visible to userland via
3064 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3065 * following attributes.
3066 *
3067 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3068 * max_active RW int : maximum number of in-flight work items
3069 *
3070 * Unbound workqueues have the following extra attributes.
3071 *
3072 * id RO int : the associated pool ID
3073 * nice RW int : nice value of the workers
3074 * cpumask RW mask : bitmask of allowed CPUs for the workers
3075 */
3076struct wq_device {
3077 struct workqueue_struct *wq;
3078 struct device dev;
3079};
3080
3081static struct workqueue_struct *dev_to_wq(struct device *dev)
3082{
3083 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3084
3085 return wq_dev->wq;
3086}
3087
3088static ssize_t wq_per_cpu_show(struct device *dev,
3089 struct device_attribute *attr, char *buf)
3090{
3091 struct workqueue_struct *wq = dev_to_wq(dev);
3092
3093 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3094}
3095
3096static ssize_t wq_max_active_show(struct device *dev,
3097 struct device_attribute *attr, char *buf)
3098{
3099 struct workqueue_struct *wq = dev_to_wq(dev);
3100
3101 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3102}
3103
3104static ssize_t wq_max_active_store(struct device *dev,
3105 struct device_attribute *attr,
3106 const char *buf, size_t count)
3107{
3108 struct workqueue_struct *wq = dev_to_wq(dev);
3109 int val;
3110
3111 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3112 return -EINVAL;
3113
3114 workqueue_set_max_active(wq, val);
3115 return count;
3116}
3117
3118static struct device_attribute wq_sysfs_attrs[] = {
3119 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3120 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3121 __ATTR_NULL,
3122};
3123
Tejun Heod55262c2013-04-01 11:23:38 -07003124static ssize_t wq_pool_ids_show(struct device *dev,
3125 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003126{
3127 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003128 const char *delim = "";
3129 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003130
3131 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003132 for_each_node(node) {
3133 written += scnprintf(buf + written, PAGE_SIZE - written,
3134 "%s%d:%d", delim, node,
3135 unbound_pwq_by_node(wq, node)->pool->id);
3136 delim = " ";
3137 }
3138 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003139 rcu_read_unlock_sched();
3140
3141 return written;
3142}
3143
3144static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3145 char *buf)
3146{
3147 struct workqueue_struct *wq = dev_to_wq(dev);
3148 int written;
3149
Tejun Heo6029a912013-04-01 11:23:34 -07003150 mutex_lock(&wq->mutex);
3151 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3152 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003153
3154 return written;
3155}
3156
3157/* prepare workqueue_attrs for sysfs store operations */
3158static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3159{
3160 struct workqueue_attrs *attrs;
3161
3162 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3163 if (!attrs)
3164 return NULL;
3165
Tejun Heo6029a912013-04-01 11:23:34 -07003166 mutex_lock(&wq->mutex);
3167 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3168 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003169 return attrs;
3170}
3171
3172static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3173 const char *buf, size_t count)
3174{
3175 struct workqueue_struct *wq = dev_to_wq(dev);
3176 struct workqueue_attrs *attrs;
3177 int ret;
3178
3179 attrs = wq_sysfs_prep_attrs(wq);
3180 if (!attrs)
3181 return -ENOMEM;
3182
3183 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3184 attrs->nice >= -20 && attrs->nice <= 19)
3185 ret = apply_workqueue_attrs(wq, attrs);
3186 else
3187 ret = -EINVAL;
3188
3189 free_workqueue_attrs(attrs);
3190 return ret ?: count;
3191}
3192
3193static ssize_t wq_cpumask_show(struct device *dev,
3194 struct device_attribute *attr, char *buf)
3195{
3196 struct workqueue_struct *wq = dev_to_wq(dev);
3197 int written;
3198
Tejun Heo6029a912013-04-01 11:23:34 -07003199 mutex_lock(&wq->mutex);
3200 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3201 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003202
3203 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3204 return written;
3205}
3206
3207static ssize_t wq_cpumask_store(struct device *dev,
3208 struct device_attribute *attr,
3209 const char *buf, size_t count)
3210{
3211 struct workqueue_struct *wq = dev_to_wq(dev);
3212 struct workqueue_attrs *attrs;
3213 int ret;
3214
3215 attrs = wq_sysfs_prep_attrs(wq);
3216 if (!attrs)
3217 return -ENOMEM;
3218
3219 ret = cpumask_parse(buf, attrs->cpumask);
3220 if (!ret)
3221 ret = apply_workqueue_attrs(wq, attrs);
3222
3223 free_workqueue_attrs(attrs);
3224 return ret ?: count;
3225}
3226
Tejun Heod55262c2013-04-01 11:23:38 -07003227static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3228 char *buf)
3229{
3230 struct workqueue_struct *wq = dev_to_wq(dev);
3231 int written;
3232
3233 mutex_lock(&wq->mutex);
3234 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3235 !wq->unbound_attrs->no_numa);
3236 mutex_unlock(&wq->mutex);
3237
3238 return written;
3239}
3240
3241static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3242 const char *buf, size_t count)
3243{
3244 struct workqueue_struct *wq = dev_to_wq(dev);
3245 struct workqueue_attrs *attrs;
3246 int v, ret;
3247
3248 attrs = wq_sysfs_prep_attrs(wq);
3249 if (!attrs)
3250 return -ENOMEM;
3251
3252 ret = -EINVAL;
3253 if (sscanf(buf, "%d", &v) == 1) {
3254 attrs->no_numa = !v;
3255 ret = apply_workqueue_attrs(wq, attrs);
3256 }
3257
3258 free_workqueue_attrs(attrs);
3259 return ret ?: count;
3260}
3261
Tejun Heo226223a2013-03-12 11:30:05 -07003262static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003263 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003264 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3265 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003266 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003267 __ATTR_NULL,
3268};
3269
3270static struct bus_type wq_subsys = {
3271 .name = "workqueue",
3272 .dev_attrs = wq_sysfs_attrs,
3273};
3274
3275static int __init wq_sysfs_init(void)
3276{
3277 return subsys_virtual_register(&wq_subsys, NULL);
3278}
3279core_initcall(wq_sysfs_init);
3280
3281static void wq_device_release(struct device *dev)
3282{
3283 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3284
3285 kfree(wq_dev);
3286}
3287
3288/**
3289 * workqueue_sysfs_register - make a workqueue visible in sysfs
3290 * @wq: the workqueue to register
3291 *
3292 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3293 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3294 * which is the preferred method.
3295 *
3296 * Workqueue user should use this function directly iff it wants to apply
3297 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3298 * apply_workqueue_attrs() may race against userland updating the
3299 * attributes.
3300 *
3301 * Returns 0 on success, -errno on failure.
3302 */
3303int workqueue_sysfs_register(struct workqueue_struct *wq)
3304{
3305 struct wq_device *wq_dev;
3306 int ret;
3307
3308 /*
3309 * Adjusting max_active or creating new pwqs by applyting
3310 * attributes breaks ordering guarantee. Disallow exposing ordered
3311 * workqueues.
3312 */
3313 if (WARN_ON(wq->flags & __WQ_ORDERED))
3314 return -EINVAL;
3315
3316 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3317 if (!wq_dev)
3318 return -ENOMEM;
3319
3320 wq_dev->wq = wq;
3321 wq_dev->dev.bus = &wq_subsys;
3322 wq_dev->dev.init_name = wq->name;
3323 wq_dev->dev.release = wq_device_release;
3324
3325 /*
3326 * unbound_attrs are created separately. Suppress uevent until
3327 * everything is ready.
3328 */
3329 dev_set_uevent_suppress(&wq_dev->dev, true);
3330
3331 ret = device_register(&wq_dev->dev);
3332 if (ret) {
3333 kfree(wq_dev);
3334 wq->wq_dev = NULL;
3335 return ret;
3336 }
3337
3338 if (wq->flags & WQ_UNBOUND) {
3339 struct device_attribute *attr;
3340
3341 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3342 ret = device_create_file(&wq_dev->dev, attr);
3343 if (ret) {
3344 device_unregister(&wq_dev->dev);
3345 wq->wq_dev = NULL;
3346 return ret;
3347 }
3348 }
3349 }
3350
3351 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3352 return 0;
3353}
3354
3355/**
3356 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3357 * @wq: the workqueue to unregister
3358 *
3359 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3360 */
3361static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3362{
3363 struct wq_device *wq_dev = wq->wq_dev;
3364
3365 if (!wq->wq_dev)
3366 return;
3367
3368 wq->wq_dev = NULL;
3369 device_unregister(&wq_dev->dev);
3370}
3371#else /* CONFIG_SYSFS */
3372static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3373#endif /* CONFIG_SYSFS */
3374
Tejun Heo7a4e3442013-03-12 11:30:00 -07003375/**
3376 * free_workqueue_attrs - free a workqueue_attrs
3377 * @attrs: workqueue_attrs to free
3378 *
3379 * Undo alloc_workqueue_attrs().
3380 */
3381void free_workqueue_attrs(struct workqueue_attrs *attrs)
3382{
3383 if (attrs) {
3384 free_cpumask_var(attrs->cpumask);
3385 kfree(attrs);
3386 }
3387}
3388
3389/**
3390 * alloc_workqueue_attrs - allocate a workqueue_attrs
3391 * @gfp_mask: allocation mask to use
3392 *
3393 * Allocate a new workqueue_attrs, initialize with default settings and
3394 * return it. Returns NULL on failure.
3395 */
3396struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3397{
3398 struct workqueue_attrs *attrs;
3399
3400 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3401 if (!attrs)
3402 goto fail;
3403 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3404 goto fail;
3405
Tejun Heo13e2e552013-04-01 11:23:31 -07003406 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003407 return attrs;
3408fail:
3409 free_workqueue_attrs(attrs);
3410 return NULL;
3411}
3412
Tejun Heo29c91e92013-03-12 11:30:03 -07003413static void copy_workqueue_attrs(struct workqueue_attrs *to,
3414 const struct workqueue_attrs *from)
3415{
3416 to->nice = from->nice;
3417 cpumask_copy(to->cpumask, from->cpumask);
3418}
3419
Tejun Heo29c91e92013-03-12 11:30:03 -07003420/* hash value of the content of @attr */
3421static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3422{
3423 u32 hash = 0;
3424
3425 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003426 hash = jhash(cpumask_bits(attrs->cpumask),
3427 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003428 return hash;
3429}
3430
3431/* content equality test */
3432static bool wqattrs_equal(const struct workqueue_attrs *a,
3433 const struct workqueue_attrs *b)
3434{
3435 if (a->nice != b->nice)
3436 return false;
3437 if (!cpumask_equal(a->cpumask, b->cpumask))
3438 return false;
3439 return true;
3440}
3441
Tejun Heo7a4e3442013-03-12 11:30:00 -07003442/**
3443 * init_worker_pool - initialize a newly zalloc'd worker_pool
3444 * @pool: worker_pool to initialize
3445 *
3446 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003447 * Returns 0 on success, -errno on failure. Even on failure, all fields
3448 * inside @pool proper are initialized and put_unbound_pool() can be called
3449 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003450 */
3451static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003452{
3453 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003454 pool->id = -1;
3455 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003456 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003457 pool->flags |= POOL_DISASSOCIATED;
3458 INIT_LIST_HEAD(&pool->worklist);
3459 INIT_LIST_HEAD(&pool->idle_list);
3460 hash_init(pool->busy_hash);
3461
3462 init_timer_deferrable(&pool->idle_timer);
3463 pool->idle_timer.function = idle_worker_timeout;
3464 pool->idle_timer.data = (unsigned long)pool;
3465
3466 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3467 (unsigned long)pool);
3468
3469 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003470 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003471 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003472
Tejun Heo29c91e92013-03-12 11:30:03 -07003473 INIT_HLIST_NODE(&pool->hash_node);
3474 pool->refcnt = 1;
3475
3476 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003477 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3478 if (!pool->attrs)
3479 return -ENOMEM;
3480 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003481}
3482
Tejun Heo29c91e92013-03-12 11:30:03 -07003483static void rcu_free_pool(struct rcu_head *rcu)
3484{
3485 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3486
Tejun Heo822d8402013-03-19 13:45:21 -07003487 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 free_workqueue_attrs(pool->attrs);
3489 kfree(pool);
3490}
3491
3492/**
3493 * put_unbound_pool - put a worker_pool
3494 * @pool: worker_pool to put
3495 *
3496 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003497 * safe manner. get_unbound_pool() calls this function on its failure path
3498 * and this function should be able to release pools which went through,
3499 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003500 *
3501 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003502 */
3503static void put_unbound_pool(struct worker_pool *pool)
3504{
3505 struct worker *worker;
3506
Tejun Heoa892cac2013-04-01 11:23:32 -07003507 lockdep_assert_held(&wq_pool_mutex);
3508
3509 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003510 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003511
3512 /* sanity checks */
3513 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003514 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003515 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003516
3517 /* release id and unhash */
3518 if (pool->id >= 0)
3519 idr_remove(&worker_pool_idr, pool->id);
3520 hash_del(&pool->hash_node);
3521
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003522 /*
3523 * Become the manager and destroy all workers. Grabbing
3524 * manager_arb prevents @pool's workers from blocking on
3525 * manager_mutex.
3526 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003527 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003528 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003529 spin_lock_irq(&pool->lock);
3530
3531 while ((worker = first_worker(pool)))
3532 destroy_worker(worker);
3533 WARN_ON(pool->nr_workers || pool->nr_idle);
3534
3535 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003536 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003537 mutex_unlock(&pool->manager_arb);
3538
3539 /* shut down the timers */
3540 del_timer_sync(&pool->idle_timer);
3541 del_timer_sync(&pool->mayday_timer);
3542
3543 /* sched-RCU protected to allow dereferences from get_work_pool() */
3544 call_rcu_sched(&pool->rcu, rcu_free_pool);
3545}
3546
3547/**
3548 * get_unbound_pool - get a worker_pool with the specified attributes
3549 * @attrs: the attributes of the worker_pool to get
3550 *
3551 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3552 * reference count and return it. If there already is a matching
3553 * worker_pool, it will be used; otherwise, this function attempts to
3554 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003555 *
3556 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003557 */
3558static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3559{
Tejun Heo29c91e92013-03-12 11:30:03 -07003560 u32 hash = wqattrs_hash(attrs);
3561 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003562 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003563
Tejun Heoa892cac2013-04-01 11:23:32 -07003564 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003565
3566 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003567 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3568 if (wqattrs_equal(pool->attrs, attrs)) {
3569 pool->refcnt++;
3570 goto out_unlock;
3571 }
3572 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003573
3574 /* nope, create a new one */
3575 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3576 if (!pool || init_worker_pool(pool) < 0)
3577 goto fail;
3578
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003579 if (workqueue_freezing)
3580 pool->flags |= POOL_FREEZING;
3581
Tejun Heo8864b4e2013-03-12 11:30:04 -07003582 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003583 copy_workqueue_attrs(pool->attrs, attrs);
3584
Tejun Heof3f90ad2013-04-01 11:23:34 -07003585 /* if cpumask is contained inside a NUMA node, we belong to that node */
3586 if (wq_numa_enabled) {
3587 for_each_node(node) {
3588 if (cpumask_subset(pool->attrs->cpumask,
3589 wq_numa_possible_cpumask[node])) {
3590 pool->node = node;
3591 break;
3592 }
3593 }
3594 }
3595
Tejun Heo29c91e92013-03-12 11:30:03 -07003596 if (worker_pool_assign_id(pool) < 0)
3597 goto fail;
3598
3599 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003600 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003601 goto fail;
3602
Tejun Heo29c91e92013-03-12 11:30:03 -07003603 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003604 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3605out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003606 return pool;
3607fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003608 if (pool)
3609 put_unbound_pool(pool);
3610 return NULL;
3611}
3612
Tejun Heo8864b4e2013-03-12 11:30:04 -07003613static void rcu_free_pwq(struct rcu_head *rcu)
3614{
3615 kmem_cache_free(pwq_cache,
3616 container_of(rcu, struct pool_workqueue, rcu));
3617}
3618
3619/*
3620 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3621 * and needs to be destroyed.
3622 */
3623static void pwq_unbound_release_workfn(struct work_struct *work)
3624{
3625 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3626 unbound_release_work);
3627 struct workqueue_struct *wq = pwq->wq;
3628 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003629 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003630
3631 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3632 return;
3633
Tejun Heo75ccf592013-03-12 11:30:04 -07003634 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003635 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003636 * necessary on release but do it anyway. It's easier to verify
3637 * and consistent with the linking path.
3638 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003639 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003640 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003641 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003642 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003643
Tejun Heoa892cac2013-04-01 11:23:32 -07003644 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003645 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003646 mutex_unlock(&wq_pool_mutex);
3647
Tejun Heo8864b4e2013-03-12 11:30:04 -07003648 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3649
3650 /*
3651 * If we're the last pwq going away, @wq is already dead and no one
3652 * is gonna access it anymore. Free it.
3653 */
Tejun Heo6029a912013-04-01 11:23:34 -07003654 if (is_last) {
3655 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003656 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003657 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003658}
3659
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003660/**
Tejun Heo699ce092013-03-13 16:51:35 -07003661 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003662 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003663 *
Tejun Heo699ce092013-03-13 16:51:35 -07003664 * If @pwq isn't freezing, set @pwq->max_active to the associated
3665 * workqueue's saved_max_active and activate delayed work items
3666 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003667 */
Tejun Heo699ce092013-03-13 16:51:35 -07003668static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003669{
Tejun Heo699ce092013-03-13 16:51:35 -07003670 struct workqueue_struct *wq = pwq->wq;
3671 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003672
Tejun Heo699ce092013-03-13 16:51:35 -07003673 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003674 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003675
3676 /* fast exit for non-freezable wqs */
3677 if (!freezable && pwq->max_active == wq->saved_max_active)
3678 return;
3679
Lai Jiangshana357fc02013-03-25 16:57:19 -07003680 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003681
3682 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3683 pwq->max_active = wq->saved_max_active;
3684
3685 while (!list_empty(&pwq->delayed_works) &&
3686 pwq->nr_active < pwq->max_active)
3687 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003688
3689 /*
3690 * Need to kick a worker after thawed or an unbound wq's
3691 * max_active is bumped. It's a slow path. Do it always.
3692 */
3693 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003694 } else {
3695 pwq->max_active = 0;
3696 }
3697
Lai Jiangshana357fc02013-03-25 16:57:19 -07003698 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003699}
3700
Tejun Heoe50aba92013-04-01 11:23:35 -07003701/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003702static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3703 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003704{
3705 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3706
Tejun Heoe50aba92013-04-01 11:23:35 -07003707 memset(pwq, 0, sizeof(*pwq));
3708
Tejun Heod2c1d402013-03-12 11:30:04 -07003709 pwq->pool = pool;
3710 pwq->wq = wq;
3711 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003712 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003713 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003714 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003715 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003716 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003717}
Tejun Heod2c1d402013-03-12 11:30:04 -07003718
Tejun Heof147f292013-04-01 11:23:35 -07003719/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003720static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003721{
3722 struct workqueue_struct *wq = pwq->wq;
3723
3724 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003725
Tejun Heo1befcf32013-04-01 11:23:35 -07003726 /* may be called multiple times, ignore if already linked */
3727 if (!list_empty(&pwq->pwqs_node))
3728 return;
3729
Tejun Heo983ca252013-03-13 16:51:35 -07003730 /*
3731 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003732 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003733 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003734 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003735
3736 /* sync max_active to the current setting */
3737 pwq_adjust_max_active(pwq);
3738
3739 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003740 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003741}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003742
Tejun Heof147f292013-04-01 11:23:35 -07003743/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3744static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3745 const struct workqueue_attrs *attrs)
3746{
3747 struct worker_pool *pool;
3748 struct pool_workqueue *pwq;
3749
3750 lockdep_assert_held(&wq_pool_mutex);
3751
3752 pool = get_unbound_pool(attrs);
3753 if (!pool)
3754 return NULL;
3755
Tejun Heoe50aba92013-04-01 11:23:35 -07003756 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003757 if (!pwq) {
3758 put_unbound_pool(pool);
3759 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003760 }
Tejun Heo6029a912013-04-01 11:23:34 -07003761
Tejun Heof147f292013-04-01 11:23:35 -07003762 init_pwq(pwq, wq, pool);
3763 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003764}
3765
Tejun Heo4c16bd32013-04-01 11:23:36 -07003766/* undo alloc_unbound_pwq(), used only in the error path */
3767static void free_unbound_pwq(struct pool_workqueue *pwq)
3768{
3769 lockdep_assert_held(&wq_pool_mutex);
3770
3771 if (pwq) {
3772 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003773 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003774 }
3775}
3776
3777/**
3778 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3779 * @attrs: the wq_attrs of interest
3780 * @node: the target NUMA node
3781 * @cpu_going_down: if >= 0, the CPU to consider as offline
3782 * @cpumask: outarg, the resulting cpumask
3783 *
3784 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3785 * @cpu_going_down is >= 0, that cpu is considered offline during
3786 * calculation. The result is stored in @cpumask. This function returns
3787 * %true if the resulting @cpumask is different from @attrs->cpumask,
3788 * %false if equal.
3789 *
3790 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3791 * enabled and @node has online CPUs requested by @attrs, the returned
3792 * cpumask is the intersection of the possible CPUs of @node and
3793 * @attrs->cpumask.
3794 *
3795 * The caller is responsible for ensuring that the cpumask of @node stays
3796 * stable.
3797 */
3798static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3799 int cpu_going_down, cpumask_t *cpumask)
3800{
Tejun Heod55262c2013-04-01 11:23:38 -07003801 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003802 goto use_dfl;
3803
3804 /* does @node have any online CPUs @attrs wants? */
3805 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3806 if (cpu_going_down >= 0)
3807 cpumask_clear_cpu(cpu_going_down, cpumask);
3808
3809 if (cpumask_empty(cpumask))
3810 goto use_dfl;
3811
3812 /* yeap, return possible CPUs in @node that @attrs wants */
3813 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3814 return !cpumask_equal(cpumask, attrs->cpumask);
3815
3816use_dfl:
3817 cpumask_copy(cpumask, attrs->cpumask);
3818 return false;
3819}
3820
Tejun Heo1befcf32013-04-01 11:23:35 -07003821/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3822static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3823 int node,
3824 struct pool_workqueue *pwq)
3825{
3826 struct pool_workqueue *old_pwq;
3827
3828 lockdep_assert_held(&wq->mutex);
3829
3830 /* link_pwq() can handle duplicate calls */
3831 link_pwq(pwq);
3832
3833 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3834 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3835 return old_pwq;
3836}
3837
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003838/**
3839 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3840 * @wq: the target workqueue
3841 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3842 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003843 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3844 * machines, this function maps a separate pwq to each NUMA node with
3845 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3846 * NUMA node it was issued on. Older pwqs are released as in-flight work
3847 * items finish. Note that a work item which repeatedly requeues itself
3848 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003849 *
3850 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3851 * failure.
3852 */
3853int apply_workqueue_attrs(struct workqueue_struct *wq,
3854 const struct workqueue_attrs *attrs)
3855{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003856 struct workqueue_attrs *new_attrs, *tmp_attrs;
3857 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003858 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003859
Tejun Heo8719dce2013-03-12 11:30:04 -07003860 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003861 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3862 return -EINVAL;
3863
Tejun Heo8719dce2013-03-12 11:30:04 -07003864 /* creating multiple pwqs breaks ordering guarantee */
3865 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3866 return -EINVAL;
3867
Tejun Heo4c16bd32013-04-01 11:23:36 -07003868 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003869 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003870 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3871 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003872 goto enomem;
3873
Tejun Heo4c16bd32013-04-01 11:23:36 -07003874 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003875 copy_workqueue_attrs(new_attrs, attrs);
3876 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3877
Tejun Heo4c16bd32013-04-01 11:23:36 -07003878 /*
3879 * We may create multiple pwqs with differing cpumasks. Make a
3880 * copy of @new_attrs which will be modified and used to obtain
3881 * pools.
3882 */
3883 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003884
Tejun Heo4c16bd32013-04-01 11:23:36 -07003885 /*
3886 * CPUs should stay stable across pwq creations and installations.
3887 * Pin CPUs, determine the target cpumask for each node and create
3888 * pwqs accordingly.
3889 */
3890 get_online_cpus();
3891
3892 mutex_lock(&wq_pool_mutex);
3893
3894 /*
3895 * If something goes wrong during CPU up/down, we'll fall back to
3896 * the default pwq covering whole @attrs->cpumask. Always create
3897 * it even if we don't use it immediately.
3898 */
3899 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3900 if (!dfl_pwq)
3901 goto enomem_pwq;
3902
3903 for_each_node(node) {
3904 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3905 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3906 if (!pwq_tbl[node])
3907 goto enomem_pwq;
3908 } else {
3909 dfl_pwq->refcnt++;
3910 pwq_tbl[node] = dfl_pwq;
3911 }
3912 }
3913
3914 mutex_unlock(&wq_pool_mutex);
3915
3916 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003917 mutex_lock(&wq->mutex);
3918
Tejun Heof147f292013-04-01 11:23:35 -07003919 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003920
3921 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003922 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003923 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3924
3925 /* @dfl_pwq might not have been used, ensure it's linked */
3926 link_pwq(dfl_pwq);
3927 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003928
3929 mutex_unlock(&wq->mutex);
3930
Tejun Heo4c16bd32013-04-01 11:23:36 -07003931 /* put the old pwqs */
3932 for_each_node(node)
3933 put_pwq_unlocked(pwq_tbl[node]);
3934 put_pwq_unlocked(dfl_pwq);
3935
3936 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003937 ret = 0;
3938 /* fall through */
3939out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003940 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003941 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003942 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003943 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003944
Tejun Heo4c16bd32013-04-01 11:23:36 -07003945enomem_pwq:
3946 free_unbound_pwq(dfl_pwq);
3947 for_each_node(node)
3948 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3949 free_unbound_pwq(pwq_tbl[node]);
3950 mutex_unlock(&wq_pool_mutex);
3951 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003952enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003953 ret = -ENOMEM;
3954 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003955}
3956
Tejun Heo4c16bd32013-04-01 11:23:36 -07003957/**
3958 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3959 * @wq: the target workqueue
3960 * @cpu: the CPU coming up or going down
3961 * @online: whether @cpu is coming up or going down
3962 *
3963 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3964 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3965 * @wq accordingly.
3966 *
3967 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3968 * falls back to @wq->dfl_pwq which may not be optimal but is always
3969 * correct.
3970 *
3971 * Note that when the last allowed CPU of a NUMA node goes offline for a
3972 * workqueue with a cpumask spanning multiple nodes, the workers which were
3973 * already executing the work items for the workqueue will lose their CPU
3974 * affinity and may execute on any CPU. This is similar to how per-cpu
3975 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3976 * affinity, it's the user's responsibility to flush the work item from
3977 * CPU_DOWN_PREPARE.
3978 */
3979static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3980 bool online)
3981{
3982 int node = cpu_to_node(cpu);
3983 int cpu_off = online ? -1 : cpu;
3984 struct pool_workqueue *old_pwq = NULL, *pwq;
3985 struct workqueue_attrs *target_attrs;
3986 cpumask_t *cpumask;
3987
3988 lockdep_assert_held(&wq_pool_mutex);
3989
3990 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3991 return;
3992
3993 /*
3994 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3995 * Let's use a preallocated one. The following buf is protected by
3996 * CPU hotplug exclusion.
3997 */
3998 target_attrs = wq_update_unbound_numa_attrs_buf;
3999 cpumask = target_attrs->cpumask;
4000
4001 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07004002 if (wq->unbound_attrs->no_numa)
4003 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004004
4005 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
4006 pwq = unbound_pwq_by_node(wq, node);
4007
4008 /*
4009 * Let's determine what needs to be done. If the target cpumask is
4010 * different from wq's, we need to compare it to @pwq's and create
4011 * a new one if they don't match. If the target cpumask equals
4012 * wq's, the default pwq should be used. If @pwq is already the
4013 * default one, nothing to do; otherwise, install the default one.
4014 */
4015 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
4016 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4017 goto out_unlock;
4018 } else {
4019 if (pwq == wq->dfl_pwq)
4020 goto out_unlock;
4021 else
4022 goto use_dfl_pwq;
4023 }
4024
4025 mutex_unlock(&wq->mutex);
4026
4027 /* create a new pwq */
4028 pwq = alloc_unbound_pwq(wq, target_attrs);
4029 if (!pwq) {
4030 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4031 wq->name);
4032 goto out_unlock;
4033 }
4034
4035 /*
4036 * Install the new pwq. As this function is called only from CPU
4037 * hotplug callbacks and applying a new attrs is wrapped with
4038 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4039 * inbetween.
4040 */
4041 mutex_lock(&wq->mutex);
4042 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4043 goto out_unlock;
4044
4045use_dfl_pwq:
4046 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4047 get_pwq(wq->dfl_pwq);
4048 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4049 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4050out_unlock:
4051 mutex_unlock(&wq->mutex);
4052 put_pwq_unlocked(old_pwq);
4053}
4054
Tejun Heo30cdf242013-03-12 11:29:57 -07004055static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004057 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07004058 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004059
Tejun Heo30cdf242013-03-12 11:29:57 -07004060 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004061 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4062 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004063 return -ENOMEM;
4064
4065 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004066 struct pool_workqueue *pwq =
4067 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004068 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004069 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004070
Tejun Heof147f292013-04-01 11:23:35 -07004071 init_pwq(pwq, wq, &cpu_pools[highpri]);
4072
4073 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004074 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004075 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004076 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004077 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07004078 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004079 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004080 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004081}
4082
Tejun Heof3421792010-07-02 10:03:51 +02004083static int wq_clamp_max_active(int max_active, unsigned int flags,
4084 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004085{
Tejun Heof3421792010-07-02 10:03:51 +02004086 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4087
4088 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004089 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4090 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004091
Tejun Heof3421792010-07-02 10:03:51 +02004092 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004093}
4094
Tejun Heob196be82012-01-10 15:11:35 -08004095struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004096 unsigned int flags,
4097 int max_active,
4098 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004099 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004100{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004101 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004102 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004103 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004104 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004105
Viresh Kumarcee22a12013-04-08 16:45:40 +05304106 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4107 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4108 flags |= WQ_UNBOUND;
4109
Tejun Heoecf68812013-04-01 11:23:34 -07004110 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004111 if (flags & WQ_UNBOUND)
4112 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4113
4114 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004115 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004116 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004117
Tejun Heo6029a912013-04-01 11:23:34 -07004118 if (flags & WQ_UNBOUND) {
4119 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4120 if (!wq->unbound_attrs)
4121 goto err_free_wq;
4122 }
4123
Tejun Heoecf68812013-04-01 11:23:34 -07004124 va_start(args, lock_name);
4125 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004126 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004127
Tejun Heod320c032010-06-29 10:07:14 +02004128 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004129 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004130
Tejun Heob196be82012-01-10 15:11:35 -08004131 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004132 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004133 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004134 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004135 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004136 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004137 INIT_LIST_HEAD(&wq->flusher_queue);
4138 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004139 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004140
Johannes Bergeb13ba82008-01-16 09:51:58 +01004141 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004142 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004143
Tejun Heo30cdf242013-03-12 11:29:57 -07004144 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004145 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004146
Tejun Heo493008a2013-03-12 11:30:03 -07004147 /*
4148 * Workqueues which may be used during memory reclaim should
4149 * have a rescuer to guarantee forward progress.
4150 */
4151 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004152 struct worker *rescuer;
4153
Tejun Heod2c1d402013-03-12 11:30:04 -07004154 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004155 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004156 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004157
Tejun Heo111c2252013-01-17 17:16:24 -08004158 rescuer->rescue_wq = wq;
4159 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004160 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004161 if (IS_ERR(rescuer->task)) {
4162 kfree(rescuer);
4163 goto err_destroy;
4164 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004165
Tejun Heod2c1d402013-03-12 11:30:04 -07004166 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004167 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004168 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004169 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004170
Tejun Heo226223a2013-03-12 11:30:05 -07004171 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4172 goto err_destroy;
4173
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004174 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004175 * wq_pool_mutex protects global freeze state and workqueues list.
4176 * Grab it, adjust max_active and add the new @wq to workqueues
4177 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004178 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004179 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004180
Lai Jiangshana357fc02013-03-25 16:57:19 -07004181 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004182 for_each_pwq(pwq, wq)
4183 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004184 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004185
Tejun Heo15376632010-06-29 10:07:11 +02004186 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004187
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004188 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004189
Oleg Nesterov3af244332007-05-09 02:34:09 -07004190 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004191
4192err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004193 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004194 kfree(wq);
4195 return NULL;
4196err_destroy:
4197 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004198 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004199}
Tejun Heod320c032010-06-29 10:07:14 +02004200EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004201
4202/**
4203 * destroy_workqueue - safely terminate a workqueue
4204 * @wq: target workqueue
4205 *
4206 * Safely destroy a workqueue. All work currently pending will be done first.
4207 */
4208void destroy_workqueue(struct workqueue_struct *wq)
4209{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004210 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004211 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004212
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004213 /* drain it before proceeding with destruction */
4214 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004215
Tejun Heo6183c002013-03-12 11:29:57 -07004216 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004217 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004218 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004219 int i;
4220
Tejun Heo76af4d92013-03-12 11:30:00 -07004221 for (i = 0; i < WORK_NR_COLORS; i++) {
4222 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004223 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004224 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004225 }
4226 }
4227
Lai Jiangshan5c529592013-04-04 10:05:38 +08004228 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004229 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004230 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004231 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004232 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004233 }
Tejun Heo6183c002013-03-12 11:29:57 -07004234 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004235 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004236
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004237 /*
4238 * wq list is used to freeze wq, remove from list after
4239 * flushing is complete in case freeze races us.
4240 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004241 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004242 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004243 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004244
Tejun Heo226223a2013-03-12 11:30:05 -07004245 workqueue_sysfs_unregister(wq);
4246
Tejun Heo493008a2013-03-12 11:30:03 -07004247 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004248 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004249 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004250 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004251 }
4252
Tejun Heo8864b4e2013-03-12 11:30:04 -07004253 if (!(wq->flags & WQ_UNBOUND)) {
4254 /*
4255 * The base ref is never dropped on per-cpu pwqs. Directly
4256 * free the pwqs and wq.
4257 */
4258 free_percpu(wq->cpu_pwqs);
4259 kfree(wq);
4260 } else {
4261 /*
4262 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004263 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4264 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004265 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004266 for_each_node(node) {
4267 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4268 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4269 put_pwq_unlocked(pwq);
4270 }
4271
4272 /*
4273 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4274 * put. Don't access it afterwards.
4275 */
4276 pwq = wq->dfl_pwq;
4277 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004278 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004279 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004280}
4281EXPORT_SYMBOL_GPL(destroy_workqueue);
4282
Tejun Heodcd989c2010-06-29 10:07:14 +02004283/**
4284 * workqueue_set_max_active - adjust max_active of a workqueue
4285 * @wq: target workqueue
4286 * @max_active: new max_active value.
4287 *
4288 * Set max_active of @wq to @max_active.
4289 *
4290 * CONTEXT:
4291 * Don't call from IRQ context.
4292 */
4293void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4294{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004295 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004296
Tejun Heo8719dce2013-03-12 11:30:04 -07004297 /* disallow meddling with max_active for ordered workqueues */
4298 if (WARN_ON(wq->flags & __WQ_ORDERED))
4299 return;
4300
Tejun Heof3421792010-07-02 10:03:51 +02004301 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004302
Lai Jiangshana357fc02013-03-25 16:57:19 -07004303 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004304
4305 wq->saved_max_active = max_active;
4306
Tejun Heo699ce092013-03-13 16:51:35 -07004307 for_each_pwq(pwq, wq)
4308 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004309
Lai Jiangshana357fc02013-03-25 16:57:19 -07004310 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004311}
4312EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4313
4314/**
Tejun Heoe6267612013-03-12 17:41:37 -07004315 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4316 *
4317 * Determine whether %current is a workqueue rescuer. Can be used from
4318 * work functions to determine whether it's being run off the rescuer task.
4319 */
4320bool current_is_workqueue_rescuer(void)
4321{
4322 struct worker *worker = current_wq_worker();
4323
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004324 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07004325}
4326
4327/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004328 * workqueue_congested - test whether a workqueue is congested
4329 * @cpu: CPU in question
4330 * @wq: target workqueue
4331 *
4332 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4333 * no synchronization around this function and the test result is
4334 * unreliable and only useful as advisory hints or for debugging.
4335 *
Tejun Heod3251852013-05-10 11:10:17 -07004336 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4337 * Note that both per-cpu and unbound workqueues may be associated with
4338 * multiple pool_workqueues which have separate congested states. A
4339 * workqueue being congested on one CPU doesn't mean the workqueue is also
4340 * contested on other CPUs / NUMA nodes.
4341 *
Tejun Heodcd989c2010-06-29 10:07:14 +02004342 * RETURNS:
4343 * %true if congested, %false otherwise.
4344 */
Tejun Heod84ff052013-03-12 11:29:59 -07004345bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004346{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004347 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004348 bool ret;
4349
Lai Jiangshan88109452013-03-20 03:28:10 +08004350 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004351
Tejun Heod3251852013-05-10 11:10:17 -07004352 if (cpu == WORK_CPU_UNBOUND)
4353 cpu = smp_processor_id();
4354
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004355 if (!(wq->flags & WQ_UNBOUND))
4356 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4357 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004358 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004359
Tejun Heo76af4d92013-03-12 11:30:00 -07004360 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004361 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004362
4363 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004364}
4365EXPORT_SYMBOL_GPL(workqueue_congested);
4366
4367/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004368 * work_busy - test whether a work is currently pending or running
4369 * @work: the work to be tested
4370 *
4371 * Test whether @work is currently pending or running. There is no
4372 * synchronization around this function and the test result is
4373 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004374 *
4375 * RETURNS:
4376 * OR'd bitmask of WORK_BUSY_* bits.
4377 */
4378unsigned int work_busy(struct work_struct *work)
4379{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004380 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004381 unsigned long flags;
4382 unsigned int ret = 0;
4383
Tejun Heodcd989c2010-06-29 10:07:14 +02004384 if (work_pending(work))
4385 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004386
Tejun Heofa1b54e2013-03-12 11:30:00 -07004387 local_irq_save(flags);
4388 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004389 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004390 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004391 if (find_worker_executing_work(pool, work))
4392 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004393 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004394 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004395 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004396
4397 return ret;
4398}
4399EXPORT_SYMBOL_GPL(work_busy);
4400
Tejun Heo3d1cb202013-04-30 15:27:22 -07004401/**
4402 * set_worker_desc - set description for the current work item
4403 * @fmt: printf-style format string
4404 * @...: arguments for the format string
4405 *
4406 * This function can be called by a running work function to describe what
4407 * the work item is about. If the worker task gets dumped, this
4408 * information will be printed out together to help debugging. The
4409 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4410 */
4411void set_worker_desc(const char *fmt, ...)
4412{
4413 struct worker *worker = current_wq_worker();
4414 va_list args;
4415
4416 if (worker) {
4417 va_start(args, fmt);
4418 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4419 va_end(args);
4420 worker->desc_valid = true;
4421 }
4422}
4423
4424/**
4425 * print_worker_info - print out worker information and description
4426 * @log_lvl: the log level to use when printing
4427 * @task: target task
4428 *
4429 * If @task is a worker and currently executing a work item, print out the
4430 * name of the workqueue being serviced and worker description set with
4431 * set_worker_desc() by the currently executing work item.
4432 *
4433 * This function can be safely called on any task as long as the
4434 * task_struct itself is accessible. While safe, this function isn't
4435 * synchronized and may print out mixups or garbages of limited length.
4436 */
4437void print_worker_info(const char *log_lvl, struct task_struct *task)
4438{
4439 work_func_t *fn = NULL;
4440 char name[WQ_NAME_LEN] = { };
4441 char desc[WORKER_DESC_LEN] = { };
4442 struct pool_workqueue *pwq = NULL;
4443 struct workqueue_struct *wq = NULL;
4444 bool desc_valid = false;
4445 struct worker *worker;
4446
4447 if (!(task->flags & PF_WQ_WORKER))
4448 return;
4449
4450 /*
4451 * This function is called without any synchronization and @task
4452 * could be in any state. Be careful with dereferences.
4453 */
4454 worker = probe_kthread_data(task);
4455
4456 /*
4457 * Carefully copy the associated workqueue's workfn and name. Keep
4458 * the original last '\0' in case the original contains garbage.
4459 */
4460 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4461 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4462 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4463 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4464
4465 /* copy worker description */
4466 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4467 if (desc_valid)
4468 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4469
4470 if (fn || name[0] || desc[0]) {
4471 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4472 if (desc[0])
4473 pr_cont(" (%s)", desc);
4474 pr_cont("\n");
4475 }
4476}
4477
Tejun Heodb7bccf2010-06-29 10:07:12 +02004478/*
4479 * CPU hotplug.
4480 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004481 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004482 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004483 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004484 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004485 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004486 * blocked draining impractical.
4487 *
Tejun Heo24647572013-01-24 11:01:33 -08004488 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004489 * running as an unbound one and allowing it to be reattached later if the
4490 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004491 */
4492
Tejun Heo706026c2013-01-24 11:01:34 -08004493static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004494{
Tejun Heo38db41d2013-01-24 11:01:34 -08004495 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004496 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004497 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004498 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004499
Tejun Heof02ae732013-03-12 11:30:03 -07004500 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004501 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004502
Tejun Heobc3a1af2013-03-13 19:47:39 -07004503 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004504 spin_lock_irq(&pool->lock);
4505
4506 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004507 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004508 * unbound and set DISASSOCIATED. Before this, all workers
4509 * except for the ones which are still executing works from
4510 * before the last CPU down must be on the cpu. After
4511 * this, they may become diasporas.
4512 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004513 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004514 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004515
Tejun Heo24647572013-01-24 11:01:33 -08004516 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004517
Tejun Heo94cf58b2013-01-24 11:01:33 -08004518 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004519 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004520
Lai Jiangshaneb283422013-03-08 15:18:28 -08004521 /*
4522 * Call schedule() so that we cross rq->lock and thus can
4523 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4524 * This is necessary as scheduler callbacks may be invoked
4525 * from other cpus.
4526 */
4527 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004528
Lai Jiangshaneb283422013-03-08 15:18:28 -08004529 /*
4530 * Sched callbacks are disabled now. Zap nr_running.
4531 * After this, nr_running stays zero and need_more_worker()
4532 * and keep_working() are always true as long as the
4533 * worklist is not empty. This pool now behaves as an
4534 * unbound (in terms of concurrency management) pool which
4535 * are served by workers tied to the pool.
4536 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004537 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004538
4539 /*
4540 * With concurrency management just turned off, a busy
4541 * worker blocking could lead to lengthy stalls. Kick off
4542 * unbound chain execution of currently pending work items.
4543 */
4544 spin_lock_irq(&pool->lock);
4545 wake_up_worker(pool);
4546 spin_unlock_irq(&pool->lock);
4547 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004548}
4549
Tejun Heobd7c0892013-03-19 13:45:21 -07004550/**
4551 * rebind_workers - rebind all workers of a pool to the associated CPU
4552 * @pool: pool of interest
4553 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004554 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004555 */
4556static void rebind_workers(struct worker_pool *pool)
4557{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004558 struct worker *worker;
4559 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004560
4561 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004562
Tejun Heoa9ab7752013-03-19 13:45:21 -07004563 /*
4564 * Restore CPU affinity of all workers. As all idle workers should
4565 * be on the run-queue of the associated CPU before any local
4566 * wake-ups for concurrency management happen, restore CPU affinty
4567 * of all workers first and then clear UNBOUND. As we're called
4568 * from CPU_ONLINE, the following shouldn't fail.
4569 */
4570 for_each_pool_worker(worker, wi, pool)
4571 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4572 pool->attrs->cpumask) < 0);
4573
4574 spin_lock_irq(&pool->lock);
4575
4576 for_each_pool_worker(worker, wi, pool) {
4577 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004578
4579 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004580 * A bound idle worker should actually be on the runqueue
4581 * of the associated CPU for local wake-ups targeting it to
4582 * work. Kick all idle workers so that they migrate to the
4583 * associated CPU. Doing this in the same loop as
4584 * replacing UNBOUND with REBOUND is safe as no worker will
4585 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004586 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004587 if (worker_flags & WORKER_IDLE)
4588 wake_up_process(worker->task);
4589
4590 /*
4591 * We want to clear UNBOUND but can't directly call
4592 * worker_clr_flags() or adjust nr_running. Atomically
4593 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4594 * @worker will clear REBOUND using worker_clr_flags() when
4595 * it initiates the next execution cycle thus restoring
4596 * concurrency management. Note that when or whether
4597 * @worker clears REBOUND doesn't affect correctness.
4598 *
4599 * ACCESS_ONCE() is necessary because @worker->flags may be
4600 * tested without holding any lock in
4601 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4602 * fail incorrectly leading to premature concurrency
4603 * management operations.
4604 */
4605 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4606 worker_flags |= WORKER_REBOUND;
4607 worker_flags &= ~WORKER_UNBOUND;
4608 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004609 }
4610
Tejun Heoa9ab7752013-03-19 13:45:21 -07004611 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004612}
4613
Tejun Heo7dbc7252013-03-19 13:45:21 -07004614/**
4615 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4616 * @pool: unbound pool of interest
4617 * @cpu: the CPU which is coming up
4618 *
4619 * An unbound pool may end up with a cpumask which doesn't have any online
4620 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4621 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4622 * online CPU before, cpus_allowed of all its workers should be restored.
4623 */
4624static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4625{
4626 static cpumask_t cpumask;
4627 struct worker *worker;
4628 int wi;
4629
4630 lockdep_assert_held(&pool->manager_mutex);
4631
4632 /* is @cpu allowed for @pool? */
4633 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4634 return;
4635
4636 /* is @cpu the only online CPU? */
4637 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4638 if (cpumask_weight(&cpumask) != 1)
4639 return;
4640
4641 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4642 for_each_pool_worker(worker, wi, pool)
4643 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4644 pool->attrs->cpumask) < 0);
4645}
4646
Tejun Heo8db25e72012-07-17 12:39:28 -07004647/*
4648 * Workqueues should be brought up before normal priority CPU notifiers.
4649 * This will be registered high priority CPU notifier.
4650 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004651static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004652 unsigned long action,
4653 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004654{
Tejun Heod84ff052013-03-12 11:29:59 -07004655 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004656 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004657 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004658 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Tejun Heo8db25e72012-07-17 12:39:28 -07004660 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004662 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004663 if (pool->nr_workers)
4664 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004665 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004666 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004668 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004669
Tejun Heo65758202012-07-17 12:39:26 -07004670 case CPU_DOWN_FAILED:
4671 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004672 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004673
4674 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004675 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004676
Tejun Heo7dbc7252013-03-19 13:45:21 -07004677 if (pool->cpu == cpu) {
4678 spin_lock_irq(&pool->lock);
4679 pool->flags &= ~POOL_DISASSOCIATED;
4680 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004681
Tejun Heo7dbc7252013-03-19 13:45:21 -07004682 rebind_workers(pool);
4683 } else if (pool->cpu < 0) {
4684 restore_unbound_workers_cpumask(pool, cpu);
4685 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004686
Tejun Heobc3a1af2013-03-13 19:47:39 -07004687 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004688 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004689
Tejun Heo4c16bd32013-04-01 11:23:36 -07004690 /* update NUMA affinity of unbound workqueues */
4691 list_for_each_entry(wq, &workqueues, list)
4692 wq_update_unbound_numa(wq, cpu, true);
4693
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004694 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004695 break;
Tejun Heo65758202012-07-17 12:39:26 -07004696 }
4697 return NOTIFY_OK;
4698}
4699
4700/*
4701 * Workqueues should be brought down after normal priority CPU notifiers.
4702 * This will be registered as low priority CPU notifier.
4703 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004704static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004705 unsigned long action,
4706 void *hcpu)
4707{
Tejun Heod84ff052013-03-12 11:29:59 -07004708 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004709 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004710 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004711
Tejun Heo65758202012-07-17 12:39:26 -07004712 switch (action & ~CPU_TASKS_FROZEN) {
4713 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004714 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004715 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004716 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004717
4718 /* update NUMA affinity of unbound workqueues */
4719 mutex_lock(&wq_pool_mutex);
4720 list_for_each_entry(wq, &workqueues, list)
4721 wq_update_unbound_numa(wq, cpu, false);
4722 mutex_unlock(&wq_pool_mutex);
4723
4724 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004725 flush_work(&unbind_work);
4726 break;
Tejun Heo65758202012-07-17 12:39:26 -07004727 }
4728 return NOTIFY_OK;
4729}
4730
Rusty Russell2d3854a2008-11-05 13:39:10 +11004731#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004732
Rusty Russell2d3854a2008-11-05 13:39:10 +11004733struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004734 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004735 long (*fn)(void *);
4736 void *arg;
4737 long ret;
4738};
4739
Tejun Heoed48ece2012-09-18 12:48:43 -07004740static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004741{
Tejun Heoed48ece2012-09-18 12:48:43 -07004742 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4743
Rusty Russell2d3854a2008-11-05 13:39:10 +11004744 wfc->ret = wfc->fn(wfc->arg);
4745}
4746
4747/**
4748 * work_on_cpu - run a function in user context on a particular cpu
4749 * @cpu: the cpu to run on
4750 * @fn: the function to run
4751 * @arg: the function arg
4752 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004753 * This will return the value @fn returns.
4754 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004755 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004756 */
Tejun Heod84ff052013-03-12 11:29:59 -07004757long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004758{
Tejun Heoed48ece2012-09-18 12:48:43 -07004759 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004760
Tejun Heoed48ece2012-09-18 12:48:43 -07004761 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4762 schedule_work_on(cpu, &wfc.work);
4763 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004764 return wfc.ret;
4765}
4766EXPORT_SYMBOL_GPL(work_on_cpu);
4767#endif /* CONFIG_SMP */
4768
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004769#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304770
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004771/**
4772 * freeze_workqueues_begin - begin freezing workqueues
4773 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004774 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004775 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004776 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004777 *
4778 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004779 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004780 */
4781void freeze_workqueues_begin(void)
4782{
Tejun Heo17116962013-03-12 11:29:58 -07004783 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004784 struct workqueue_struct *wq;
4785 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004786 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004787
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004788 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004789
Tejun Heo6183c002013-03-12 11:29:57 -07004790 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004791 workqueue_freezing = true;
4792
Tejun Heo24b8a842013-03-12 11:29:58 -07004793 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004794 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004795 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004796 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4797 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004798 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004799 }
4800
Tejun Heo24b8a842013-03-12 11:29:58 -07004801 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004802 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004803 for_each_pwq(pwq, wq)
4804 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004805 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004806 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004807
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004808 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004810
4811/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004812 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004813 *
4814 * Check whether freezing is complete. This function must be called
4815 * between freeze_workqueues_begin() and thaw_workqueues().
4816 *
4817 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004818 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004819 *
4820 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004821 * %true if some freezable workqueues are still busy. %false if freezing
4822 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004823 */
4824bool freeze_workqueues_busy(void)
4825{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004826 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004827 struct workqueue_struct *wq;
4828 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004829
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004830 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004831
Tejun Heo6183c002013-03-12 11:29:57 -07004832 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004833
Tejun Heo24b8a842013-03-12 11:29:58 -07004834 list_for_each_entry(wq, &workqueues, list) {
4835 if (!(wq->flags & WQ_FREEZABLE))
4836 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004837 /*
4838 * nr_active is monotonically decreasing. It's safe
4839 * to peek without lock.
4840 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004841 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004842 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004843 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004844 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004845 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004846 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004847 goto out_unlock;
4848 }
4849 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004850 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004851 }
4852out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004853 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004854 return busy;
4855}
4856
4857/**
4858 * thaw_workqueues - thaw workqueues
4859 *
4860 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004861 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004862 *
4863 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004864 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004865 */
4866void thaw_workqueues(void)
4867{
Tejun Heo24b8a842013-03-12 11:29:58 -07004868 struct workqueue_struct *wq;
4869 struct pool_workqueue *pwq;
4870 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004871 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004872
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004873 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004874
4875 if (!workqueue_freezing)
4876 goto out_unlock;
4877
Tejun Heo24b8a842013-03-12 11:29:58 -07004878 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004879 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004880 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004881 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4882 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004883 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004884 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004885
Tejun Heo24b8a842013-03-12 11:29:58 -07004886 /* restore max_active and repopulate worklist */
4887 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004888 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004889 for_each_pwq(pwq, wq)
4890 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004891 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004892 }
4893
4894 workqueue_freezing = false;
4895out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004896 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004897}
4898#endif /* CONFIG_FREEZER */
4899
Tejun Heobce90382013-04-01 11:23:32 -07004900static void __init wq_numa_init(void)
4901{
4902 cpumask_var_t *tbl;
4903 int node, cpu;
4904
4905 /* determine NUMA pwq table len - highest node id + 1 */
4906 for_each_node(node)
4907 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4908
4909 if (num_possible_nodes() <= 1)
4910 return;
4911
Tejun Heod55262c2013-04-01 11:23:38 -07004912 if (wq_disable_numa) {
4913 pr_info("workqueue: NUMA affinity support disabled\n");
4914 return;
4915 }
4916
Tejun Heo4c16bd32013-04-01 11:23:36 -07004917 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4918 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4919
Tejun Heobce90382013-04-01 11:23:32 -07004920 /*
4921 * We want masks of possible CPUs of each node which isn't readily
4922 * available. Build one from cpu_to_node() which should have been
4923 * fully initialized by now.
4924 */
4925 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4926 BUG_ON(!tbl);
4927
4928 for_each_node(node)
Tejun Heo1be0c252013-05-15 14:24:24 -07004929 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
4930 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07004931
4932 for_each_possible_cpu(cpu) {
4933 node = cpu_to_node(cpu);
4934 if (WARN_ON(node == NUMA_NO_NODE)) {
4935 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4936 /* happens iff arch is bonkers, let's just proceed */
4937 return;
4938 }
4939 cpumask_set_cpu(cpu, tbl[node]);
4940 }
4941
4942 wq_numa_possible_cpumask = tbl;
4943 wq_numa_enabled = true;
4944}
4945
Suresh Siddha6ee05782010-07-30 14:57:37 -07004946static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004948 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4949 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004950
Tejun Heo7c3eed52013-01-24 11:01:33 -08004951 /* make sure we have enough bits for OFFQ pool ID */
4952 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004953 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004954
Tejun Heoe904e6c2013-03-12 11:29:57 -07004955 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4956
4957 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4958
Tejun Heo65758202012-07-17 12:39:26 -07004959 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004960 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004961
Tejun Heobce90382013-04-01 11:23:32 -07004962 wq_numa_init();
4963
Tejun Heo706026c2013-01-24 11:01:34 -08004964 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004965 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004966 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004967
Tejun Heo7a4e3442013-03-12 11:30:00 -07004968 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004969 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004970 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004971 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004972 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004973 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004974 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004975
Tejun Heo9daf9e62013-01-24 11:01:33 -08004976 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004977 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004978 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004979 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004980 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004981 }
4982
Tejun Heoe22bee72010-06-29 10:07:14 +02004983 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004984 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004985 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004986
Tejun Heof02ae732013-03-12 11:30:03 -07004987 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004988 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004989 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004990 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004991 }
4992
Tejun Heo29c91e92013-03-12 11:30:03 -07004993 /* create default unbound wq attrs */
4994 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4995 struct workqueue_attrs *attrs;
4996
4997 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004998 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004999 unbound_std_wq_attrs[i] = attrs;
5000 }
5001
Tejun Heod320c032010-06-29 10:07:14 +02005002 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005003 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005004 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005005 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5006 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005007 system_freezable_wq = alloc_workqueue("events_freezable",
5008 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305009 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5010 WQ_POWER_EFFICIENT, 0);
5011 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5012 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5013 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005014 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305015 !system_unbound_wq || !system_freezable_wq ||
5016 !system_power_efficient_wq ||
5017 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005020early_initcall(init_workqueues);