blob: eebd9a66c04462b9e5ccffe741ee7755d6365d79 [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 Lameter469340232006-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 Heo794b18bc2013-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 Heo974271c42012-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 Heo974271c42012-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 Heo9f9c236442010-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 Heo9f9c236442010-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:
Tejun Heod565ed62013-01-24 11:01:33 -08002037 * spin_lock_irq(pool->lock) which may be released and regrabbed
2038 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002039 */
2040static bool manage_workers(struct worker *worker)
2041{
Tejun Heo63d95a92012-07-12 14:46:37 -07002042 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002043 bool ret = false;
2044
Tejun Heobc3a1af2013-03-13 19:47:39 -07002045 /*
2046 * Managership is governed by two mutexes - manager_arb and
2047 * manager_mutex. manager_arb handles arbitration of manager role.
2048 * Anyone who successfully grabs manager_arb wins the arbitration
2049 * and becomes the manager. mutex_trylock() on pool->manager_arb
2050 * failure while holding pool->lock reliably indicates that someone
2051 * else is managing the pool and the worker which failed trylock
2052 * can proceed to executing work items. This means that anyone
2053 * grabbing manager_arb is responsible for actually performing
2054 * manager duties. If manager_arb is grabbed and released without
2055 * actual management, the pool may stall indefinitely.
2056 *
2057 * manager_mutex is used for exclusion of actual management
2058 * operations. The holder of manager_mutex can be sure that none
2059 * of management operations, including creation and destruction of
2060 * workers, won't take place until the mutex is released. Because
2061 * manager_mutex doesn't interfere with manager role arbitration,
2062 * it is guaranteed that the pool's management, while may be
2063 * delayed, won't be disturbed by someone else grabbing
2064 * manager_mutex.
2065 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002066 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002067 return ret;
2068
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002069 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002070 * With manager arbitration won, manager_mutex would be free in
2071 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002072 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002073 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002074 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002075 mutex_lock(&pool->manager_mutex);
Joonsoo Kim8f174b12013-05-01 00:07:00 +09002076 spin_lock_irq(&pool->lock);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002077 ret = true;
2078 }
2079
Tejun Heo11ebea52012-07-12 14:46:37 -07002080 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002081
2082 /*
2083 * Destroy and then create so that may_start_working() is true
2084 * on return.
2085 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002086 ret |= maybe_destroy_workers(pool);
2087 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002088
Tejun Heobc3a1af2013-03-13 19:47:39 -07002089 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002090 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002091 return ret;
2092}
2093
Tejun Heoa62428c2010-06-29 10:07:10 +02002094/**
2095 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002096 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002097 * @work: work to process
2098 *
2099 * Process @work. This function contains all the logics necessary to
2100 * process a single work including synchronization against and
2101 * interaction with other workers on the same cpu, queueing and
2102 * flushing. As long as context requirement is met, any worker can
2103 * call this function to process a work.
2104 *
2105 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002106 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002107 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002108static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002109__releases(&pool->lock)
2110__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002111{
Tejun Heo112202d2013-02-13 19:29:12 -08002112 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002113 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002114 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002115 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002116 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002117#ifdef CONFIG_LOCKDEP
2118 /*
2119 * It is permissible to free the struct work_struct from
2120 * inside the function that is called from it, this we need to
2121 * take into account for lockdep too. To avoid bogus "held
2122 * lock freed" warnings as well as problems when looking into
2123 * work->lockdep_map, make a copy and use that here.
2124 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002125 struct lockdep_map lockdep_map;
2126
2127 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002128#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002129 /*
2130 * Ensure we're on the correct CPU. DISASSOCIATED test is
2131 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002132 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002133 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002134 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002135 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002136 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002137
Tejun Heo7e116292010-06-29 10:07:13 +02002138 /*
2139 * A single work shouldn't be executed concurrently by
2140 * multiple workers on a single cpu. Check whether anyone is
2141 * already processing the work. If so, defer the work to the
2142 * currently executing one.
2143 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002144 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002145 if (unlikely(collision)) {
2146 move_linked_works(work, &collision->scheduled, NULL);
2147 return;
2148 }
2149
Tejun Heo8930cab2012-08-03 10:30:45 -07002150 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002151 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002152 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002153 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002154 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002155 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002156 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002157
Tejun Heoa62428c2010-06-29 10:07:10 +02002158 list_del_init(&work->entry);
2159
Tejun Heo649027d2010-06-29 10:07:14 +02002160 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002161 * CPU intensive works don't participate in concurrency
2162 * management. They're the scheduler's responsibility.
2163 */
2164 if (unlikely(cpu_intensive))
2165 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2166
Tejun Heo974271c42012-07-12 14:46:37 -07002167 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002168 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c42012-07-12 14:46:37 -07002169 * executed ASAP. Wake up another worker if necessary.
2170 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002171 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2172 wake_up_worker(pool);
Tejun Heo974271c42012-07-12 14:46:37 -07002173
Tejun Heo8930cab2012-08-03 10:30:45 -07002174 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002175 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002176 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002177 * PENDING and queued state changes happen together while IRQ is
2178 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002179 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002180 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002181
Tejun Heod565ed62013-01-24 11:01:33 -08002182 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002183
Tejun Heo112202d2013-02-13 19:29:12 -08002184 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002185 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002186 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002187 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002188 /*
2189 * While we must be careful to not use "work" after this, the trace
2190 * point will only record its address.
2191 */
2192 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002193 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002194 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002195
2196 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002197 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2198 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002199 current->comm, preempt_count(), task_pid_nr(current),
2200 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201 debug_show_held_locks(current);
2202 dump_stack();
2203 }
2204
Tejun Heod565ed62013-01-24 11:01:33 -08002205 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002206
Tejun Heofb0e7be2010-06-29 10:07:15 +02002207 /* clear cpu intensive status */
2208 if (unlikely(cpu_intensive))
2209 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2210
Tejun Heoa62428c2010-06-29 10:07:10 +02002211 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002212 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002213 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002214 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002215 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002216 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002217 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002218}
2219
Tejun Heoaffee4b2010-06-29 10:07:12 +02002220/**
2221 * process_scheduled_works - process scheduled works
2222 * @worker: self
2223 *
2224 * Process all scheduled works. Please note that the scheduled list
2225 * may change while processing a work, so this function repeatedly
2226 * fetches a work from the top and executes it.
2227 *
2228 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002229 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002230 * multiple times.
2231 */
2232static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002234 while (!list_empty(&worker->scheduled)) {
2235 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002237 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Tejun Heo4690c4a2010-06-29 10:07:10 +02002241/**
2242 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002243 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002244 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002245 * The worker thread function. All workers belong to a worker_pool -
2246 * either a per-cpu one or dynamic unbound one. These workers process all
2247 * work items regardless of their specific target workqueue. The only
2248 * exception is work items which belong to workqueues with a rescuer which
2249 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002250 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002251static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Tejun Heoc34056a2010-06-29 10:07:11 +02002253 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002254 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Tejun Heoe22bee72010-06-29 10:07:14 +02002256 /* tell the scheduler that this is a workqueue worker */
2257 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002258woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002259 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Tejun Heoa9ab7752013-03-19 13:45:21 -07002261 /* am I supposed to die? */
2262 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002263 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002264 WARN_ON_ONCE(!list_empty(&worker->entry));
2265 worker->task->flags &= ~PF_WQ_WORKER;
2266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
Tejun Heoc8e55f32010-06-29 10:07:12 +02002269 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002270recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002271 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002272 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002273 goto sleep;
2274
2275 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002276 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 goto recheck;
2278
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 /*
2280 * ->scheduled list can only be filled while a worker is
2281 * preparing to process a work or actually processing it.
2282 * Make sure nobody diddled with it while I was sleeping.
2283 */
Tejun Heo6183c002013-03-12 11:29:57 -07002284 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002285
Tejun Heoe22bee72010-06-29 10:07:14 +02002286 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002287 * Finish PREP stage. We're guaranteed to have at least one idle
2288 * worker or that someone else has already assumed the manager
2289 * role. This is where @worker starts participating in concurrency
2290 * management if applicable and concurrency management is restored
2291 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002292 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002293 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002294
2295 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002296 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002297 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002298 struct work_struct, entry);
2299
2300 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2301 /* optimization path, not strictly necessary */
2302 process_one_work(worker, work);
2303 if (unlikely(!list_empty(&worker->scheduled)))
2304 process_scheduled_works(worker);
2305 } else {
2306 move_linked_works(work, &worker->scheduled, NULL);
2307 process_scheduled_works(worker);
2308 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002309 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002310
Tejun Heoe22bee72010-06-29 10:07:14 +02002311 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002312sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002313 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002314 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002315
Tejun Heoc8e55f32010-06-29 10:07:12 +02002316 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002317 * pool->lock is held and there's no work to process and no need to
2318 * manage, sleep. Workers are woken up only while holding
2319 * pool->lock or from local cpu, so setting the current state
2320 * before releasing pool->lock is enough to prevent losing any
2321 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002322 */
2323 worker_enter_idle(worker);
2324 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002325 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002326 schedule();
2327 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Tejun Heoe22bee72010-06-29 10:07:14 +02002330/**
2331 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002332 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002333 *
2334 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002335 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 *
Tejun Heo706026c2013-01-24 11:01:34 -08002337 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002338 * worker which uses GFP_KERNEL allocation which has slight chance of
2339 * developing into deadlock if some works currently on the same queue
2340 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2341 * the problem rescuer solves.
2342 *
Tejun Heo706026c2013-01-24 11:01:34 -08002343 * When such condition is possible, the pool summons rescuers of all
2344 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002345 * those works so that forward progress can be guaranteed.
2346 *
2347 * This should happen rarely.
2348 */
Tejun Heo111c2252013-01-17 17:16:24 -08002349static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002350{
Tejun Heo111c2252013-01-17 17:16:24 -08002351 struct worker *rescuer = __rescuer;
2352 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002353 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002354
2355 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002356
2357 /*
2358 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2359 * doesn't participate in concurrency management.
2360 */
2361 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002362repeat:
2363 set_current_state(TASK_INTERRUPTIBLE);
2364
Mike Galbraith412d32e2012-11-28 07:17:18 +01002365 if (kthread_should_stop()) {
2366 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002367 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002368 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002369 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002370
Tejun Heo493a1722013-03-12 11:29:59 -07002371 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002372 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002373
2374 while (!list_empty(&wq->maydays)) {
2375 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2376 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002377 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002378 struct work_struct *work, *n;
2379
2380 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002381 list_del_init(&pwq->mayday_node);
2382
Tejun Heo2e109a22013-03-13 19:47:40 -07002383 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002384
2385 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002386 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002387 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002388
2389 /*
2390 * Slurp in all works issued via this workqueue and
2391 * process'em.
2392 */
Tejun Heo6183c002013-03-12 11:29:57 -07002393 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002394 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002395 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002396 move_linked_works(work, scheduled, &n);
2397
2398 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002399
2400 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002401 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002402 * regular worker; otherwise, we end up with 0 concurrency
2403 * and stalling the execution.
2404 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002405 if (keep_working(pool))
2406 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002407
Lai Jiangshanb3104102013-02-19 12:17:02 -08002408 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002409 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002410 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002411 }
2412
Tejun Heo2e109a22013-03-13 19:47:40 -07002413 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002414
Tejun Heo111c2252013-01-17 17:16:24 -08002415 /* rescuers should never participate in concurrency management */
2416 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002417 schedule();
2418 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002421struct wq_barrier {
2422 struct work_struct work;
2423 struct completion done;
2424};
2425
2426static void wq_barrier_func(struct work_struct *work)
2427{
2428 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2429 complete(&barr->done);
2430}
2431
Tejun Heo4690c4a2010-06-29 10:07:10 +02002432/**
2433 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002434 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002435 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002436 * @target: target work to attach @barr to
2437 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002438 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002439 * @barr is linked to @target such that @barr is completed only after
2440 * @target finishes execution. Please note that the ordering
2441 * guarantee is observed only with respect to @target and on the local
2442 * cpu.
2443 *
2444 * Currently, a queued barrier can't be canceled. This is because
2445 * try_to_grab_pending() can't determine whether the work to be
2446 * grabbed is at the head of the queue and thus can't clear LINKED
2447 * flag of the previous work while there must be a valid next work
2448 * after a work with LINKED flag set.
2449 *
2450 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002451 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002452 *
2453 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002454 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002455 */
Tejun Heo112202d2013-02-13 19:29:12 -08002456static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002457 struct wq_barrier *barr,
2458 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002459{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002460 struct list_head *head;
2461 unsigned int linked = 0;
2462
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002463 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002464 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002465 * as we know for sure that this will not trigger any of the
2466 * checks and call back into the fixup functions where we
2467 * might deadlock.
2468 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002469 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002470 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002471 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002472
Tejun Heoaffee4b2010-06-29 10:07:12 +02002473 /*
2474 * If @target is currently being executed, schedule the
2475 * barrier to the worker; otherwise, put it after @target.
2476 */
2477 if (worker)
2478 head = worker->scheduled.next;
2479 else {
2480 unsigned long *bits = work_data_bits(target);
2481
2482 head = target->entry.next;
2483 /* there can already be other linked works, inherit and set */
2484 linked = *bits & WORK_STRUCT_LINKED;
2485 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2486 }
2487
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002488 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002489 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002490 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002491}
2492
Tejun Heo73f53c42010-06-29 10:07:11 +02002493/**
Tejun Heo112202d2013-02-13 19:29:12 -08002494 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002495 * @wq: workqueue being flushed
2496 * @flush_color: new flush color, < 0 for no-op
2497 * @work_color: new work color, < 0 for no-op
2498 *
Tejun Heo112202d2013-02-13 19:29:12 -08002499 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002500 *
Tejun Heo112202d2013-02-13 19:29:12 -08002501 * If @flush_color is non-negative, flush_color on all pwqs should be
2502 * -1. If no pwq has in-flight commands at the specified color, all
2503 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2504 * has in flight commands, its pwq->flush_color is set to
2505 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002506 * wakeup logic is armed and %true is returned.
2507 *
2508 * The caller should have initialized @wq->first_flusher prior to
2509 * calling this function with non-negative @flush_color. If
2510 * @flush_color is negative, no flush color update is done and %false
2511 * is returned.
2512 *
Tejun Heo112202d2013-02-13 19:29:12 -08002513 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002514 * work_color which is previous to @work_color and all will be
2515 * advanced to @work_color.
2516 *
2517 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002518 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002519 *
2520 * RETURNS:
2521 * %true if @flush_color >= 0 and there's something to flush. %false
2522 * otherwise.
2523 */
Tejun Heo112202d2013-02-13 19:29:12 -08002524static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002525 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Tejun Heo73f53c42010-06-29 10:07:11 +02002527 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002528 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002529
Tejun Heo73f53c42010-06-29 10:07:11 +02002530 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002531 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002532 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002533 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002534
Tejun Heo49e3cf42013-03-12 11:29:58 -07002535 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002536 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002537
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002538 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002539
2540 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002541 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002542
Tejun Heo112202d2013-02-13 19:29:12 -08002543 if (pwq->nr_in_flight[flush_color]) {
2544 pwq->flush_color = flush_color;
2545 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002546 wait = true;
2547 }
2548 }
2549
2550 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002551 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002552 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002553 }
2554
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002555 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 }
2557
Tejun Heo112202d2013-02-13 19:29:12 -08002558 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 complete(&wq->first_flusher->done);
2560
2561 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002566 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002568 * This function sleeps until all work items which were queued on entry
2569 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002571void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
Tejun Heo73f53c42010-06-29 10:07:11 +02002573 struct wq_flusher this_flusher = {
2574 .list = LIST_HEAD_INIT(this_flusher.list),
2575 .flush_color = -1,
2576 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2577 };
2578 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002579
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002580 lock_map_acquire(&wq->lockdep_map);
2581 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002582
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002583 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002584
2585 /*
2586 * Start-to-wait phase
2587 */
2588 next_color = work_next_color(wq->work_color);
2589
2590 if (next_color != wq->flush_color) {
2591 /*
2592 * Color space is not full. The current work_color
2593 * becomes our flush_color and work_color is advanced
2594 * by one.
2595 */
Tejun Heo6183c002013-03-12 11:29:57 -07002596 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002597 this_flusher.flush_color = wq->work_color;
2598 wq->work_color = next_color;
2599
2600 if (!wq->first_flusher) {
2601 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002602 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002603
2604 wq->first_flusher = &this_flusher;
2605
Tejun Heo112202d2013-02-13 19:29:12 -08002606 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002607 wq->work_color)) {
2608 /* nothing to flush, done */
2609 wq->flush_color = next_color;
2610 wq->first_flusher = NULL;
2611 goto out_unlock;
2612 }
2613 } else {
2614 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002615 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002616 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002617 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002618 }
2619 } else {
2620 /*
2621 * Oops, color space is full, wait on overflow queue.
2622 * The next flush completion will assign us
2623 * flush_color and transfer to flusher_queue.
2624 */
2625 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2626 }
2627
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002628 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002629
2630 wait_for_completion(&this_flusher.done);
2631
2632 /*
2633 * Wake-up-and-cascade phase
2634 *
2635 * First flushers are responsible for cascading flushes and
2636 * handling overflow. Non-first flushers can simply return.
2637 */
2638 if (wq->first_flusher != &this_flusher)
2639 return;
2640
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002641 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002642
Tejun Heo4ce48b32010-07-02 10:03:51 +02002643 /* we might have raced, check again with mutex held */
2644 if (wq->first_flusher != &this_flusher)
2645 goto out_unlock;
2646
Tejun Heo73f53c42010-06-29 10:07:11 +02002647 wq->first_flusher = NULL;
2648
Tejun Heo6183c002013-03-12 11:29:57 -07002649 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2650 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002651
2652 while (true) {
2653 struct wq_flusher *next, *tmp;
2654
2655 /* complete all the flushers sharing the current flush color */
2656 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2657 if (next->flush_color != wq->flush_color)
2658 break;
2659 list_del_init(&next->list);
2660 complete(&next->done);
2661 }
2662
Tejun Heo6183c002013-03-12 11:29:57 -07002663 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2664 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002665
2666 /* this flush_color is finished, advance by one */
2667 wq->flush_color = work_next_color(wq->flush_color);
2668
2669 /* one color has been freed, handle overflow queue */
2670 if (!list_empty(&wq->flusher_overflow)) {
2671 /*
2672 * Assign the same color to all overflowed
2673 * flushers, advance work_color and append to
2674 * flusher_queue. This is the start-to-wait
2675 * phase for these overflowed flushers.
2676 */
2677 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2678 tmp->flush_color = wq->work_color;
2679
2680 wq->work_color = work_next_color(wq->work_color);
2681
2682 list_splice_tail_init(&wq->flusher_overflow,
2683 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002684 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002685 }
2686
2687 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002688 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002689 break;
2690 }
2691
2692 /*
2693 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002694 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002695 */
Tejun Heo6183c002013-03-12 11:29:57 -07002696 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2697 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002698
2699 list_del_init(&next->list);
2700 wq->first_flusher = next;
2701
Tejun Heo112202d2013-02-13 19:29:12 -08002702 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002703 break;
2704
2705 /*
2706 * Meh... this color is already done, clear first
2707 * flusher and repeat cascading.
2708 */
2709 wq->first_flusher = NULL;
2710 }
2711
2712out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002713 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
Dave Jonesae90dd52006-06-30 01:40:45 -04002715EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002717/**
2718 * drain_workqueue - drain a workqueue
2719 * @wq: workqueue to drain
2720 *
2721 * Wait until the workqueue becomes empty. While draining is in progress,
2722 * only chain queueing is allowed. IOW, only currently pending or running
2723 * work items on @wq can queue further work items on it. @wq is flushed
2724 * repeatedly until it becomes empty. The number of flushing is detemined
2725 * by the depth of chaining and should be relatively short. Whine if it
2726 * takes too long.
2727 */
2728void drain_workqueue(struct workqueue_struct *wq)
2729{
2730 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002731 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002732
2733 /*
2734 * __queue_work() needs to test whether there are drainers, is much
2735 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002736 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002737 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002738 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002739 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002740 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002741 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002742reflush:
2743 flush_workqueue(wq);
2744
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002745 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002746
Tejun Heo49e3cf42013-03-12 11:29:58 -07002747 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002748 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002749
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002750 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002751 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002752 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002753
2754 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002755 continue;
2756
2757 if (++flush_cnt == 10 ||
2758 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002759 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002760 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002761
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002762 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002763 goto reflush;
2764 }
2765
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002766 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002767 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002768 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002769}
2770EXPORT_SYMBOL_GPL(drain_workqueue);
2771
Tejun Heo606a5022012-08-20 14:51:23 -07002772static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002773{
2774 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002775 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002776 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002777
2778 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002779
Tejun Heofa1b54e2013-03-12 11:30:00 -07002780 local_irq_disable();
2781 pool = get_work_pool(work);
2782 if (!pool) {
2783 local_irq_enable();
2784 return false;
2785 }
2786
2787 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002788 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002789 pwq = get_work_pwq(work);
2790 if (pwq) {
2791 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002792 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002793 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002794 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002795 if (!worker)
2796 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002797 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002798 }
Tejun Heobaf59022010-09-16 10:42:16 +02002799
Tejun Heo112202d2013-02-13 19:29:12 -08002800 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002801 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002802
Tejun Heoe159489b2011-01-09 23:32:15 +01002803 /*
2804 * If @max_active is 1 or rescuer is in use, flushing another work
2805 * item on the same workqueue may lead to deadlock. Make sure the
2806 * flusher is not running on the same workqueue by verifying write
2807 * access.
2808 */
Tejun Heo493008a2013-03-12 11:30:03 -07002809 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002810 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002811 else
Tejun Heo112202d2013-02-13 19:29:12 -08002812 lock_map_acquire_read(&pwq->wq->lockdep_map);
2813 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002814
Tejun Heobaf59022010-09-16 10:42:16 +02002815 return true;
2816already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002817 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002818 return false;
2819}
2820
Oleg Nesterovdb700892008-07-25 01:47:49 -07002821/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002822 * flush_work - wait for a work to finish executing the last queueing instance
2823 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002824 *
Tejun Heo606a5022012-08-20 14:51:23 -07002825 * Wait until @work has finished execution. @work is guaranteed to be idle
2826 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002827 *
2828 * RETURNS:
2829 * %true if flush_work() waited for the work to finish execution,
2830 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002831 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002832bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002833{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002834 struct wq_barrier barr;
2835
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002836 lock_map_acquire(&work->lockdep_map);
2837 lock_map_release(&work->lockdep_map);
2838
Tejun Heo606a5022012-08-20 14:51:23 -07002839 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002840 wait_for_completion(&barr.done);
2841 destroy_work_on_stack(&barr.work);
2842 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002843 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002844 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002845 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002846}
2847EXPORT_SYMBOL_GPL(flush_work);
2848
Tejun Heo36e227d2012-08-03 10:30:46 -07002849static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002850{
Tejun Heobbb68df2012-08-03 10:30:46 -07002851 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002852 int ret;
2853
2854 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002855 ret = try_to_grab_pending(work, is_dwork, &flags);
2856 /*
2857 * If someone else is canceling, wait for the same event it
2858 * would be waiting for before retrying.
2859 */
2860 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002861 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002862 } while (unlikely(ret < 0));
2863
Tejun Heobbb68df2012-08-03 10:30:46 -07002864 /* tell other tasks trying to grab @work to back off */
2865 mark_work_canceling(work);
2866 local_irq_restore(flags);
2867
Tejun Heo606a5022012-08-20 14:51:23 -07002868 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002869 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002870 return ret;
2871}
2872
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002873/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 * cancel_work_sync - cancel a work and wait for it to finish
2875 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002876 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002877 * Cancel @work and wait for its execution to finish. This function
2878 * can be used even if the work re-queues itself or migrates to
2879 * another workqueue. On return from this function, @work is
2880 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002881 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002882 * cancel_work_sync(&delayed_work->work) must not be used for
2883 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002884 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002885 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002886 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002887 *
2888 * RETURNS:
2889 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002890 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002891bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002892{
Tejun Heo36e227d2012-08-03 10:30:46 -07002893 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002894}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002895EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002896
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002897/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002898 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2899 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * Delayed timer is cancelled and the pending work is queued for
2902 * immediate execution. Like flush_work(), this function only
2903 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002904 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002905 * RETURNS:
2906 * %true if flush_work() waited for the work to finish execution,
2907 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002908 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002909bool flush_delayed_work(struct delayed_work *dwork)
2910{
Tejun Heo8930cab2012-08-03 10:30:45 -07002911 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002912 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002913 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002914 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002915 return flush_work(&dwork->work);
2916}
2917EXPORT_SYMBOL(flush_delayed_work);
2918
2919/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002920 * cancel_delayed_work - cancel a delayed work
2921 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002922 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002923 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2924 * and canceled; %false if wasn't pending. Note that the work callback
2925 * function may still be running on return, unless it returns %true and the
2926 * work doesn't re-arm itself. Explicitly flush or use
2927 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002928 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002929 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002930 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002931bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002932{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002933 unsigned long flags;
2934 int ret;
2935
2936 do {
2937 ret = try_to_grab_pending(&dwork->work, true, &flags);
2938 } while (unlikely(ret == -EAGAIN));
2939
2940 if (unlikely(ret < 0))
2941 return false;
2942
Tejun Heo7c3eed52013-01-24 11:01:33 -08002943 set_work_pool_and_clear_pending(&dwork->work,
2944 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002945 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002946 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002947}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002948EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002949
2950/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002951 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2952 * @dwork: the delayed work cancel
2953 *
2954 * This is cancel_work_sync() for delayed works.
2955 *
2956 * RETURNS:
2957 * %true if @dwork was pending, %false otherwise.
2958 */
2959bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002960{
Tejun Heo36e227d2012-08-03 10:30:46 -07002961 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002962}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002963EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002965/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002966 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002967 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002968 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002969 * schedule_on_each_cpu() executes @func on each online CPU using the
2970 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002971 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002972 *
2973 * RETURNS:
2974 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002975 */
David Howells65f27f32006-11-22 14:55:48 +00002976int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba82006-01-08 01:00:43 -08002977{
2978 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002979 struct work_struct __percpu *works;
Christoph Lameter15316ba82006-01-08 01:00:43 -08002980
Andrew Mortonb6136772006-06-25 05:47:49 -07002981 works = alloc_percpu(struct work_struct);
2982 if (!works)
Christoph Lameter15316ba82006-01-08 01:00:43 -08002983 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002984
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002985 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002986
Christoph Lameter15316ba82006-01-08 01:00:43 -08002987 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002988 struct work_struct *work = per_cpu_ptr(works, cpu);
2989
2990 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002991 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002992 }
Tejun Heo93981802009-11-17 14:06:20 -08002993
2994 for_each_online_cpu(cpu)
2995 flush_work(per_cpu_ptr(works, cpu));
2996
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002997 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002998 free_percpu(works);
Christoph Lameter15316ba82006-01-08 01:00:43 -08002999 return 0;
3000}
3001
Alan Sterneef6a7d2010-02-12 17:39:21 +09003002/**
3003 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3004 *
3005 * Forces execution of the kernel-global workqueue and blocks until its
3006 * completion.
3007 *
3008 * Think twice before calling this function! It's very easy to get into
3009 * trouble if you don't take great care. Either of the following situations
3010 * will lead to deadlock:
3011 *
3012 * One of the work items currently on the workqueue needs to acquire
3013 * a lock held by your code or its caller.
3014 *
3015 * Your code is running in the context of a work routine.
3016 *
3017 * They will be detected by lockdep when they occur, but the first might not
3018 * occur very often. It depends on what work items are on the workqueue and
3019 * what locks they need, which you have no control over.
3020 *
3021 * In most situations flushing the entire workqueue is overkill; you merely
3022 * need to know that a particular work item isn't queued and isn't running.
3023 * In such cases you should use cancel_delayed_work_sync() or
3024 * cancel_work_sync() instead.
3025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026void flush_scheduled_work(void)
3027{
Tejun Heod320c032010-06-29 10:07:14 +02003028 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
Dave Jonesae90dd52006-06-30 01:40:45 -04003030EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003033 * execute_in_process_context - reliably execute the routine with user context
3034 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003035 * @ew: guaranteed storage for the execute work structure (must
3036 * be available when the work executes)
3037 *
3038 * Executes the function immediately if process context is available,
3039 * otherwise schedules the function for delayed execution.
3040 *
3041 * Returns: 0 - function was executed
3042 * 1 - function was scheduled for execution
3043 */
David Howells65f27f32006-11-22 14:55:48 +00003044int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003045{
3046 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003047 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003048 return 0;
3049 }
3050
David Howells65f27f32006-11-22 14:55:48 +00003051 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003052 schedule_work(&ew->work);
3053
3054 return 1;
3055}
3056EXPORT_SYMBOL_GPL(execute_in_process_context);
3057
Tejun Heo226223a2013-03-12 11:30:05 -07003058#ifdef CONFIG_SYSFS
3059/*
3060 * Workqueues with WQ_SYSFS flag set is visible to userland via
3061 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3062 * following attributes.
3063 *
3064 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3065 * max_active RW int : maximum number of in-flight work items
3066 *
3067 * Unbound workqueues have the following extra attributes.
3068 *
3069 * id RO int : the associated pool ID
3070 * nice RW int : nice value of the workers
3071 * cpumask RW mask : bitmask of allowed CPUs for the workers
3072 */
3073struct wq_device {
3074 struct workqueue_struct *wq;
3075 struct device dev;
3076};
3077
3078static struct workqueue_struct *dev_to_wq(struct device *dev)
3079{
3080 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3081
3082 return wq_dev->wq;
3083}
3084
3085static ssize_t wq_per_cpu_show(struct device *dev,
3086 struct device_attribute *attr, char *buf)
3087{
3088 struct workqueue_struct *wq = dev_to_wq(dev);
3089
3090 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3091}
3092
3093static ssize_t wq_max_active_show(struct device *dev,
3094 struct device_attribute *attr, char *buf)
3095{
3096 struct workqueue_struct *wq = dev_to_wq(dev);
3097
3098 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3099}
3100
3101static ssize_t wq_max_active_store(struct device *dev,
3102 struct device_attribute *attr,
3103 const char *buf, size_t count)
3104{
3105 struct workqueue_struct *wq = dev_to_wq(dev);
3106 int val;
3107
3108 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3109 return -EINVAL;
3110
3111 workqueue_set_max_active(wq, val);
3112 return count;
3113}
3114
3115static struct device_attribute wq_sysfs_attrs[] = {
3116 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3117 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3118 __ATTR_NULL,
3119};
3120
Tejun Heod55262c2013-04-01 11:23:38 -07003121static ssize_t wq_pool_ids_show(struct device *dev,
3122 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003123{
3124 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003125 const char *delim = "";
3126 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003127
3128 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003129 for_each_node(node) {
3130 written += scnprintf(buf + written, PAGE_SIZE - written,
3131 "%s%d:%d", delim, node,
3132 unbound_pwq_by_node(wq, node)->pool->id);
3133 delim = " ";
3134 }
3135 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003136 rcu_read_unlock_sched();
3137
3138 return written;
3139}
3140
3141static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3142 char *buf)
3143{
3144 struct workqueue_struct *wq = dev_to_wq(dev);
3145 int written;
3146
Tejun Heo6029a912013-04-01 11:23:34 -07003147 mutex_lock(&wq->mutex);
3148 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3149 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003150
3151 return written;
3152}
3153
3154/* prepare workqueue_attrs for sysfs store operations */
3155static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3156{
3157 struct workqueue_attrs *attrs;
3158
3159 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3160 if (!attrs)
3161 return NULL;
3162
Tejun Heo6029a912013-04-01 11:23:34 -07003163 mutex_lock(&wq->mutex);
3164 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3165 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003166 return attrs;
3167}
3168
3169static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3170 const char *buf, size_t count)
3171{
3172 struct workqueue_struct *wq = dev_to_wq(dev);
3173 struct workqueue_attrs *attrs;
3174 int ret;
3175
3176 attrs = wq_sysfs_prep_attrs(wq);
3177 if (!attrs)
3178 return -ENOMEM;
3179
3180 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3181 attrs->nice >= -20 && attrs->nice <= 19)
3182 ret = apply_workqueue_attrs(wq, attrs);
3183 else
3184 ret = -EINVAL;
3185
3186 free_workqueue_attrs(attrs);
3187 return ret ?: count;
3188}
3189
3190static ssize_t wq_cpumask_show(struct device *dev,
3191 struct device_attribute *attr, char *buf)
3192{
3193 struct workqueue_struct *wq = dev_to_wq(dev);
3194 int written;
3195
Tejun Heo6029a912013-04-01 11:23:34 -07003196 mutex_lock(&wq->mutex);
3197 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3198 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003199
3200 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3201 return written;
3202}
3203
3204static ssize_t wq_cpumask_store(struct device *dev,
3205 struct device_attribute *attr,
3206 const char *buf, size_t count)
3207{
3208 struct workqueue_struct *wq = dev_to_wq(dev);
3209 struct workqueue_attrs *attrs;
3210 int ret;
3211
3212 attrs = wq_sysfs_prep_attrs(wq);
3213 if (!attrs)
3214 return -ENOMEM;
3215
3216 ret = cpumask_parse(buf, attrs->cpumask);
3217 if (!ret)
3218 ret = apply_workqueue_attrs(wq, attrs);
3219
3220 free_workqueue_attrs(attrs);
3221 return ret ?: count;
3222}
3223
Tejun Heod55262c2013-04-01 11:23:38 -07003224static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3225 char *buf)
3226{
3227 struct workqueue_struct *wq = dev_to_wq(dev);
3228 int written;
3229
3230 mutex_lock(&wq->mutex);
3231 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3232 !wq->unbound_attrs->no_numa);
3233 mutex_unlock(&wq->mutex);
3234
3235 return written;
3236}
3237
3238static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3239 const char *buf, size_t count)
3240{
3241 struct workqueue_struct *wq = dev_to_wq(dev);
3242 struct workqueue_attrs *attrs;
3243 int v, ret;
3244
3245 attrs = wq_sysfs_prep_attrs(wq);
3246 if (!attrs)
3247 return -ENOMEM;
3248
3249 ret = -EINVAL;
3250 if (sscanf(buf, "%d", &v) == 1) {
3251 attrs->no_numa = !v;
3252 ret = apply_workqueue_attrs(wq, attrs);
3253 }
3254
3255 free_workqueue_attrs(attrs);
3256 return ret ?: count;
3257}
3258
Tejun Heo226223a2013-03-12 11:30:05 -07003259static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003260 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003261 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3262 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003263 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003264 __ATTR_NULL,
3265};
3266
3267static struct bus_type wq_subsys = {
3268 .name = "workqueue",
3269 .dev_attrs = wq_sysfs_attrs,
3270};
3271
3272static int __init wq_sysfs_init(void)
3273{
3274 return subsys_virtual_register(&wq_subsys, NULL);
3275}
3276core_initcall(wq_sysfs_init);
3277
3278static void wq_device_release(struct device *dev)
3279{
3280 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3281
3282 kfree(wq_dev);
3283}
3284
3285/**
3286 * workqueue_sysfs_register - make a workqueue visible in sysfs
3287 * @wq: the workqueue to register
3288 *
3289 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3290 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3291 * which is the preferred method.
3292 *
3293 * Workqueue user should use this function directly iff it wants to apply
3294 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3295 * apply_workqueue_attrs() may race against userland updating the
3296 * attributes.
3297 *
3298 * Returns 0 on success, -errno on failure.
3299 */
3300int workqueue_sysfs_register(struct workqueue_struct *wq)
3301{
3302 struct wq_device *wq_dev;
3303 int ret;
3304
3305 /*
3306 * Adjusting max_active or creating new pwqs by applyting
3307 * attributes breaks ordering guarantee. Disallow exposing ordered
3308 * workqueues.
3309 */
3310 if (WARN_ON(wq->flags & __WQ_ORDERED))
3311 return -EINVAL;
3312
3313 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3314 if (!wq_dev)
3315 return -ENOMEM;
3316
3317 wq_dev->wq = wq;
3318 wq_dev->dev.bus = &wq_subsys;
3319 wq_dev->dev.init_name = wq->name;
3320 wq_dev->dev.release = wq_device_release;
3321
3322 /*
3323 * unbound_attrs are created separately. Suppress uevent until
3324 * everything is ready.
3325 */
3326 dev_set_uevent_suppress(&wq_dev->dev, true);
3327
3328 ret = device_register(&wq_dev->dev);
3329 if (ret) {
3330 kfree(wq_dev);
3331 wq->wq_dev = NULL;
3332 return ret;
3333 }
3334
3335 if (wq->flags & WQ_UNBOUND) {
3336 struct device_attribute *attr;
3337
3338 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3339 ret = device_create_file(&wq_dev->dev, attr);
3340 if (ret) {
3341 device_unregister(&wq_dev->dev);
3342 wq->wq_dev = NULL;
3343 return ret;
3344 }
3345 }
3346 }
3347
3348 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3349 return 0;
3350}
3351
3352/**
3353 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3354 * @wq: the workqueue to unregister
3355 *
3356 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3357 */
3358static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3359{
3360 struct wq_device *wq_dev = wq->wq_dev;
3361
3362 if (!wq->wq_dev)
3363 return;
3364
3365 wq->wq_dev = NULL;
3366 device_unregister(&wq_dev->dev);
3367}
3368#else /* CONFIG_SYSFS */
3369static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3370#endif /* CONFIG_SYSFS */
3371
Tejun Heo7a4e3442013-03-12 11:30:00 -07003372/**
3373 * free_workqueue_attrs - free a workqueue_attrs
3374 * @attrs: workqueue_attrs to free
3375 *
3376 * Undo alloc_workqueue_attrs().
3377 */
3378void free_workqueue_attrs(struct workqueue_attrs *attrs)
3379{
3380 if (attrs) {
3381 free_cpumask_var(attrs->cpumask);
3382 kfree(attrs);
3383 }
3384}
3385
3386/**
3387 * alloc_workqueue_attrs - allocate a workqueue_attrs
3388 * @gfp_mask: allocation mask to use
3389 *
3390 * Allocate a new workqueue_attrs, initialize with default settings and
3391 * return it. Returns NULL on failure.
3392 */
3393struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3394{
3395 struct workqueue_attrs *attrs;
3396
3397 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3398 if (!attrs)
3399 goto fail;
3400 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3401 goto fail;
3402
Tejun Heo13e2e552013-04-01 11:23:31 -07003403 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003404 return attrs;
3405fail:
3406 free_workqueue_attrs(attrs);
3407 return NULL;
3408}
3409
Tejun Heo29c91e92013-03-12 11:30:03 -07003410static void copy_workqueue_attrs(struct workqueue_attrs *to,
3411 const struct workqueue_attrs *from)
3412{
3413 to->nice = from->nice;
3414 cpumask_copy(to->cpumask, from->cpumask);
3415}
3416
Tejun Heo29c91e92013-03-12 11:30:03 -07003417/* hash value of the content of @attr */
3418static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3419{
3420 u32 hash = 0;
3421
3422 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003423 hash = jhash(cpumask_bits(attrs->cpumask),
3424 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003425 return hash;
3426}
3427
3428/* content equality test */
3429static bool wqattrs_equal(const struct workqueue_attrs *a,
3430 const struct workqueue_attrs *b)
3431{
3432 if (a->nice != b->nice)
3433 return false;
3434 if (!cpumask_equal(a->cpumask, b->cpumask))
3435 return false;
3436 return true;
3437}
3438
Tejun Heo7a4e3442013-03-12 11:30:00 -07003439/**
3440 * init_worker_pool - initialize a newly zalloc'd worker_pool
3441 * @pool: worker_pool to initialize
3442 *
3443 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003444 * Returns 0 on success, -errno on failure. Even on failure, all fields
3445 * inside @pool proper are initialized and put_unbound_pool() can be called
3446 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003447 */
3448static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003449{
3450 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003451 pool->id = -1;
3452 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003453 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003454 pool->flags |= POOL_DISASSOCIATED;
3455 INIT_LIST_HEAD(&pool->worklist);
3456 INIT_LIST_HEAD(&pool->idle_list);
3457 hash_init(pool->busy_hash);
3458
3459 init_timer_deferrable(&pool->idle_timer);
3460 pool->idle_timer.function = idle_worker_timeout;
3461 pool->idle_timer.data = (unsigned long)pool;
3462
3463 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3464 (unsigned long)pool);
3465
3466 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003467 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003468 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003469
Tejun Heo29c91e92013-03-12 11:30:03 -07003470 INIT_HLIST_NODE(&pool->hash_node);
3471 pool->refcnt = 1;
3472
3473 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003474 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3475 if (!pool->attrs)
3476 return -ENOMEM;
3477 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003478}
3479
Tejun Heo29c91e92013-03-12 11:30:03 -07003480static void rcu_free_pool(struct rcu_head *rcu)
3481{
3482 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3483
Tejun Heo822d8402013-03-19 13:45:21 -07003484 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003485 free_workqueue_attrs(pool->attrs);
3486 kfree(pool);
3487}
3488
3489/**
3490 * put_unbound_pool - put a worker_pool
3491 * @pool: worker_pool to put
3492 *
3493 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003494 * safe manner. get_unbound_pool() calls this function on its failure path
3495 * and this function should be able to release pools which went through,
3496 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003497 *
3498 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003499 */
3500static void put_unbound_pool(struct worker_pool *pool)
3501{
3502 struct worker *worker;
3503
Tejun Heoa892cac2013-04-01 11:23:32 -07003504 lockdep_assert_held(&wq_pool_mutex);
3505
3506 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003507 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003508
3509 /* sanity checks */
3510 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003511 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003512 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003513
3514 /* release id and unhash */
3515 if (pool->id >= 0)
3516 idr_remove(&worker_pool_idr, pool->id);
3517 hash_del(&pool->hash_node);
3518
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003519 /*
3520 * Become the manager and destroy all workers. Grabbing
3521 * manager_arb prevents @pool's workers from blocking on
3522 * manager_mutex.
3523 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003524 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003525 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003526 spin_lock_irq(&pool->lock);
3527
3528 while ((worker = first_worker(pool)))
3529 destroy_worker(worker);
3530 WARN_ON(pool->nr_workers || pool->nr_idle);
3531
3532 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003533 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003534 mutex_unlock(&pool->manager_arb);
3535
3536 /* shut down the timers */
3537 del_timer_sync(&pool->idle_timer);
3538 del_timer_sync(&pool->mayday_timer);
3539
3540 /* sched-RCU protected to allow dereferences from get_work_pool() */
3541 call_rcu_sched(&pool->rcu, rcu_free_pool);
3542}
3543
3544/**
3545 * get_unbound_pool - get a worker_pool with the specified attributes
3546 * @attrs: the attributes of the worker_pool to get
3547 *
3548 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3549 * reference count and return it. If there already is a matching
3550 * worker_pool, it will be used; otherwise, this function attempts to
3551 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003552 *
3553 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003554 */
3555static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3556{
Tejun Heo29c91e92013-03-12 11:30:03 -07003557 u32 hash = wqattrs_hash(attrs);
3558 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003559 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003560
Tejun Heoa892cac2013-04-01 11:23:32 -07003561 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003562
3563 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003564 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3565 if (wqattrs_equal(pool->attrs, attrs)) {
3566 pool->refcnt++;
3567 goto out_unlock;
3568 }
3569 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003570
3571 /* nope, create a new one */
3572 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3573 if (!pool || init_worker_pool(pool) < 0)
3574 goto fail;
3575
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003576 if (workqueue_freezing)
3577 pool->flags |= POOL_FREEZING;
3578
Tejun Heo8864b4e2013-03-12 11:30:04 -07003579 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003580 copy_workqueue_attrs(pool->attrs, attrs);
3581
Tejun Heof3f90ad2013-04-01 11:23:34 -07003582 /* if cpumask is contained inside a NUMA node, we belong to that node */
3583 if (wq_numa_enabled) {
3584 for_each_node(node) {
3585 if (cpumask_subset(pool->attrs->cpumask,
3586 wq_numa_possible_cpumask[node])) {
3587 pool->node = node;
3588 break;
3589 }
3590 }
3591 }
3592
Tejun Heo29c91e92013-03-12 11:30:03 -07003593 if (worker_pool_assign_id(pool) < 0)
3594 goto fail;
3595
3596 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003597 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003598 goto fail;
3599
Tejun Heo29c91e92013-03-12 11:30:03 -07003600 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003601 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3602out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003603 return pool;
3604fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003605 if (pool)
3606 put_unbound_pool(pool);
3607 return NULL;
3608}
3609
Tejun Heo8864b4e2013-03-12 11:30:04 -07003610static void rcu_free_pwq(struct rcu_head *rcu)
3611{
3612 kmem_cache_free(pwq_cache,
3613 container_of(rcu, struct pool_workqueue, rcu));
3614}
3615
3616/*
3617 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3618 * and needs to be destroyed.
3619 */
3620static void pwq_unbound_release_workfn(struct work_struct *work)
3621{
3622 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3623 unbound_release_work);
3624 struct workqueue_struct *wq = pwq->wq;
3625 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003626 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003627
3628 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3629 return;
3630
Tejun Heo75ccf592013-03-12 11:30:04 -07003631 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003632 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003633 * necessary on release but do it anyway. It's easier to verify
3634 * and consistent with the linking path.
3635 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003636 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003637 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003638 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003639 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003640
Tejun Heoa892cac2013-04-01 11:23:32 -07003641 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003642 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003643 mutex_unlock(&wq_pool_mutex);
3644
Tejun Heo8864b4e2013-03-12 11:30:04 -07003645 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3646
3647 /*
3648 * If we're the last pwq going away, @wq is already dead and no one
3649 * is gonna access it anymore. Free it.
3650 */
Tejun Heo6029a912013-04-01 11:23:34 -07003651 if (is_last) {
3652 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003653 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003654 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003655}
3656
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003657/**
Tejun Heo699ce092013-03-13 16:51:35 -07003658 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003659 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003660 *
Tejun Heo699ce092013-03-13 16:51:35 -07003661 * If @pwq isn't freezing, set @pwq->max_active to the associated
3662 * workqueue's saved_max_active and activate delayed work items
3663 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003664 */
Tejun Heo699ce092013-03-13 16:51:35 -07003665static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003666{
Tejun Heo699ce092013-03-13 16:51:35 -07003667 struct workqueue_struct *wq = pwq->wq;
3668 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003669
Tejun Heo699ce092013-03-13 16:51:35 -07003670 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003671 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003672
3673 /* fast exit for non-freezable wqs */
3674 if (!freezable && pwq->max_active == wq->saved_max_active)
3675 return;
3676
Lai Jiangshana357fc02013-03-25 16:57:19 -07003677 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003678
3679 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3680 pwq->max_active = wq->saved_max_active;
3681
3682 while (!list_empty(&pwq->delayed_works) &&
3683 pwq->nr_active < pwq->max_active)
3684 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003685
3686 /*
3687 * Need to kick a worker after thawed or an unbound wq's
3688 * max_active is bumped. It's a slow path. Do it always.
3689 */
3690 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003691 } else {
3692 pwq->max_active = 0;
3693 }
3694
Lai Jiangshana357fc02013-03-25 16:57:19 -07003695 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003696}
3697
Tejun Heoe50aba92013-04-01 11:23:35 -07003698/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003699static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3700 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003701{
3702 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3703
Tejun Heoe50aba92013-04-01 11:23:35 -07003704 memset(pwq, 0, sizeof(*pwq));
3705
Tejun Heod2c1d402013-03-12 11:30:04 -07003706 pwq->pool = pool;
3707 pwq->wq = wq;
3708 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003709 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003710 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003711 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003712 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003713 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003714}
Tejun Heod2c1d402013-03-12 11:30:04 -07003715
Tejun Heof147f292013-04-01 11:23:35 -07003716/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003717static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003718{
3719 struct workqueue_struct *wq = pwq->wq;
3720
3721 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003722
Tejun Heo1befcf32013-04-01 11:23:35 -07003723 /* may be called multiple times, ignore if already linked */
3724 if (!list_empty(&pwq->pwqs_node))
3725 return;
3726
Tejun Heo983ca252013-03-13 16:51:35 -07003727 /*
3728 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003729 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003730 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003731 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003732
3733 /* sync max_active to the current setting */
3734 pwq_adjust_max_active(pwq);
3735
3736 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003737 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003738}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003739
Tejun Heof147f292013-04-01 11:23:35 -07003740/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3741static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3742 const struct workqueue_attrs *attrs)
3743{
3744 struct worker_pool *pool;
3745 struct pool_workqueue *pwq;
3746
3747 lockdep_assert_held(&wq_pool_mutex);
3748
3749 pool = get_unbound_pool(attrs);
3750 if (!pool)
3751 return NULL;
3752
Tejun Heoe50aba92013-04-01 11:23:35 -07003753 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003754 if (!pwq) {
3755 put_unbound_pool(pool);
3756 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003757 }
Tejun Heo6029a912013-04-01 11:23:34 -07003758
Tejun Heof147f292013-04-01 11:23:35 -07003759 init_pwq(pwq, wq, pool);
3760 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003761}
3762
Tejun Heo4c16bd32013-04-01 11:23:36 -07003763/* undo alloc_unbound_pwq(), used only in the error path */
3764static void free_unbound_pwq(struct pool_workqueue *pwq)
3765{
3766 lockdep_assert_held(&wq_pool_mutex);
3767
3768 if (pwq) {
3769 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003770 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003771 }
3772}
3773
3774/**
3775 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3776 * @attrs: the wq_attrs of interest
3777 * @node: the target NUMA node
3778 * @cpu_going_down: if >= 0, the CPU to consider as offline
3779 * @cpumask: outarg, the resulting cpumask
3780 *
3781 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3782 * @cpu_going_down is >= 0, that cpu is considered offline during
3783 * calculation. The result is stored in @cpumask. This function returns
3784 * %true if the resulting @cpumask is different from @attrs->cpumask,
3785 * %false if equal.
3786 *
3787 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3788 * enabled and @node has online CPUs requested by @attrs, the returned
3789 * cpumask is the intersection of the possible CPUs of @node and
3790 * @attrs->cpumask.
3791 *
3792 * The caller is responsible for ensuring that the cpumask of @node stays
3793 * stable.
3794 */
3795static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3796 int cpu_going_down, cpumask_t *cpumask)
3797{
Tejun Heod55262c2013-04-01 11:23:38 -07003798 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003799 goto use_dfl;
3800
3801 /* does @node have any online CPUs @attrs wants? */
3802 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3803 if (cpu_going_down >= 0)
3804 cpumask_clear_cpu(cpu_going_down, cpumask);
3805
3806 if (cpumask_empty(cpumask))
3807 goto use_dfl;
3808
3809 /* yeap, return possible CPUs in @node that @attrs wants */
3810 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3811 return !cpumask_equal(cpumask, attrs->cpumask);
3812
3813use_dfl:
3814 cpumask_copy(cpumask, attrs->cpumask);
3815 return false;
3816}
3817
Tejun Heo1befcf32013-04-01 11:23:35 -07003818/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3819static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3820 int node,
3821 struct pool_workqueue *pwq)
3822{
3823 struct pool_workqueue *old_pwq;
3824
3825 lockdep_assert_held(&wq->mutex);
3826
3827 /* link_pwq() can handle duplicate calls */
3828 link_pwq(pwq);
3829
3830 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3831 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3832 return old_pwq;
3833}
3834
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003835/**
3836 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3837 * @wq: the target workqueue
3838 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3839 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003840 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3841 * machines, this function maps a separate pwq to each NUMA node with
3842 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3843 * NUMA node it was issued on. Older pwqs are released as in-flight work
3844 * items finish. Note that a work item which repeatedly requeues itself
3845 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003846 *
3847 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3848 * failure.
3849 */
3850int apply_workqueue_attrs(struct workqueue_struct *wq,
3851 const struct workqueue_attrs *attrs)
3852{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003853 struct workqueue_attrs *new_attrs, *tmp_attrs;
3854 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003855 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003856
Tejun Heo8719dce2013-03-12 11:30:04 -07003857 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003858 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3859 return -EINVAL;
3860
Tejun Heo8719dce2013-03-12 11:30:04 -07003861 /* creating multiple pwqs breaks ordering guarantee */
3862 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3863 return -EINVAL;
3864
Tejun Heo4c16bd32013-04-01 11:23:36 -07003865 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003866 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003867 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3868 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003869 goto enomem;
3870
Tejun Heo4c16bd32013-04-01 11:23:36 -07003871 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003872 copy_workqueue_attrs(new_attrs, attrs);
3873 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3874
Tejun Heo4c16bd32013-04-01 11:23:36 -07003875 /*
3876 * We may create multiple pwqs with differing cpumasks. Make a
3877 * copy of @new_attrs which will be modified and used to obtain
3878 * pools.
3879 */
3880 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003881
Tejun Heo4c16bd32013-04-01 11:23:36 -07003882 /*
3883 * CPUs should stay stable across pwq creations and installations.
3884 * Pin CPUs, determine the target cpumask for each node and create
3885 * pwqs accordingly.
3886 */
3887 get_online_cpus();
3888
3889 mutex_lock(&wq_pool_mutex);
3890
3891 /*
3892 * If something goes wrong during CPU up/down, we'll fall back to
3893 * the default pwq covering whole @attrs->cpumask. Always create
3894 * it even if we don't use it immediately.
3895 */
3896 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3897 if (!dfl_pwq)
3898 goto enomem_pwq;
3899
3900 for_each_node(node) {
3901 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3902 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3903 if (!pwq_tbl[node])
3904 goto enomem_pwq;
3905 } else {
3906 dfl_pwq->refcnt++;
3907 pwq_tbl[node] = dfl_pwq;
3908 }
3909 }
3910
3911 mutex_unlock(&wq_pool_mutex);
3912
3913 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003914 mutex_lock(&wq->mutex);
3915
Tejun Heof147f292013-04-01 11:23:35 -07003916 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003917
3918 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003919 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003920 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3921
3922 /* @dfl_pwq might not have been used, ensure it's linked */
3923 link_pwq(dfl_pwq);
3924 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003925
3926 mutex_unlock(&wq->mutex);
3927
Tejun Heo4c16bd32013-04-01 11:23:36 -07003928 /* put the old pwqs */
3929 for_each_node(node)
3930 put_pwq_unlocked(pwq_tbl[node]);
3931 put_pwq_unlocked(dfl_pwq);
3932
3933 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003934 ret = 0;
3935 /* fall through */
3936out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003937 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003938 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003939 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003940 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003941
Tejun Heo4c16bd32013-04-01 11:23:36 -07003942enomem_pwq:
3943 free_unbound_pwq(dfl_pwq);
3944 for_each_node(node)
3945 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3946 free_unbound_pwq(pwq_tbl[node]);
3947 mutex_unlock(&wq_pool_mutex);
3948 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003949enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003950 ret = -ENOMEM;
3951 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003952}
3953
Tejun Heo4c16bd32013-04-01 11:23:36 -07003954/**
3955 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3956 * @wq: the target workqueue
3957 * @cpu: the CPU coming up or going down
3958 * @online: whether @cpu is coming up or going down
3959 *
3960 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3961 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3962 * @wq accordingly.
3963 *
3964 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3965 * falls back to @wq->dfl_pwq which may not be optimal but is always
3966 * correct.
3967 *
3968 * Note that when the last allowed CPU of a NUMA node goes offline for a
3969 * workqueue with a cpumask spanning multiple nodes, the workers which were
3970 * already executing the work items for the workqueue will lose their CPU
3971 * affinity and may execute on any CPU. This is similar to how per-cpu
3972 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3973 * affinity, it's the user's responsibility to flush the work item from
3974 * CPU_DOWN_PREPARE.
3975 */
3976static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3977 bool online)
3978{
3979 int node = cpu_to_node(cpu);
3980 int cpu_off = online ? -1 : cpu;
3981 struct pool_workqueue *old_pwq = NULL, *pwq;
3982 struct workqueue_attrs *target_attrs;
3983 cpumask_t *cpumask;
3984
3985 lockdep_assert_held(&wq_pool_mutex);
3986
3987 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3988 return;
3989
3990 /*
3991 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3992 * Let's use a preallocated one. The following buf is protected by
3993 * CPU hotplug exclusion.
3994 */
3995 target_attrs = wq_update_unbound_numa_attrs_buf;
3996 cpumask = target_attrs->cpumask;
3997
3998 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07003999 if (wq->unbound_attrs->no_numa)
4000 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004001
4002 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
4003 pwq = unbound_pwq_by_node(wq, node);
4004
4005 /*
4006 * Let's determine what needs to be done. If the target cpumask is
4007 * different from wq's, we need to compare it to @pwq's and create
4008 * a new one if they don't match. If the target cpumask equals
4009 * wq's, the default pwq should be used. If @pwq is already the
4010 * default one, nothing to do; otherwise, install the default one.
4011 */
4012 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
4013 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4014 goto out_unlock;
4015 } else {
4016 if (pwq == wq->dfl_pwq)
4017 goto out_unlock;
4018 else
4019 goto use_dfl_pwq;
4020 }
4021
4022 mutex_unlock(&wq->mutex);
4023
4024 /* create a new pwq */
4025 pwq = alloc_unbound_pwq(wq, target_attrs);
4026 if (!pwq) {
4027 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4028 wq->name);
4029 goto out_unlock;
4030 }
4031
4032 /*
4033 * Install the new pwq. As this function is called only from CPU
4034 * hotplug callbacks and applying a new attrs is wrapped with
4035 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4036 * inbetween.
4037 */
4038 mutex_lock(&wq->mutex);
4039 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4040 goto out_unlock;
4041
4042use_dfl_pwq:
4043 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4044 get_pwq(wq->dfl_pwq);
4045 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4046 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4047out_unlock:
4048 mutex_unlock(&wq->mutex);
4049 put_pwq_unlocked(old_pwq);
4050}
4051
Tejun Heo30cdf2492013-03-12 11:29:57 -07004052static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004054 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf2492013-03-12 11:29:57 -07004055 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004056
Tejun Heo30cdf2492013-03-12 11:29:57 -07004057 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004058 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4059 if (!wq->cpu_pwqs)
Tejun Heo30cdf2492013-03-12 11:29:57 -07004060 return -ENOMEM;
4061
4062 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004063 struct pool_workqueue *pwq =
4064 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004065 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004066 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf2492013-03-12 11:29:57 -07004067
Tejun Heof147f292013-04-01 11:23:35 -07004068 init_pwq(pwq, wq, &cpu_pools[highpri]);
4069
4070 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004071 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004072 mutex_unlock(&wq->mutex);
Tejun Heo30cdf2492013-03-12 11:29:57 -07004073 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004074 return 0;
Tejun Heo30cdf2492013-03-12 11:29:57 -07004075 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004076 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf2492013-03-12 11:29:57 -07004077 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004078}
4079
Tejun Heof3421792010-07-02 10:03:51 +02004080static int wq_clamp_max_active(int max_active, unsigned int flags,
4081 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004082{
Tejun Heof3421792010-07-02 10:03:51 +02004083 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4084
4085 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004086 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4087 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004088
Tejun Heof3421792010-07-02 10:03:51 +02004089 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004090}
4091
Tejun Heob196be82012-01-10 15:11:35 -08004092struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004093 unsigned int flags,
4094 int max_active,
4095 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004096 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004097{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004098 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004099 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004100 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004101 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004102
Viresh Kumarcee22a12013-04-08 16:45:40 +05304103 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4104 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4105 flags |= WQ_UNBOUND;
4106
Tejun Heoecf68812013-04-01 11:23:34 -07004107 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004108 if (flags & WQ_UNBOUND)
4109 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4110
4111 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004112 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004113 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004114
Tejun Heo6029a912013-04-01 11:23:34 -07004115 if (flags & WQ_UNBOUND) {
4116 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4117 if (!wq->unbound_attrs)
4118 goto err_free_wq;
4119 }
4120
Tejun Heoecf68812013-04-01 11:23:34 -07004121 va_start(args, lock_name);
4122 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004123 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004124
Tejun Heod320c032010-06-29 10:07:14 +02004125 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004126 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004127
Tejun Heob196be82012-01-10 15:11:35 -08004128 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004129 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004130 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004131 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004132 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf2492013-03-12 11:29:57 -07004133 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004134 INIT_LIST_HEAD(&wq->flusher_queue);
4135 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004136 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004137
Johannes Bergeb13ba82008-01-16 09:51:58 +01004138 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004139 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004140
Tejun Heo30cdf2492013-03-12 11:29:57 -07004141 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004142 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004143
Tejun Heo493008a2013-03-12 11:30:03 -07004144 /*
4145 * Workqueues which may be used during memory reclaim should
4146 * have a rescuer to guarantee forward progress.
4147 */
4148 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004149 struct worker *rescuer;
4150
Tejun Heod2c1d402013-03-12 11:30:04 -07004151 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004152 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004153 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004154
Tejun Heo111c2252013-01-17 17:16:24 -08004155 rescuer->rescue_wq = wq;
4156 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004157 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004158 if (IS_ERR(rescuer->task)) {
4159 kfree(rescuer);
4160 goto err_destroy;
4161 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004162
Tejun Heod2c1d402013-03-12 11:30:04 -07004163 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004164 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004165 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004166 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004167
Tejun Heo226223a2013-03-12 11:30:05 -07004168 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4169 goto err_destroy;
4170
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004171 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004172 * wq_pool_mutex protects global freeze state and workqueues list.
4173 * Grab it, adjust max_active and add the new @wq to workqueues
4174 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004175 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004176 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004177
Lai Jiangshana357fc02013-03-25 16:57:19 -07004178 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004179 for_each_pwq(pwq, wq)
4180 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004181 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004182
Tejun Heo15376632010-06-29 10:07:11 +02004183 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004184
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004185 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004186
Oleg Nesterov3af244332007-05-09 02:34:09 -07004187 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004188
4189err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004190 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004191 kfree(wq);
4192 return NULL;
4193err_destroy:
4194 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004195 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004196}
Tejun Heod320c032010-06-29 10:07:14 +02004197EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004198
4199/**
4200 * destroy_workqueue - safely terminate a workqueue
4201 * @wq: target workqueue
4202 *
4203 * Safely destroy a workqueue. All work currently pending will be done first.
4204 */
4205void destroy_workqueue(struct workqueue_struct *wq)
4206{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004207 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004208 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004209
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004210 /* drain it before proceeding with destruction */
4211 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004212
Tejun Heo6183c002013-03-12 11:29:57 -07004213 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004214 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004215 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004216 int i;
4217
Tejun Heo76af4d92013-03-12 11:30:00 -07004218 for (i = 0; i < WORK_NR_COLORS; i++) {
4219 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004220 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004221 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004222 }
4223 }
4224
Lai Jiangshan5c529592013-04-04 10:05:38 +08004225 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004226 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004227 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004228 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004229 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004230 }
Tejun Heo6183c002013-03-12 11:29:57 -07004231 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004232 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004233
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004234 /*
4235 * wq list is used to freeze wq, remove from list after
4236 * flushing is complete in case freeze races us.
4237 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004238 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004239 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004240 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004241
Tejun Heo226223a2013-03-12 11:30:05 -07004242 workqueue_sysfs_unregister(wq);
4243
Tejun Heo493008a2013-03-12 11:30:03 -07004244 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004245 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004246 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004247 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004248 }
4249
Tejun Heo8864b4e2013-03-12 11:30:04 -07004250 if (!(wq->flags & WQ_UNBOUND)) {
4251 /*
4252 * The base ref is never dropped on per-cpu pwqs. Directly
4253 * free the pwqs and wq.
4254 */
4255 free_percpu(wq->cpu_pwqs);
4256 kfree(wq);
4257 } else {
4258 /*
4259 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004260 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4261 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004262 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004263 for_each_node(node) {
4264 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4265 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4266 put_pwq_unlocked(pwq);
4267 }
4268
4269 /*
4270 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4271 * put. Don't access it afterwards.
4272 */
4273 pwq = wq->dfl_pwq;
4274 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004275 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004276 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004277}
4278EXPORT_SYMBOL_GPL(destroy_workqueue);
4279
Tejun Heodcd989c2010-06-29 10:07:14 +02004280/**
4281 * workqueue_set_max_active - adjust max_active of a workqueue
4282 * @wq: target workqueue
4283 * @max_active: new max_active value.
4284 *
4285 * Set max_active of @wq to @max_active.
4286 *
4287 * CONTEXT:
4288 * Don't call from IRQ context.
4289 */
4290void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4291{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004292 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004293
Tejun Heo8719dce2013-03-12 11:30:04 -07004294 /* disallow meddling with max_active for ordered workqueues */
4295 if (WARN_ON(wq->flags & __WQ_ORDERED))
4296 return;
4297
Tejun Heof3421792010-07-02 10:03:51 +02004298 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004299
Lai Jiangshana357fc02013-03-25 16:57:19 -07004300 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004301
4302 wq->saved_max_active = max_active;
4303
Tejun Heo699ce092013-03-13 16:51:35 -07004304 for_each_pwq(pwq, wq)
4305 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004306
Lai Jiangshana357fc02013-03-25 16:57:19 -07004307 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004308}
4309EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4310
4311/**
Tejun Heoe62676162013-03-12 17:41:37 -07004312 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4313 *
4314 * Determine whether %current is a workqueue rescuer. Can be used from
4315 * work functions to determine whether it's being run off the rescuer task.
4316 */
4317bool current_is_workqueue_rescuer(void)
4318{
4319 struct worker *worker = current_wq_worker();
4320
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004321 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07004322}
4323
4324/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004325 * workqueue_congested - test whether a workqueue is congested
4326 * @cpu: CPU in question
4327 * @wq: target workqueue
4328 *
4329 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4330 * no synchronization around this function and the test result is
4331 * unreliable and only useful as advisory hints or for debugging.
4332 *
Tejun Heod3251852013-05-10 11:10:17 -07004333 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4334 * Note that both per-cpu and unbound workqueues may be associated with
4335 * multiple pool_workqueues which have separate congested states. A
4336 * workqueue being congested on one CPU doesn't mean the workqueue is also
4337 * contested on other CPUs / NUMA nodes.
4338 *
Tejun Heodcd989c2010-06-29 10:07:14 +02004339 * RETURNS:
4340 * %true if congested, %false otherwise.
4341 */
Tejun Heod84ff052013-03-12 11:29:59 -07004342bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004343{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004344 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004345 bool ret;
4346
Lai Jiangshan88109452013-03-20 03:28:10 +08004347 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004348
Tejun Heod3251852013-05-10 11:10:17 -07004349 if (cpu == WORK_CPU_UNBOUND)
4350 cpu = smp_processor_id();
4351
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004352 if (!(wq->flags & WQ_UNBOUND))
4353 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4354 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004355 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004356
Tejun Heo76af4d92013-03-12 11:30:00 -07004357 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004358 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004359
4360 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004361}
4362EXPORT_SYMBOL_GPL(workqueue_congested);
4363
4364/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004365 * work_busy - test whether a work is currently pending or running
4366 * @work: the work to be tested
4367 *
4368 * Test whether @work is currently pending or running. There is no
4369 * synchronization around this function and the test result is
4370 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004371 *
4372 * RETURNS:
4373 * OR'd bitmask of WORK_BUSY_* bits.
4374 */
4375unsigned int work_busy(struct work_struct *work)
4376{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004377 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004378 unsigned long flags;
4379 unsigned int ret = 0;
4380
Tejun Heodcd989c2010-06-29 10:07:14 +02004381 if (work_pending(work))
4382 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004383
Tejun Heofa1b54e2013-03-12 11:30:00 -07004384 local_irq_save(flags);
4385 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004386 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004387 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004388 if (find_worker_executing_work(pool, work))
4389 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004390 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004391 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004392 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004393
4394 return ret;
4395}
4396EXPORT_SYMBOL_GPL(work_busy);
4397
Tejun Heo3d1cb202013-04-30 15:27:22 -07004398/**
4399 * set_worker_desc - set description for the current work item
4400 * @fmt: printf-style format string
4401 * @...: arguments for the format string
4402 *
4403 * This function can be called by a running work function to describe what
4404 * the work item is about. If the worker task gets dumped, this
4405 * information will be printed out together to help debugging. The
4406 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4407 */
4408void set_worker_desc(const char *fmt, ...)
4409{
4410 struct worker *worker = current_wq_worker();
4411 va_list args;
4412
4413 if (worker) {
4414 va_start(args, fmt);
4415 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4416 va_end(args);
4417 worker->desc_valid = true;
4418 }
4419}
4420
4421/**
4422 * print_worker_info - print out worker information and description
4423 * @log_lvl: the log level to use when printing
4424 * @task: target task
4425 *
4426 * If @task is a worker and currently executing a work item, print out the
4427 * name of the workqueue being serviced and worker description set with
4428 * set_worker_desc() by the currently executing work item.
4429 *
4430 * This function can be safely called on any task as long as the
4431 * task_struct itself is accessible. While safe, this function isn't
4432 * synchronized and may print out mixups or garbages of limited length.
4433 */
4434void print_worker_info(const char *log_lvl, struct task_struct *task)
4435{
4436 work_func_t *fn = NULL;
4437 char name[WQ_NAME_LEN] = { };
4438 char desc[WORKER_DESC_LEN] = { };
4439 struct pool_workqueue *pwq = NULL;
4440 struct workqueue_struct *wq = NULL;
4441 bool desc_valid = false;
4442 struct worker *worker;
4443
4444 if (!(task->flags & PF_WQ_WORKER))
4445 return;
4446
4447 /*
4448 * This function is called without any synchronization and @task
4449 * could be in any state. Be careful with dereferences.
4450 */
4451 worker = probe_kthread_data(task);
4452
4453 /*
4454 * Carefully copy the associated workqueue's workfn and name. Keep
4455 * the original last '\0' in case the original contains garbage.
4456 */
4457 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4458 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4459 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4460 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4461
4462 /* copy worker description */
4463 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4464 if (desc_valid)
4465 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4466
4467 if (fn || name[0] || desc[0]) {
4468 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4469 if (desc[0])
4470 pr_cont(" (%s)", desc);
4471 pr_cont("\n");
4472 }
4473}
4474
Tejun Heodb7bccf2010-06-29 10:07:12 +02004475/*
4476 * CPU hotplug.
4477 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004478 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004479 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004480 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004481 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004482 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004483 * blocked draining impractical.
4484 *
Tejun Heo24647572013-01-24 11:01:33 -08004485 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004486 * running as an unbound one and allowing it to be reattached later if the
4487 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004488 */
4489
Tejun Heo706026c2013-01-24 11:01:34 -08004490static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004491{
Tejun Heo38db41d2013-01-24 11:01:34 -08004492 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004493 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004494 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004495 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004496
Tejun Heof02ae732013-03-12 11:30:03 -07004497 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004498 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004499
Tejun Heobc3a1af2013-03-13 19:47:39 -07004500 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004501 spin_lock_irq(&pool->lock);
4502
4503 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004504 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004505 * unbound and set DISASSOCIATED. Before this, all workers
4506 * except for the ones which are still executing works from
4507 * before the last CPU down must be on the cpu. After
4508 * this, they may become diasporas.
4509 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004510 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004511 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004512
Tejun Heo24647572013-01-24 11:01:33 -08004513 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004514
Tejun Heo94cf58b2013-01-24 11:01:33 -08004515 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004516 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004517
Lai Jiangshaneb283422013-03-08 15:18:28 -08004518 /*
4519 * Call schedule() so that we cross rq->lock and thus can
4520 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4521 * This is necessary as scheduler callbacks may be invoked
4522 * from other cpus.
4523 */
4524 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004525
Lai Jiangshaneb283422013-03-08 15:18:28 -08004526 /*
4527 * Sched callbacks are disabled now. Zap nr_running.
4528 * After this, nr_running stays zero and need_more_worker()
4529 * and keep_working() are always true as long as the
4530 * worklist is not empty. This pool now behaves as an
4531 * unbound (in terms of concurrency management) pool which
4532 * are served by workers tied to the pool.
4533 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004534 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004535
4536 /*
4537 * With concurrency management just turned off, a busy
4538 * worker blocking could lead to lengthy stalls. Kick off
4539 * unbound chain execution of currently pending work items.
4540 */
4541 spin_lock_irq(&pool->lock);
4542 wake_up_worker(pool);
4543 spin_unlock_irq(&pool->lock);
4544 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004545}
4546
Tejun Heobd7c0892013-03-19 13:45:21 -07004547/**
4548 * rebind_workers - rebind all workers of a pool to the associated CPU
4549 * @pool: pool of interest
4550 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004551 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004552 */
4553static void rebind_workers(struct worker_pool *pool)
4554{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004555 struct worker *worker;
4556 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004557
4558 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004559
Tejun Heoa9ab7752013-03-19 13:45:21 -07004560 /*
4561 * Restore CPU affinity of all workers. As all idle workers should
4562 * be on the run-queue of the associated CPU before any local
4563 * wake-ups for concurrency management happen, restore CPU affinty
4564 * of all workers first and then clear UNBOUND. As we're called
4565 * from CPU_ONLINE, the following shouldn't fail.
4566 */
4567 for_each_pool_worker(worker, wi, pool)
4568 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4569 pool->attrs->cpumask) < 0);
4570
4571 spin_lock_irq(&pool->lock);
4572
4573 for_each_pool_worker(worker, wi, pool) {
4574 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004575
4576 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004577 * A bound idle worker should actually be on the runqueue
4578 * of the associated CPU for local wake-ups targeting it to
4579 * work. Kick all idle workers so that they migrate to the
4580 * associated CPU. Doing this in the same loop as
4581 * replacing UNBOUND with REBOUND is safe as no worker will
4582 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004583 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004584 if (worker_flags & WORKER_IDLE)
4585 wake_up_process(worker->task);
4586
4587 /*
4588 * We want to clear UNBOUND but can't directly call
4589 * worker_clr_flags() or adjust nr_running. Atomically
4590 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4591 * @worker will clear REBOUND using worker_clr_flags() when
4592 * it initiates the next execution cycle thus restoring
4593 * concurrency management. Note that when or whether
4594 * @worker clears REBOUND doesn't affect correctness.
4595 *
4596 * ACCESS_ONCE() is necessary because @worker->flags may be
4597 * tested without holding any lock in
4598 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4599 * fail incorrectly leading to premature concurrency
4600 * management operations.
4601 */
4602 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4603 worker_flags |= WORKER_REBOUND;
4604 worker_flags &= ~WORKER_UNBOUND;
4605 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004606 }
4607
Tejun Heoa9ab7752013-03-19 13:45:21 -07004608 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004609}
4610
Tejun Heo7dbc7252013-03-19 13:45:21 -07004611/**
4612 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4613 * @pool: unbound pool of interest
4614 * @cpu: the CPU which is coming up
4615 *
4616 * An unbound pool may end up with a cpumask which doesn't have any online
4617 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4618 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4619 * online CPU before, cpus_allowed of all its workers should be restored.
4620 */
4621static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4622{
4623 static cpumask_t cpumask;
4624 struct worker *worker;
4625 int wi;
4626
4627 lockdep_assert_held(&pool->manager_mutex);
4628
4629 /* is @cpu allowed for @pool? */
4630 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4631 return;
4632
4633 /* is @cpu the only online CPU? */
4634 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4635 if (cpumask_weight(&cpumask) != 1)
4636 return;
4637
4638 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4639 for_each_pool_worker(worker, wi, pool)
4640 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4641 pool->attrs->cpumask) < 0);
4642}
4643
Tejun Heo8db25e72012-07-17 12:39:28 -07004644/*
4645 * Workqueues should be brought up before normal priority CPU notifiers.
4646 * This will be registered high priority CPU notifier.
4647 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004648static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004649 unsigned long action,
4650 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004651{
Tejun Heod84ff052013-03-12 11:29:59 -07004652 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004653 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004654 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004655 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Tejun Heo8db25e72012-07-17 12:39:28 -07004657 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004659 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004660 if (pool->nr_workers)
4661 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004662 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004663 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004665 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004666
Tejun Heo65758202012-07-17 12:39:26 -07004667 case CPU_DOWN_FAILED:
4668 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004669 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004670
4671 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004672 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004673
Tejun Heo7dbc7252013-03-19 13:45:21 -07004674 if (pool->cpu == cpu) {
4675 spin_lock_irq(&pool->lock);
4676 pool->flags &= ~POOL_DISASSOCIATED;
4677 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004678
Tejun Heo7dbc7252013-03-19 13:45:21 -07004679 rebind_workers(pool);
4680 } else if (pool->cpu < 0) {
4681 restore_unbound_workers_cpumask(pool, cpu);
4682 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004683
Tejun Heobc3a1af2013-03-13 19:47:39 -07004684 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004685 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004686
Tejun Heo4c16bd32013-04-01 11:23:36 -07004687 /* update NUMA affinity of unbound workqueues */
4688 list_for_each_entry(wq, &workqueues, list)
4689 wq_update_unbound_numa(wq, cpu, true);
4690
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004691 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004692 break;
Tejun Heo65758202012-07-17 12:39:26 -07004693 }
4694 return NOTIFY_OK;
4695}
4696
4697/*
4698 * Workqueues should be brought down after normal priority CPU notifiers.
4699 * This will be registered as low priority CPU notifier.
4700 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004701static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004702 unsigned long action,
4703 void *hcpu)
4704{
Tejun Heod84ff052013-03-12 11:29:59 -07004705 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004706 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004707 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004708
Tejun Heo65758202012-07-17 12:39:26 -07004709 switch (action & ~CPU_TASKS_FROZEN) {
4710 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004711 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004712 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004713 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004714
4715 /* update NUMA affinity of unbound workqueues */
4716 mutex_lock(&wq_pool_mutex);
4717 list_for_each_entry(wq, &workqueues, list)
4718 wq_update_unbound_numa(wq, cpu, false);
4719 mutex_unlock(&wq_pool_mutex);
4720
4721 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004722 flush_work(&unbind_work);
4723 break;
Tejun Heo65758202012-07-17 12:39:26 -07004724 }
4725 return NOTIFY_OK;
4726}
4727
Rusty Russell2d3854a2008-11-05 13:39:10 +11004728#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004729
Rusty Russell2d3854a2008-11-05 13:39:10 +11004730struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004731 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004732 long (*fn)(void *);
4733 void *arg;
4734 long ret;
4735};
4736
Tejun Heoed48ece2012-09-18 12:48:43 -07004737static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004738{
Tejun Heoed48ece2012-09-18 12:48:43 -07004739 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4740
Rusty Russell2d3854a2008-11-05 13:39:10 +11004741 wfc->ret = wfc->fn(wfc->arg);
4742}
4743
4744/**
4745 * work_on_cpu - run a function in user context on a particular cpu
4746 * @cpu: the cpu to run on
4747 * @fn: the function to run
4748 * @arg: the function arg
4749 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004750 * This will return the value @fn returns.
4751 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06004752 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004753 */
Tejun Heod84ff052013-03-12 11:29:59 -07004754long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004755{
Tejun Heoed48ece2012-09-18 12:48:43 -07004756 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004757
Tejun Heoed48ece2012-09-18 12:48:43 -07004758 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4759 schedule_work_on(cpu, &wfc.work);
4760 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004761 return wfc.ret;
4762}
4763EXPORT_SYMBOL_GPL(work_on_cpu);
4764#endif /* CONFIG_SMP */
4765
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004766#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304767
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004768/**
4769 * freeze_workqueues_begin - begin freezing workqueues
4770 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004771 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004772 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004773 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004774 *
4775 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004776 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004777 */
4778void freeze_workqueues_begin(void)
4779{
Tejun Heo17116962013-03-12 11:29:58 -07004780 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004781 struct workqueue_struct *wq;
4782 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004783 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004784
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004785 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004786
Tejun Heo6183c002013-03-12 11:29:57 -07004787 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004788 workqueue_freezing = true;
4789
Tejun Heo24b8a842013-03-12 11:29:58 -07004790 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004791 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004792 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004793 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4794 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004795 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004796 }
4797
Tejun Heo24b8a842013-03-12 11:29:58 -07004798 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004799 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004800 for_each_pwq(pwq, wq)
4801 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004802 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004803 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004804
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004805 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004807
4808/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004809 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004810 *
4811 * Check whether freezing is complete. This function must be called
4812 * between freeze_workqueues_begin() and thaw_workqueues().
4813 *
4814 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004815 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004816 *
4817 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004818 * %true if some freezable workqueues are still busy. %false if freezing
4819 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004820 */
4821bool freeze_workqueues_busy(void)
4822{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004823 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004824 struct workqueue_struct *wq;
4825 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004826
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004827 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004828
Tejun Heo6183c002013-03-12 11:29:57 -07004829 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004830
Tejun Heo24b8a842013-03-12 11:29:58 -07004831 list_for_each_entry(wq, &workqueues, list) {
4832 if (!(wq->flags & WQ_FREEZABLE))
4833 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004834 /*
4835 * nr_active is monotonically decreasing. It's safe
4836 * to peek without lock.
4837 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004838 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004839 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004840 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004841 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004842 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004843 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004844 goto out_unlock;
4845 }
4846 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004847 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004848 }
4849out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004850 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004851 return busy;
4852}
4853
4854/**
4855 * thaw_workqueues - thaw workqueues
4856 *
4857 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004858 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004859 *
4860 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004861 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004862 */
4863void thaw_workqueues(void)
4864{
Tejun Heo24b8a842013-03-12 11:29:58 -07004865 struct workqueue_struct *wq;
4866 struct pool_workqueue *pwq;
4867 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004868 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004869
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004870 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004871
4872 if (!workqueue_freezing)
4873 goto out_unlock;
4874
Tejun Heo24b8a842013-03-12 11:29:58 -07004875 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004876 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004877 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004878 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4879 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004880 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004881 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004882
Tejun Heo24b8a842013-03-12 11:29:58 -07004883 /* restore max_active and repopulate worklist */
4884 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004885 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004886 for_each_pwq(pwq, wq)
4887 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004888 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004889 }
4890
4891 workqueue_freezing = false;
4892out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004893 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004894}
4895#endif /* CONFIG_FREEZER */
4896
Tejun Heobce90382013-04-01 11:23:32 -07004897static void __init wq_numa_init(void)
4898{
4899 cpumask_var_t *tbl;
4900 int node, cpu;
4901
4902 /* determine NUMA pwq table len - highest node id + 1 */
4903 for_each_node(node)
4904 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4905
4906 if (num_possible_nodes() <= 1)
4907 return;
4908
Tejun Heod55262c2013-04-01 11:23:38 -07004909 if (wq_disable_numa) {
4910 pr_info("workqueue: NUMA affinity support disabled\n");
4911 return;
4912 }
4913
Tejun Heo4c16bd32013-04-01 11:23:36 -07004914 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4915 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4916
Tejun Heobce90382013-04-01 11:23:32 -07004917 /*
4918 * We want masks of possible CPUs of each node which isn't readily
4919 * available. Build one from cpu_to_node() which should have been
4920 * fully initialized by now.
4921 */
4922 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4923 BUG_ON(!tbl);
4924
4925 for_each_node(node)
Tejun Heo1be0c252013-05-15 14:24:24 -07004926 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
4927 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07004928
4929 for_each_possible_cpu(cpu) {
4930 node = cpu_to_node(cpu);
4931 if (WARN_ON(node == NUMA_NO_NODE)) {
4932 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4933 /* happens iff arch is bonkers, let's just proceed */
4934 return;
4935 }
4936 cpumask_set_cpu(cpu, tbl[node]);
4937 }
4938
4939 wq_numa_possible_cpumask = tbl;
4940 wq_numa_enabled = true;
4941}
4942
Suresh Siddha6ee05782010-07-30 14:57:37 -07004943static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004945 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4946 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004947
Tejun Heo7c3eed52013-01-24 11:01:33 -08004948 /* make sure we have enough bits for OFFQ pool ID */
4949 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004950 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004951
Tejun Heoe904e6c2013-03-12 11:29:57 -07004952 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4953
4954 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4955
Tejun Heo65758202012-07-17 12:39:26 -07004956 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004957 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004958
Tejun Heobce90382013-04-01 11:23:32 -07004959 wq_numa_init();
4960
Tejun Heo706026c2013-01-24 11:01:34 -08004961 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004962 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004963 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004964
Tejun Heo7a4e3442013-03-12 11:30:00 -07004965 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004966 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004967 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004968 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004969 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004970 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004971 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004972
Tejun Heo9daf9e62013-01-24 11:01:33 -08004973 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004974 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004975 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004976 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004977 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004978 }
4979
Tejun Heoe22bee72010-06-29 10:07:14 +02004980 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004981 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004982 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004983
Tejun Heof02ae732013-03-12 11:30:03 -07004984 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004985 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004986 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004987 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004988 }
4989
Tejun Heo29c91e92013-03-12 11:30:03 -07004990 /* create default unbound wq attrs */
4991 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4992 struct workqueue_attrs *attrs;
4993
4994 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004995 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004996 unbound_std_wq_attrs[i] = attrs;
4997 }
4998
Tejun Heod320c032010-06-29 10:07:14 +02004999 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005000 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005001 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005002 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5003 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005004 system_freezable_wq = alloc_workqueue("events_freezable",
5005 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305006 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5007 WQ_POWER_EFFICIENT, 0);
5008 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5009 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5010 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005011 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305012 !system_unbound_wq || !system_freezable_wq ||
5013 !system_power_efficient_wq ||
5014 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005017early_initcall(init_workqueues);