blob: 6aee854ae0816fe9b0fa0c6868da43e8c0fc723c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
Libinb11895c2013-08-21 08:50:39 +080019 * automatically managed. There are two worker pools for each CPU (one for
20 * normal work items and the other for high priority ones) and some extra
21 * pools for workqueues which are not bound to any specific CPU - the
22 * number of these backing pools is dynamic.
Tejun Heoc54fce62010-09-10 16:51:36 +020023 *
24 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Paul Gortmaker9984de12011-05-23 14:51:41 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/sched.h>
30#include <linux/init.h>
31#include <linux/signal.h>
32#include <linux/completion.h>
33#include <linux/workqueue.h>
34#include <linux/slab.h>
35#include <linux/cpu.h>
36#include <linux/notifier.h>
37#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060038#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070039#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080040#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080041#include <linux/kallsyms.h>
42#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070043#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020044#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070045#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050046#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070047#include <linux/rculist.h>
Tejun Heobce90382013-04-01 11:23:32 -070048#include <linux/nodemask.h>
Tejun Heo4c16bd32013-04-01 11:23:36 -070049#include <linux/moduleparam.h>
Tejun Heo3d1cb202013-04-30 15:27:22 -070050#include <linux/uaccess.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020051
Tejun Heoea138442013-01-18 14:05:55 -080052#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tejun Heoc8e55f32010-06-29 10:07:12 +020054enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 /*
Tejun Heo24647572013-01-24 11:01:33 -080056 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070057 *
Tejun Heo24647572013-01-24 11:01:33 -080058 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070059 * While associated (!DISASSOCIATED), all workers are bound to the
60 * CPU and none has %WORKER_UNBOUND set and concurrency management
61 * is in effect.
62 *
63 * While DISASSOCIATED, the cpu may be offline and all workers have
64 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080065 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070067 * Note that DISASSOCIATED should be flipped only while holding
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +080068 * attach_mutex to avoid changing binding state while
Lai Jiangshan4736cbf2014-05-20 17:46:35 +080069 * worker_attach_to_pool() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070070 */
Tejun Heo24647572013-01-24 11:01:33 -080071 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020072
Tejun Heoc8e55f32010-06-29 10:07:12 +020073 /* worker flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074 WORKER_DIE = 1 << 1, /* die die die */
75 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020076 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020077 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020078 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070079 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020080
Tejun Heoa9ab7752013-03-19 13:45:21 -070081 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
82 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020083
Tejun Heoe34cdddb2013-01-24 11:01:33 -080084 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070085
Tejun Heo29c91e92013-03-12 11:30:03 -070086 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020087 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020088
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
90 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
91
Tejun Heo3233cdb2011-02-16 18:10:19 +010092 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
93 /* call for help after 10ms
94 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
96 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020097
98 /*
99 * Rescue workers are used only on emergencies and shared by
Dongsheng Yang8698a742014-03-11 18:09:12 +0800100 * all cpus. Give MIN_NICE.
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 */
Dongsheng Yang8698a742014-03-11 18:09:12 +0800102 RESCUER_NICE_LEVEL = MIN_NICE,
103 HIGHPRI_NICE_LEVEL = MIN_NICE,
Tejun Heoecf68812013-04-01 11:23:34 -0700104
105 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 * Structure fields follow one of the following exclusion rules.
110 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200111 * I: Modifiable by initialization/destruction paths and read-only for
112 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200113 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200114 * P: Preemption protected. Disabling preemption is enough and should
115 * only be modified and accessed from the local cpu.
116 *
Tejun Heod565ed62013-01-24 11:01:33 -0800117 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heod565ed62013-01-24 11:01:33 -0800119 * X: During normal operation, modification requires pool->lock and should
120 * be done only from local cpu. Either disabling preemption on local
121 * cpu or grabbing pool->lock is enough for read access. If
122 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200123 *
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800124 * A: pool->attach_mutex protected.
Tejun Heo822d8402013-03-19 13:45:21 -0700125 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700126 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700127 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700128 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700129 *
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800130 * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
131 *
132 * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or
133 * sched-RCU for reads.
134 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700135 * WQ: wq->mutex protected.
136 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700137 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700138 *
139 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200140 */
141
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800142/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200143
Tejun Heobd7bdd42012-07-12 14:46:37 -0700144struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800145 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700146 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700147 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800148 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700149 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700150
151 struct list_head worklist; /* L: list of pending works */
152 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700153
154 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700155 int nr_idle; /* L: currently idle ones */
156
157 struct list_head idle_list; /* X: list of idle workers */
158 struct timer_list idle_timer; /* L: worker idle timeout */
159 struct timer_list mayday_timer; /* L: SOS timer for workers */
160
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700161 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800162 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
163 /* L: hash of busy workers */
164
Tejun Heobc3a1af2013-03-13 19:47:39 -0700165 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700166 struct mutex manager_arb; /* manager arbitration */
Tejun Heo2607d7a2015-03-09 09:22:28 -0400167 struct worker *manager; /* L: purely informational */
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800168 struct mutex attach_mutex; /* attach/detach exclusion */
169 struct list_head workers; /* A: attached workers */
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +0800170 struct completion *detach_completion; /* all workers detached */
Tejun Heoe19e3972013-01-24 11:39:44 -0800171
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +0800172 struct ida worker_ida; /* worker IDs for task name */
Tejun Heoe19e3972013-01-24 11:39:44 -0800173
Tejun Heo7a4e3442013-03-12 11:30:00 -0700174 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700175 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
176 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700177
Tejun Heoe19e3972013-01-24 11:39:44 -0800178 /*
179 * The current concurrency level. As it's likely to be accessed
180 * from other CPUs during try_to_wake_up(), put it in a separate
181 * cacheline.
182 */
183 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700184
185 /*
186 * Destruction of pool is sched-RCU protected to allow dereferences
187 * from get_work_pool().
188 */
189 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200190} ____cacheline_aligned_in_smp;
191
192/*
Tejun Heo112202d2013-02-13 19:29:12 -0800193 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
194 * of work_struct->data are used for flags and the remaining high bits
195 * point to the pwq; thus, pwqs need to be aligned at two's power of the
196 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Tejun Heo112202d2013-02-13 19:29:12 -0800198struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700199 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200200 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200201 int work_color; /* L: current color */
202 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700203 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200204 int nr_in_flight[WORK_NR_COLORS];
205 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200206 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200207 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200208 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700209 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700210 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700211
212 /*
213 * Release of unbound pwq is punted to system_wq. See put_pwq()
214 * and pwq_unbound_release_workfn() for details. pool_workqueue
215 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700216 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700217 */
218 struct work_struct unbound_release_work;
219 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700220} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200223 * Structure used to wait for workqueue flush.
224 */
225struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700226 struct list_head list; /* WQ: list of flushers */
227 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200228 struct completion done; /* flush completion */
229};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Tejun Heo226223a2013-03-12 11:30:05 -0700231struct wq_device;
232
Tejun Heo73f53c42010-06-29 10:07:11 +0200233/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700234 * The externally visible workqueue. It relays the issued work items to
235 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700238 struct list_head pwqs; /* WR: all pwqs of this wq */
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400239 struct list_head list; /* PR: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200240
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700241 struct mutex mutex; /* protects this wq */
242 int work_color; /* WQ: current work color */
243 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800244 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700245 struct wq_flusher *first_flusher; /* WQ: first flusher */
246 struct list_head flusher_queue; /* WQ: flush waiters */
247 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200248
Tejun Heo2e109a22013-03-13 19:47:40 -0700249 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200250 struct worker *rescuer; /* I: rescue worker */
251
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700252 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700253 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700254
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800255 struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */
256 struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700257
Tejun Heo226223a2013-03-12 11:30:05 -0700258#ifdef CONFIG_SYSFS
259 struct wq_device *wq_dev; /* I: for sysfs interface */
260#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700261#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200262 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700263#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700264 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700265
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400266 /*
267 * Destruction of workqueue_struct is sched-RCU protected to allow
268 * walking the workqueues list without grabbing wq_pool_mutex.
269 * This is used to dump all workqueues from sysrq.
270 */
271 struct rcu_head rcu;
272
Tejun Heo2728fd22013-04-01 11:23:35 -0700273 /* hot fields used during command issue, aligned to cacheline */
274 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
275 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800276 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Tejun Heoe904e6c2013-03-12 11:29:57 -0700279static struct kmem_cache *pwq_cache;
280
Tejun Heobce90382013-04-01 11:23:32 -0700281static cpumask_var_t *wq_numa_possible_cpumask;
282 /* possible CPUs of each node */
283
Tejun Heod55262c2013-04-01 11:23:38 -0700284static bool wq_disable_numa;
285module_param_named(disable_numa, wq_disable_numa, bool, 0444);
286
Viresh Kumarcee22a12013-04-08 16:45:40 +0530287/* see the comment above the definition of WQ_POWER_EFFICIENT */
Luis R. Rodriguez552f5302015-05-27 11:09:39 +0930288static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT);
Viresh Kumarcee22a12013-04-08 16:45:40 +0530289module_param_named(power_efficient, wq_power_efficient, bool, 0444);
290
Tejun Heobce90382013-04-01 11:23:32 -0700291static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
292
Tejun Heo4c16bd32013-04-01 11:23:36 -0700293/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
294static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
295
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700296static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700297static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700298
Tejun Heoe2dca7a2015-03-09 09:22:28 -0400299static LIST_HEAD(workqueues); /* PR: list of all workqueues */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700300static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700301
Lai Jiangshan042f7df2015-04-30 17:16:12 +0800302static cpumask_var_t wq_unbound_cpumask; /* PL: low level cpumask for all unbound wqs */
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +0800303
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700304/* the per-cpu worker pools */
305static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
306 cpu_worker_pools);
307
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700308static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700309
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700310/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700311static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
312
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700313/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700314static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
315
Tejun Heo8a2b7532013-09-05 12:30:04 -0400316/* I: attributes used when instantiating ordered pools on demand */
317static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS];
318
Tejun Heod320c032010-06-29 10:07:14 +0200319struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400320EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300321struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900322EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300323struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200324EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300325struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200326EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300327struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100328EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530329struct workqueue_struct *system_power_efficient_wq __read_mostly;
330EXPORT_SYMBOL_GPL(system_power_efficient_wq);
331struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
332EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200333
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700334static int worker_thread(void *__worker);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +0800335static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700336
Tejun Heo97bd2342010-10-05 10:41:14 +0200337#define CREATE_TRACE_POINTS
338#include <trace/events/workqueue.h>
339
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700340#define assert_rcu_or_pool_mutex() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700341 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
342 !lockdep_is_held(&wq_pool_mutex), \
343 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700344
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700345#define assert_rcu_or_wq_mutex(wq) \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700346 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
347 !lockdep_is_held(&wq->mutex), \
348 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700349
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800350#define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700351 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
352 !lockdep_is_held(&wq->mutex) && \
353 !lockdep_is_held(&wq_pool_mutex), \
354 "sched RCU, wq->mutex or wq_pool_mutex should be held")
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800355
Tejun Heof02ae732013-03-12 11:30:03 -0700356#define for_each_cpu_worker_pool(pool, cpu) \
357 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
358 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700359 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700360
Tejun Heo49e3cf42013-03-12 11:29:58 -0700361/**
Tejun Heo17116962013-03-12 11:29:58 -0700362 * for_each_pool - iterate through all worker_pools in the system
363 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700364 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700365 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700366 * This must be called either with wq_pool_mutex held or sched RCU read
367 * locked. If the pool needs to be used beyond the locking in effect, the
368 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700369 *
370 * The if/else clause exists only for the lockdep assertion and can be
371 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700372 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700373#define for_each_pool(pool, pi) \
374 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700375 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700376 else
Tejun Heo17116962013-03-12 11:29:58 -0700377
378/**
Tejun Heo822d8402013-03-19 13:45:21 -0700379 * for_each_pool_worker - iterate through all workers of a worker_pool
380 * @worker: iteration cursor
Tejun Heo822d8402013-03-19 13:45:21 -0700381 * @pool: worker_pool to iterate workers of
382 *
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800383 * This must be called with @pool->attach_mutex.
Tejun Heo822d8402013-03-19 13:45:21 -0700384 *
385 * The if/else clause exists only for the lockdep assertion and can be
386 * ignored.
387 */
Lai Jiangshanda028462014-05-20 17:46:31 +0800388#define for_each_pool_worker(worker, pool) \
389 list_for_each_entry((worker), &(pool)->workers, node) \
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +0800390 if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \
Tejun Heo822d8402013-03-19 13:45:21 -0700391 else
392
393/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700394 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
395 * @pwq: iteration cursor
396 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700397 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700398 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700399 * If the pwq needs to be used beyond the locking in effect, the caller is
400 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700401 *
402 * The if/else clause exists only for the lockdep assertion and can be
403 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700404 */
405#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700406 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700407 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700408 else
Tejun Heof3421792010-07-02 10:03:51 +0200409
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900410#ifdef CONFIG_DEBUG_OBJECTS_WORK
411
412static struct debug_obj_descr work_debug_descr;
413
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100414static void *work_debug_hint(void *addr)
415{
416 return ((struct work_struct *) addr)->func;
417}
418
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900419/*
420 * fixup_init is called when:
421 * - an active object is initialized
422 */
423static int work_fixup_init(void *addr, enum debug_obj_state state)
424{
425 struct work_struct *work = addr;
426
427 switch (state) {
428 case ODEBUG_STATE_ACTIVE:
429 cancel_work_sync(work);
430 debug_object_init(work, &work_debug_descr);
431 return 1;
432 default:
433 return 0;
434 }
435}
436
437/*
438 * fixup_activate is called when:
439 * - an active object is activated
440 * - an unknown object is activated (might be a statically initialized object)
441 */
442static int work_fixup_activate(void *addr, enum debug_obj_state state)
443{
444 struct work_struct *work = addr;
445
446 switch (state) {
447
448 case ODEBUG_STATE_NOTAVAILABLE:
449 /*
450 * This is not really a fixup. The work struct was
451 * statically initialized. We just make sure that it
452 * is tracked in the object tracker.
453 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200454 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900455 debug_object_init(work, &work_debug_descr);
456 debug_object_activate(work, &work_debug_descr);
457 return 0;
458 }
459 WARN_ON_ONCE(1);
460 return 0;
461
462 case ODEBUG_STATE_ACTIVE:
463 WARN_ON(1);
464
465 default:
466 return 0;
467 }
468}
469
470/*
471 * fixup_free is called when:
472 * - an active object is freed
473 */
474static int work_fixup_free(void *addr, enum debug_obj_state state)
475{
476 struct work_struct *work = addr;
477
478 switch (state) {
479 case ODEBUG_STATE_ACTIVE:
480 cancel_work_sync(work);
481 debug_object_free(work, &work_debug_descr);
482 return 1;
483 default:
484 return 0;
485 }
486}
487
488static struct debug_obj_descr work_debug_descr = {
489 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100490 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900491 .fixup_init = work_fixup_init,
492 .fixup_activate = work_fixup_activate,
493 .fixup_free = work_fixup_free,
494};
495
496static inline void debug_work_activate(struct work_struct *work)
497{
498 debug_object_activate(work, &work_debug_descr);
499}
500
501static inline void debug_work_deactivate(struct work_struct *work)
502{
503 debug_object_deactivate(work, &work_debug_descr);
504}
505
506void __init_work(struct work_struct *work, int onstack)
507{
508 if (onstack)
509 debug_object_init_on_stack(work, &work_debug_descr);
510 else
511 debug_object_init(work, &work_debug_descr);
512}
513EXPORT_SYMBOL_GPL(__init_work);
514
515void destroy_work_on_stack(struct work_struct *work)
516{
517 debug_object_free(work, &work_debug_descr);
518}
519EXPORT_SYMBOL_GPL(destroy_work_on_stack);
520
Thomas Gleixnerea2e64f2014-03-23 14:20:44 +0000521void destroy_delayed_work_on_stack(struct delayed_work *work)
522{
523 destroy_timer_on_stack(&work->timer);
524 debug_object_free(&work->work, &work_debug_descr);
525}
526EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack);
527
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900528#else
529static inline void debug_work_activate(struct work_struct *work) { }
530static inline void debug_work_deactivate(struct work_struct *work) { }
531#endif
532
Li Bin4e8b22b2013-09-10 09:52:35 +0800533/**
534 * worker_pool_assign_id - allocate ID and assing it to @pool
535 * @pool: the pool pointer of interest
536 *
537 * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned
538 * successfully, -errno on failure.
539 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800540static int worker_pool_assign_id(struct worker_pool *pool)
541{
542 int ret;
543
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700544 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700545
Li Bin4e8b22b2013-09-10 09:52:35 +0800546 ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,
547 GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700548 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700549 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700550 return 0;
551 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800552 return ret;
553}
554
Tejun Heo76af4d92013-03-12 11:30:00 -0700555/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700556 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
557 * @wq: the target workqueue
558 * @node: the node ID
559 *
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800560 * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
561 * read locked.
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700562 * If the pwq needs to be used beyond the locking in effect, the caller is
563 * responsible for guaranteeing that the pwq stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700564 *
565 * Return: The unbound pool_workqueue for @node.
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700566 */
567static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
568 int node)
569{
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +0800570 assert_rcu_or_wq_mutex_or_pool_mutex(wq);
Tejun Heo21b34b42016-02-03 13:54:25 -0500571
572 /*
573 * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a
574 * delayed item is pending. The plan is to keep CPU -> NODE
575 * mapping valid and stable across CPU on/offlines. Once that
576 * happens, this workaround can be removed.
577 */
578 if (unlikely(node == NUMA_NO_NODE))
579 return wq->dfl_pwq;
580
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700581 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
582}
583
Tejun Heo73f53c42010-06-29 10:07:11 +0200584static unsigned int work_color_to_flags(int color)
585{
586 return color << WORK_STRUCT_COLOR_SHIFT;
587}
588
589static int get_work_color(struct work_struct *work)
590{
591 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
592 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
593}
594
595static int work_next_color(int color)
596{
597 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700598}
599
David Howells4594bf12006-12-07 11:33:26 +0000600/*
Tejun Heo112202d2013-02-13 19:29:12 -0800601 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
602 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800603 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200604 *
Tejun Heo112202d2013-02-13 19:29:12 -0800605 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
606 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700607 * work->data. These functions should only be called while the work is
608 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200609 *
Tejun Heo112202d2013-02-13 19:29:12 -0800610 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800611 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800612 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800613 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700614 *
615 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
616 * canceled. While being canceled, a work item may have its PENDING set
617 * but stay off timer and worklist for arbitrarily long and nobody should
618 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000619 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200620static inline void set_work_data(struct work_struct *work, unsigned long data,
621 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000622{
Tejun Heo6183c002013-03-12 11:29:57 -0700623 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200624 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000625}
David Howells365970a2006-11-22 14:54:49 +0000626
Tejun Heo112202d2013-02-13 19:29:12 -0800627static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200628 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200629{
Tejun Heo112202d2013-02-13 19:29:12 -0800630 set_work_data(work, (unsigned long)pwq,
631 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200632}
633
Lai Jiangshan4468a002013-02-06 18:04:53 -0800634static void set_work_pool_and_keep_pending(struct work_struct *work,
635 int pool_id)
636{
637 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
638 WORK_STRUCT_PENDING);
639}
640
Tejun Heo7c3eed52013-01-24 11:01:33 -0800641static void set_work_pool_and_clear_pending(struct work_struct *work,
642 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000643{
Tejun Heo23657bb2012-08-13 17:08:19 -0700644 /*
645 * The following wmb is paired with the implied mb in
646 * test_and_set_bit(PENDING) and ensures all updates to @work made
647 * here are visible to and precede any updates by the next PENDING
648 * owner.
649 */
650 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800651 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Roman Pen2da96062016-04-26 13:15:35 +0200652 /*
653 * The following mb guarantees that previous clear of a PENDING bit
654 * will not be reordered with any speculative LOADS or STORES from
655 * work->current_func, which is executed afterwards. This possible
656 * reordering can lead to a missed execution on attempt to qeueue
657 * the same @work. E.g. consider this case:
658 *
659 * CPU#0 CPU#1
660 * ---------------------------- --------------------------------
661 *
662 * 1 STORE event_indicated
663 * 2 queue_work_on() {
664 * 3 test_and_set_bit(PENDING)
665 * 4 } set_..._and_clear_pending() {
666 * 5 set_work_data() # clear bit
667 * 6 smp_mb()
668 * 7 work->current_func() {
669 * 8 LOAD event_indicated
670 * }
671 *
672 * Without an explicit full barrier speculative LOAD on line 8 can
673 * be executed before CPU#0 does STORE on line 1. If that happens,
674 * CPU#0 observes the PENDING bit is still set and new execution of
675 * a @work is not queued in a hope, that CPU#1 will eventually
676 * finish the queued @work. Meanwhile CPU#1 does not see
677 * event_indicated is set, because speculative LOAD was executed
678 * before actual STORE.
679 */
680 smp_mb();
Tejun Heo7a22ad72010-06-29 10:07:13 +0200681}
682
683static void clear_work_data(struct work_struct *work)
684{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800685 smp_wmb(); /* see set_work_pool_and_clear_pending() */
686 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200687}
688
Tejun Heo112202d2013-02-13 19:29:12 -0800689static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200690{
Tejun Heoe1201532010-07-22 14:14:25 +0200691 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200692
Tejun Heo112202d2013-02-13 19:29:12 -0800693 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200694 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
695 else
696 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200697}
698
Tejun Heo7c3eed52013-01-24 11:01:33 -0800699/**
700 * get_work_pool - return the worker_pool a given work was associated with
701 * @work: the work item of interest
702 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700703 * Pools are created and destroyed under wq_pool_mutex, and allows read
704 * access under sched-RCU read lock. As such, this function should be
705 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700706 *
707 * All fields of the returned pool are accessible as long as the above
708 * mentioned locking is in effect. If the returned pool needs to be used
709 * beyond the critical section, the caller is responsible for ensuring the
710 * returned pool is and stays online.
Yacine Belkadid185af32013-07-31 14:59:24 -0700711 *
712 * Return: The worker_pool @work was last associated with. %NULL if none.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800713 */
714static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200715{
Tejun Heoe1201532010-07-22 14:14:25 +0200716 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800717 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200718
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700719 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700720
Tejun Heo112202d2013-02-13 19:29:12 -0800721 if (data & WORK_STRUCT_PWQ)
722 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800723 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200724
Tejun Heo7c3eed52013-01-24 11:01:33 -0800725 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
726 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200727 return NULL;
728
Tejun Heofa1b54e2013-03-12 11:30:00 -0700729 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800730}
731
732/**
733 * get_work_pool_id - return the worker pool ID a given work is associated with
734 * @work: the work item of interest
735 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700736 * Return: The worker_pool ID @work was last associated with.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800737 * %WORK_OFFQ_POOL_NONE if none.
738 */
739static int get_work_pool_id(struct work_struct *work)
740{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800741 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800742
Tejun Heo112202d2013-02-13 19:29:12 -0800743 if (data & WORK_STRUCT_PWQ)
744 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800745 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
746
747 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800748}
749
Tejun Heobbb68df2012-08-03 10:30:46 -0700750static void mark_work_canceling(struct work_struct *work)
751{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800752 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700753
Tejun Heo7c3eed52013-01-24 11:01:33 -0800754 pool_id <<= WORK_OFFQ_POOL_SHIFT;
755 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700756}
757
758static bool work_is_canceling(struct work_struct *work)
759{
760 unsigned long data = atomic_long_read(&work->data);
761
Tejun Heo112202d2013-02-13 19:29:12 -0800762 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700763}
764
David Howells365970a2006-11-22 14:54:49 +0000765/*
Tejun Heo32704762012-07-13 22:16:45 -0700766 * Policy functions. These define the policies on how the global worker
767 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800768 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000769 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200770
Tejun Heo63d95a92012-07-12 14:46:37 -0700771static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000772{
Tejun Heoe19e3972013-01-24 11:39:44 -0800773 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000774}
775
Tejun Heoe22bee72010-06-29 10:07:14 +0200776/*
777 * Need to wake up a worker? Called from anything but currently
778 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700779 *
780 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800781 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700782 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200783 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700784static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000785{
Tejun Heo63d95a92012-07-12 14:46:37 -0700786 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000787}
788
Tejun Heoe22bee72010-06-29 10:07:14 +0200789/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700790static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200791{
Tejun Heo63d95a92012-07-12 14:46:37 -0700792 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200793}
794
795/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700796static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200797{
Tejun Heoe19e3972013-01-24 11:39:44 -0800798 return !list_empty(&pool->worklist) &&
799 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200800}
801
802/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700803static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200804{
Tejun Heo63d95a92012-07-12 14:46:37 -0700805 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200806}
807
Tejun Heoe22bee72010-06-29 10:07:14 +0200808/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700809static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200810{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700811 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700812 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
813 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200814
815 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
816}
817
818/*
819 * Wake up functions.
820 */
821
Lai Jiangshan1037de32014-05-22 16:44:07 +0800822/* Return the first idle worker. Safe with preemption disabled */
823static struct worker *first_idle_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200824{
Tejun Heo63d95a92012-07-12 14:46:37 -0700825 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200826 return NULL;
827
Tejun Heo63d95a92012-07-12 14:46:37 -0700828 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200829}
830
831/**
832 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700833 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200834 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700835 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200836 *
837 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800838 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200839 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700840static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200841{
Lai Jiangshan1037de32014-05-22 16:44:07 +0800842 struct worker *worker = first_idle_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200843
844 if (likely(worker))
845 wake_up_process(worker->task);
846}
847
Tejun Heo4690c4a2010-06-29 10:07:10 +0200848/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200849 * wq_worker_waking_up - a worker is waking up
850 * @task: task waking up
851 * @cpu: CPU @task is waking up to
852 *
853 * This function is called during try_to_wake_up() when a worker is
854 * being awoken.
855 *
856 * CONTEXT:
857 * spin_lock_irq(rq->lock)
858 */
Tejun Heod84ff052013-03-12 11:29:59 -0700859void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200860{
861 struct worker *worker = kthread_data(task);
862
Joonsoo Kim36576002012-10-26 23:03:49 +0900863 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800864 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800865 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900866 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200867}
868
869/**
870 * wq_worker_sleeping - a worker is going to sleep
871 * @task: task going to sleep
872 * @cpu: CPU in question, must be the current CPU number
873 *
874 * This function is called during schedule() when a busy worker is
875 * going to sleep. Worker on the same cpu can be woken up by
876 * returning pointer to its task.
877 *
878 * CONTEXT:
879 * spin_lock_irq(rq->lock)
880 *
Yacine Belkadid185af32013-07-31 14:59:24 -0700881 * Return:
Tejun Heoe22bee72010-06-29 10:07:14 +0200882 * Worker task on @cpu to wake up, %NULL if none.
883 */
Tejun Heod84ff052013-03-12 11:29:59 -0700884struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200885{
886 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800887 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200888
Tejun Heo111c2252013-01-17 17:16:24 -0800889 /*
890 * Rescuers, which may not have all the fields set up like normal
891 * workers, also reach here, let's not access anything before
892 * checking NOT_RUNNING.
893 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500894 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200895 return NULL;
896
Tejun Heo111c2252013-01-17 17:16:24 -0800897 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800898
Tejun Heoe22bee72010-06-29 10:07:14 +0200899 /* this can only happen on the local cpu */
Lai Jiangshan92b69f52014-06-03 15:33:08 +0800900 if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
Tejun Heo6183c002013-03-12 11:29:57 -0700901 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200902
903 /*
904 * The counterpart of the following dec_and_test, implied mb,
905 * worklist not empty test sequence is in insert_work().
906 * Please read comment there.
907 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700908 * NOT_RUNNING is clear. This means that we're bound to and
909 * running on the local cpu w/ rq lock held and preemption
910 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800911 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700912 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200913 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800914 if (atomic_dec_and_test(&pool->nr_running) &&
915 !list_empty(&pool->worklist))
Lai Jiangshan1037de32014-05-22 16:44:07 +0800916 to_wakeup = first_idle_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200917 return to_wakeup ? to_wakeup->task : NULL;
918}
919
920/**
921 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200922 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200923 * @flags: flags to set
Tejun Heod302f012010-06-29 10:07:13 +0200924 *
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800925 * Set @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200926 *
Tejun Heocb444762010-07-02 10:03:50 +0200927 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800928 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200929 */
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800930static inline void worker_set_flags(struct worker *worker, unsigned int flags)
Tejun Heod302f012010-06-29 10:07:13 +0200931{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700932 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200933
Tejun Heocb444762010-07-02 10:03:50 +0200934 WARN_ON_ONCE(worker->task != current);
935
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800936 /* If transitioning into NOT_RUNNING, adjust nr_running. */
Tejun Heoe22bee72010-06-29 10:07:14 +0200937 if ((flags & WORKER_NOT_RUNNING) &&
938 !(worker->flags & WORKER_NOT_RUNNING)) {
Lai Jiangshan228f1d02014-07-22 13:02:00 +0800939 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200940 }
941
Tejun Heod302f012010-06-29 10:07:13 +0200942 worker->flags |= flags;
943}
944
945/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200946 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200947 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200948 * @flags: flags to clear
949 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200950 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200951 *
Tejun Heocb444762010-07-02 10:03:50 +0200952 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800953 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200954 */
955static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
956{
Tejun Heo63d95a92012-07-12 14:46:37 -0700957 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200958 unsigned int oflags = worker->flags;
959
Tejun Heocb444762010-07-02 10:03:50 +0200960 WARN_ON_ONCE(worker->task != current);
961
Tejun Heod302f012010-06-29 10:07:13 +0200962 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200963
Tejun Heo42c025f2011-01-11 15:58:49 +0100964 /*
965 * If transitioning out of NOT_RUNNING, increment nr_running. Note
966 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
967 * of multiple flags, not a single flag.
968 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200969 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
970 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800971 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200972}
973
974/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200975 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800976 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200977 * @work: work to find worker for
978 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800979 * Find a worker which is executing @work on @pool by searching
980 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800981 * to match, its current execution should match the address of @work and
982 * its work function. This is to avoid unwanted dependency between
983 * unrelated work executions through a work item being recycled while still
984 * being executed.
985 *
986 * This is a bit tricky. A work item may be freed once its execution
987 * starts and nothing prevents the freed area from being recycled for
988 * another work item. If the same work item address ends up being reused
989 * before the original execution finishes, workqueue will identify the
990 * recycled work item as currently executing and make it wait until the
991 * current execution finishes, introducing an unwanted dependency.
992 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700993 * This function checks the work item address and work function to avoid
994 * false positives. Note that this isn't complete as one may construct a
995 * work function which can introduce dependency onto itself through a
996 * recycled work item. Well, if somebody wants to shoot oneself in the
997 * foot that badly, there's only so much we can do, and if such deadlock
998 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200999 *
1000 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001001 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001002 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001003 * Return:
1004 * Pointer to worker which is executing @work if found, %NULL
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001005 * otherwise.
1006 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001007static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001008 struct work_struct *work)
1009{
Sasha Levin42f85702012-12-17 10:01:23 -05001010 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -05001011
Sasha Levinb67bfe02013-02-27 17:06:00 -08001012 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -08001013 (unsigned long)work)
1014 if (worker->current_work == work &&
1015 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -05001016 return worker;
1017
1018 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001019}
1020
1021/**
Tejun Heobf4ede02012-08-03 10:30:46 -07001022 * move_linked_works - move linked works to a list
1023 * @work: start of series of works to be scheduled
1024 * @head: target list to append @work to
Shailendra Verma402dd892015-05-23 10:38:14 +05301025 * @nextp: out parameter for nested worklist walking
Tejun Heobf4ede02012-08-03 10:30:46 -07001026 *
1027 * Schedule linked works starting from @work to @head. Work series to
1028 * be scheduled starts at @work and includes any consecutive work with
1029 * WORK_STRUCT_LINKED set in its predecessor.
1030 *
1031 * If @nextp is not NULL, it's updated to point to the next work of
1032 * the last scheduled work. This allows move_linked_works() to be
1033 * nested inside outer list_for_each_entry_safe().
1034 *
1035 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001036 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001037 */
1038static void move_linked_works(struct work_struct *work, struct list_head *head,
1039 struct work_struct **nextp)
1040{
1041 struct work_struct *n;
1042
1043 /*
1044 * Linked worklist will always end before the end of the list,
1045 * use NULL for list head.
1046 */
1047 list_for_each_entry_safe_from(work, n, NULL, entry) {
1048 list_move_tail(&work->entry, head);
1049 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1050 break;
1051 }
1052
1053 /*
1054 * If we're already inside safe list traversal and have moved
1055 * multiple works to the scheduled queue, the next position
1056 * needs to be updated.
1057 */
1058 if (nextp)
1059 *nextp = n;
1060}
1061
Tejun Heo8864b4e2013-03-12 11:30:04 -07001062/**
1063 * get_pwq - get an extra reference on the specified pool_workqueue
1064 * @pwq: pool_workqueue to get
1065 *
1066 * Obtain an extra reference on @pwq. The caller should guarantee that
1067 * @pwq has positive refcnt and be holding the matching pool->lock.
1068 */
1069static void get_pwq(struct pool_workqueue *pwq)
1070{
1071 lockdep_assert_held(&pwq->pool->lock);
1072 WARN_ON_ONCE(pwq->refcnt <= 0);
1073 pwq->refcnt++;
1074}
1075
1076/**
1077 * put_pwq - put a pool_workqueue reference
1078 * @pwq: pool_workqueue to put
1079 *
1080 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1081 * destruction. The caller should be holding the matching pool->lock.
1082 */
1083static void put_pwq(struct pool_workqueue *pwq)
1084{
1085 lockdep_assert_held(&pwq->pool->lock);
1086 if (likely(--pwq->refcnt))
1087 return;
1088 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1089 return;
1090 /*
1091 * @pwq can't be released under pool->lock, bounce to
1092 * pwq_unbound_release_workfn(). This never recurses on the same
1093 * pool->lock as this path is taken only for unbound workqueues and
1094 * the release work item is scheduled on a per-cpu workqueue. To
1095 * avoid lockdep warning, unbound pool->locks are given lockdep
1096 * subclass of 1 in get_unbound_pool().
1097 */
1098 schedule_work(&pwq->unbound_release_work);
1099}
1100
Tejun Heodce90d42013-04-01 11:23:35 -07001101/**
1102 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1103 * @pwq: pool_workqueue to put (can be %NULL)
1104 *
1105 * put_pwq() with locking. This function also allows %NULL @pwq.
1106 */
1107static void put_pwq_unlocked(struct pool_workqueue *pwq)
1108{
1109 if (pwq) {
1110 /*
1111 * As both pwqs and pools are sched-RCU protected, the
1112 * following lock operations are safe.
1113 */
1114 spin_lock_irq(&pwq->pool->lock);
1115 put_pwq(pwq);
1116 spin_unlock_irq(&pwq->pool->lock);
1117 }
1118}
1119
Tejun Heo112202d2013-02-13 19:29:12 -08001120static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001121{
Tejun Heo112202d2013-02-13 19:29:12 -08001122 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001123
1124 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001125 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001126 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001127 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001128}
1129
Tejun Heo112202d2013-02-13 19:29:12 -08001130static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001131{
Tejun Heo112202d2013-02-13 19:29:12 -08001132 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001133 struct work_struct, entry);
1134
Tejun Heo112202d2013-02-13 19:29:12 -08001135 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001136}
1137
Tejun Heobf4ede02012-08-03 10:30:46 -07001138/**
Tejun Heo112202d2013-02-13 19:29:12 -08001139 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1140 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001141 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001142 *
1143 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001144 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001145 *
1146 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001147 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001148 */
Tejun Heo112202d2013-02-13 19:29:12 -08001149static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001150{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001151 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001152 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001153 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001154
Tejun Heo112202d2013-02-13 19:29:12 -08001155 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001156
Tejun Heo112202d2013-02-13 19:29:12 -08001157 pwq->nr_active--;
1158 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001159 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001160 if (pwq->nr_active < pwq->max_active)
1161 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001162 }
1163
1164 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001165 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001166 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001167
1168 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001169 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001170 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001171
Tejun Heo112202d2013-02-13 19:29:12 -08001172 /* this pwq is done, clear flush_color */
1173 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001174
1175 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001176 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001177 * will handle the rest.
1178 */
Tejun Heo112202d2013-02-13 19:29:12 -08001179 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1180 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001181out_put:
1182 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001183}
1184
Tejun Heo36e227d2012-08-03 10:30:46 -07001185/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001186 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001187 * @work: work item to steal
1188 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001189 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001190 *
1191 * Try to grab PENDING bit of @work. This function can handle @work in any
Yacine Belkadid185af32013-07-31 14:59:24 -07001192 * stable state - idle, on timer or on worklist.
Tejun Heo36e227d2012-08-03 10:30:46 -07001193 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001194 * Return:
Tejun Heo36e227d2012-08-03 10:30:46 -07001195 * 1 if @work was pending and we successfully stole PENDING
1196 * 0 if @work was idle and we claimed PENDING
1197 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001198 * -ENOENT if someone else is canceling @work, this state may persist
1199 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001200 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001201 * Note:
Tejun Heobbb68df2012-08-03 10:30:46 -07001202 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001203 * interrupted while holding PENDING and @work off queue, irq must be
1204 * disabled on entry. This, combined with delayed_work->timer being
1205 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001206 *
1207 * On successful return, >= 0, irq is disabled and the caller is
1208 * responsible for releasing it using local_irq_restore(*@flags).
1209 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001210 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001211 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001212static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1213 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001214{
Tejun Heod565ed62013-01-24 11:01:33 -08001215 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001216 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001217
Tejun Heobbb68df2012-08-03 10:30:46 -07001218 local_irq_save(*flags);
1219
Tejun Heo36e227d2012-08-03 10:30:46 -07001220 /* try to steal the timer if it exists */
1221 if (is_dwork) {
1222 struct delayed_work *dwork = to_delayed_work(work);
1223
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001224 /*
1225 * dwork->timer is irqsafe. If del_timer() fails, it's
1226 * guaranteed that the timer is not queued anywhere and not
1227 * running on the local CPU.
1228 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001229 if (likely(del_timer(&dwork->timer)))
1230 return 1;
1231 }
1232
1233 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001234 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1235 return 0;
1236
1237 /*
1238 * The queueing is in progress, or it is already queued. Try to
1239 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1240 */
Tejun Heod565ed62013-01-24 11:01:33 -08001241 pool = get_work_pool(work);
1242 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001243 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001244
Tejun Heod565ed62013-01-24 11:01:33 -08001245 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001246 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001247 * work->data is guaranteed to point to pwq only while the work
1248 * item is queued on pwq->wq, and both updating work->data to point
1249 * to pwq on queueing and to pool on dequeueing are done under
1250 * pwq->pool->lock. This in turn guarantees that, if work->data
1251 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001252 * item is currently queued on that pool.
1253 */
Tejun Heo112202d2013-02-13 19:29:12 -08001254 pwq = get_work_pwq(work);
1255 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001256 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001257
Tejun Heo16062832013-02-06 18:04:53 -08001258 /*
1259 * A delayed work item cannot be grabbed directly because
1260 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001261 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001262 * management later on and cause stall. Make sure the work
1263 * item is activated before grabbing.
1264 */
1265 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001266 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001267
Tejun Heo16062832013-02-06 18:04:53 -08001268 list_del_init(&work->entry);
Lai Jiangshan9c34a702014-07-11 00:11:13 +08001269 pwq_dec_nr_in_flight(pwq, get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001270
Tejun Heo112202d2013-02-13 19:29:12 -08001271 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001272 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001273
Tejun Heo16062832013-02-06 18:04:53 -08001274 spin_unlock(&pool->lock);
1275 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001276 }
Tejun Heod565ed62013-01-24 11:01:33 -08001277 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001278fail:
1279 local_irq_restore(*flags);
1280 if (work_is_canceling(work))
1281 return -ENOENT;
1282 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001283 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001284}
1285
1286/**
Tejun Heo706026c2013-01-24 11:01:34 -08001287 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001288 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001289 * @work: work to insert
1290 * @head: insertion point
1291 * @extra_flags: extra WORK_STRUCT_* flags to set
1292 *
Tejun Heo112202d2013-02-13 19:29:12 -08001293 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001294 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001295 *
1296 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001297 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001298 */
Tejun Heo112202d2013-02-13 19:29:12 -08001299static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1300 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001301{
Tejun Heo112202d2013-02-13 19:29:12 -08001302 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001303
Tejun Heo4690c4a2010-06-29 10:07:10 +02001304 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001305 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001306 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001307 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001308
1309 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001310 * Ensure either wq_worker_sleeping() sees the above
1311 * list_add_tail() or we see zero nr_running to avoid workers lying
1312 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001313 */
1314 smp_mb();
1315
Tejun Heo63d95a92012-07-12 14:46:37 -07001316 if (__need_more_worker(pool))
1317 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001318}
1319
Tejun Heoc8efcc22010-12-20 19:32:04 +01001320/*
1321 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001322 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001323 */
1324static bool is_chained_work(struct workqueue_struct *wq)
1325{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001326 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001327
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001328 worker = current_wq_worker();
1329 /*
1330 * Return %true iff I'm a worker execuing a work item on @wq. If
1331 * I'm @worker, it's safe to dereference it without locking.
1332 */
Tejun Heo112202d2013-02-13 19:29:12 -08001333 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001334}
1335
Tejun Heod84ff052013-03-12 11:29:59 -07001336static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct work_struct *work)
1338{
Tejun Heo112202d2013-02-13 19:29:12 -08001339 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001340 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001341 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001342 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001343 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001344
1345 /*
1346 * While a work item is PENDING && off queue, a task trying to
1347 * steal the PENDING will busy-loop waiting for it to either get
1348 * queued or lose PENDING. Grabbing PENDING and queueing should
1349 * happen with IRQ disabled.
1350 */
1351 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001353 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001354
Li Bin9ef28a72013-09-09 13:13:58 +08001355 /* if draining, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001356 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001357 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001358 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001359retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001360 if (req_cpu == WORK_CPU_UNBOUND)
1361 cpu = raw_smp_processor_id();
1362
Tejun Heoc9178082013-03-12 11:30:04 -07001363 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001364 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001365 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001366 else
1367 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001368
Tejun Heoc9178082013-03-12 11:30:04 -07001369 /*
1370 * If @work was previously on a different pool, it might still be
1371 * running there, in which case the work needs to be queued on that
1372 * pool to guarantee non-reentrancy.
1373 */
1374 last_pool = get_work_pool(work);
1375 if (last_pool && last_pool != pwq->pool) {
1376 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001377
Tejun Heoc9178082013-03-12 11:30:04 -07001378 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001379
Tejun Heoc9178082013-03-12 11:30:04 -07001380 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001381
Tejun Heoc9178082013-03-12 11:30:04 -07001382 if (worker && worker->current_pwq->wq == wq) {
1383 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001384 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001385 /* meh... not running there, queue here */
1386 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001387 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001388 }
Tejun Heof3421792010-07-02 10:03:51 +02001389 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001390 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001391 }
1392
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001393 /*
1394 * pwq is determined and locked. For unbound pools, we could have
1395 * raced with pwq release and it could already be dead. If its
1396 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001397 * without another pwq replacing it in the numa_pwq_tbl or while
1398 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001399 * make forward-progress.
1400 */
1401 if (unlikely(!pwq->refcnt)) {
1402 if (wq->flags & WQ_UNBOUND) {
1403 spin_unlock(&pwq->pool->lock);
1404 cpu_relax();
1405 goto retry;
1406 }
1407 /* oops */
1408 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1409 wq->name, cpu);
1410 }
1411
Tejun Heo112202d2013-02-13 19:29:12 -08001412 /* pwq determined, queue */
1413 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001414
Dan Carpenterf5b25522012-04-13 22:06:58 +03001415 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001416 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001417 return;
1418 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001419
Tejun Heo112202d2013-02-13 19:29:12 -08001420 pwq->nr_in_flight[pwq->work_color]++;
1421 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001422
Tejun Heo112202d2013-02-13 19:29:12 -08001423 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001424 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001425 pwq->nr_active++;
1426 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001427 } else {
1428 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001429 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001430 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001431
Tejun Heo112202d2013-02-13 19:29:12 -08001432 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001433
Tejun Heo112202d2013-02-13 19:29:12 -08001434 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001437/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001438 * queue_work_on - queue work on specific cpu
1439 * @cpu: CPU number to execute work on
1440 * @wq: workqueue to use
1441 * @work: work to queue
1442 *
Zhang Ruic1a220e2008-07-23 21:28:39 -07001443 * We queue the work to a specific CPU, the caller must ensure it
1444 * can't go away.
Yacine Belkadid185af32013-07-31 14:59:24 -07001445 *
1446 * Return: %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001447 */
Tejun Heod4283e92012-08-03 10:30:44 -07001448bool queue_work_on(int cpu, struct workqueue_struct *wq,
1449 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001450{
Tejun Heod4283e92012-08-03 10:30:44 -07001451 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001452 unsigned long flags;
1453
1454 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001455
Tejun Heo22df02b2010-06-29 10:07:10 +02001456 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001457 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001458 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001459 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001460
1461 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001462 return ret;
1463}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001464EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001465
Tejun Heod8e794d2012-08-03 10:30:45 -07001466void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
David Howells52bad642006-11-22 14:54:01 +00001468 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001470 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001471 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001473EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001475static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1476 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001478 struct timer_list *timer = &dwork->timer;
1479 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001481 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1482 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001483 WARN_ON_ONCE(timer_pending(timer));
1484 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001485
Tejun Heo8852aac2012-12-01 16:23:42 -08001486 /*
1487 * If @delay is 0, queue @dwork->work immediately. This is for
1488 * both optimization and correctness. The earliest @timer can
1489 * expire is on the closest next tick and delayed_work users depend
1490 * on that there's no such delay when @delay is 0.
1491 */
1492 if (!delay) {
1493 __queue_work(cpu, wq, &dwork->work);
1494 return;
1495 }
1496
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001497 timer_stats_timer_set_start_info(&dwork->timer);
1498
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001499 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001500 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001501 timer->expires = jiffies + delay;
1502
Tejun Heo66847102016-02-09 16:11:26 -05001503 if (unlikely(cpu != WORK_CPU_UNBOUND))
1504 add_timer_on(timer, cpu);
1505 else
1506 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001509/**
1510 * queue_delayed_work_on - queue work on specific CPU after delay
1511 * @cpu: CPU number to execute work on
1512 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001513 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001514 * @delay: number of jiffies to wait before queueing
1515 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001516 * Return: %false if @work was already on a queue, %true otherwise. If
Tejun Heo715f1302012-08-03 10:30:46 -07001517 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1518 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001519 */
Tejun Heod4283e92012-08-03 10:30:44 -07001520bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1521 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001522{
David Howells52bad642006-11-22 14:54:01 +00001523 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001524 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001525 unsigned long flags;
1526
1527 /* read the comment in __queue_work() */
1528 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001529
Tejun Heo22df02b2010-06-29 10:07:10 +02001530 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001531 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001532 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001533 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001534
1535 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001536 return ret;
1537}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001538EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Tejun Heoc8e55f32010-06-29 10:07:12 +02001540/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001541 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1542 * @cpu: CPU number to execute work on
1543 * @wq: workqueue to use
1544 * @dwork: work to queue
1545 * @delay: number of jiffies to wait before queueing
1546 *
1547 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1548 * modify @dwork's timer so that it expires after @delay. If @delay is
1549 * zero, @work is guaranteed to be scheduled immediately regardless of its
1550 * current state.
1551 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001552 * Return: %false if @dwork was idle and queued, %true if @dwork was
Tejun Heo8376fe22012-08-03 10:30:47 -07001553 * pending and its timer was modified.
1554 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001555 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001556 * See try_to_grab_pending() for details.
1557 */
1558bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1559 struct delayed_work *dwork, unsigned long delay)
1560{
1561 unsigned long flags;
1562 int ret;
1563
1564 do {
1565 ret = try_to_grab_pending(&dwork->work, true, &flags);
1566 } while (unlikely(ret == -EAGAIN));
1567
1568 if (likely(ret >= 0)) {
1569 __queue_delayed_work(cpu, wq, dwork, delay);
1570 local_irq_restore(flags);
1571 }
1572
1573 /* -ENOENT from try_to_grab_pending() becomes %true */
1574 return ret;
1575}
1576EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1577
1578/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001579 * worker_enter_idle - enter idle state
1580 * @worker: worker which is entering idle state
1581 *
1582 * @worker is entering idle state. Update stats and idle timer if
1583 * necessary.
1584 *
1585 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001586 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001587 */
1588static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001590 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Tejun Heo6183c002013-03-12 11:29:57 -07001592 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1593 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1594 (worker->hentry.next || worker->hentry.pprev)))
1595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Lai Jiangshan051e1852014-07-22 13:03:02 +08001597 /* can't use worker_set_flags(), also called from create_worker() */
Tejun Heocb444762010-07-02 10:03:50 +02001598 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001599 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001601
Tejun Heoc8e55f32010-06-29 10:07:12 +02001602 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001603 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001604
Tejun Heo628c78e2012-07-17 12:39:27 -07001605 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1606 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001607
Tejun Heo544ecf32012-05-14 15:04:50 -07001608 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001609 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001610 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001611 * nr_running, the warning may trigger spuriously. Check iff
1612 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001613 */
Tejun Heo24647572013-01-24 11:01:33 -08001614 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001615 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001616 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Tejun Heoc8e55f32010-06-29 10:07:12 +02001619/**
1620 * worker_leave_idle - leave idle state
1621 * @worker: worker which is leaving idle state
1622 *
1623 * @worker is leaving idle state. Update stats.
1624 *
1625 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001626 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001627 */
1628static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001630 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Tejun Heo6183c002013-03-12 11:29:57 -07001632 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1633 return;
Tejun Heod302f012010-06-29 10:07:13 +02001634 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001635 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001636 list_del_init(&worker->entry);
1637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001639static struct worker *alloc_worker(int node)
Tejun Heoc34056a2010-06-29 10:07:11 +02001640{
1641 struct worker *worker;
1642
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001643 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001644 if (worker) {
1645 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001646 INIT_LIST_HEAD(&worker->scheduled);
Lai Jiangshanda028462014-05-20 17:46:31 +08001647 INIT_LIST_HEAD(&worker->node);
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 /* on creation a worker is in !idle && prep state */
1649 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001650 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001651 return worker;
1652}
1653
1654/**
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001655 * worker_attach_to_pool() - attach a worker to a pool
1656 * @worker: worker to be attached
1657 * @pool: the target pool
1658 *
1659 * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
1660 * cpu-binding of @worker are kept coordinated with the pool across
1661 * cpu-[un]hotplugs.
1662 */
1663static void worker_attach_to_pool(struct worker *worker,
1664 struct worker_pool *pool)
1665{
1666 mutex_lock(&pool->attach_mutex);
1667
1668 /*
1669 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1670 * online CPUs. It'll be re-applied when any of the CPUs come up.
1671 */
1672 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1673
1674 /*
1675 * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
1676 * stable across this function. See the comments above the
1677 * flag definition for details.
1678 */
1679 if (pool->flags & POOL_DISASSOCIATED)
1680 worker->flags |= WORKER_UNBOUND;
1681
1682 list_add_tail(&worker->node, &pool->workers);
1683
1684 mutex_unlock(&pool->attach_mutex);
1685}
1686
1687/**
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001688 * worker_detach_from_pool() - detach a worker from its pool
1689 * @worker: worker which is attached to its pool
1690 * @pool: the pool @worker is attached to
1691 *
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001692 * Undo the attaching which had been done in worker_attach_to_pool(). The
1693 * caller worker shouldn't access to the pool after detached except it has
1694 * other reference to the pool.
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001695 */
1696static void worker_detach_from_pool(struct worker *worker,
1697 struct worker_pool *pool)
1698{
1699 struct completion *detach_completion = NULL;
1700
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001701 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08001702 list_del(&worker->node);
1703 if (list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001704 detach_completion = pool->detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08001705 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001706
Lai Jiangshanb62c0752014-06-03 15:32:52 +08001707 /* clear leftover flags without pool->lock after it is detached */
1708 worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
1709
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001710 if (detach_completion)
1711 complete(detach_completion);
1712}
1713
1714/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001715 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001716 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001717 *
Lai Jiangshan051e1852014-07-22 13:03:02 +08001718 * Create and start a new worker which is attached to @pool.
Tejun Heoc34056a2010-06-29 10:07:11 +02001719 *
1720 * CONTEXT:
1721 * Might sleep. Does GFP_KERNEL allocations.
1722 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001723 * Return:
Tejun Heoc34056a2010-06-29 10:07:11 +02001724 * Pointer to the newly created worker.
1725 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001726static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001727{
Tejun Heoc34056a2010-06-29 10:07:11 +02001728 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001729 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001730 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001731
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001732 /* ID is needed to determine kthread name */
1733 id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL);
Tejun Heo822d8402013-03-19 13:45:21 -07001734 if (id < 0)
1735 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001736
Lai Jiangshanf7537df2014-07-15 17:24:15 +08001737 worker = alloc_worker(pool->node);
Tejun Heoc34056a2010-06-29 10:07:11 +02001738 if (!worker)
1739 goto fail;
1740
Tejun Heobd7bdd42012-07-12 14:46:37 -07001741 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001742 worker->id = id;
1743
Tejun Heo29c91e92013-03-12 11:30:03 -07001744 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001745 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1746 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001747 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001748 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1749
Tejun Heof3f90ad2013-04-01 11:23:34 -07001750 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001751 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001752 if (IS_ERR(worker->task))
1753 goto fail;
1754
Oleg Nesterov91151222013-11-14 12:56:18 +01001755 set_user_nice(worker->task, pool->attrs->nice);
Peter Zijlstra25834c72015-05-15 17:43:34 +02001756 kthread_bind_mask(worker->task, pool->attrs->cpumask);
Oleg Nesterov91151222013-11-14 12:56:18 +01001757
Lai Jiangshanda028462014-05-20 17:46:31 +08001758 /* successful, attach the worker to the pool */
Lai Jiangshan4736cbf2014-05-20 17:46:35 +08001759 worker_attach_to_pool(worker, pool);
Tejun Heo822d8402013-03-19 13:45:21 -07001760
Lai Jiangshan051e1852014-07-22 13:03:02 +08001761 /* start the newly created worker */
1762 spin_lock_irq(&pool->lock);
1763 worker->pool->nr_workers++;
1764 worker_enter_idle(worker);
1765 wake_up_process(worker->task);
1766 spin_unlock_irq(&pool->lock);
1767
Tejun Heoc34056a2010-06-29 10:07:11 +02001768 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001769
Tejun Heoc34056a2010-06-29 10:07:11 +02001770fail:
Lai Jiangshan9625ab12014-05-20 17:46:27 +08001771 if (id >= 0)
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08001772 ida_simple_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 kfree(worker);
1774 return NULL;
1775}
1776
1777/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001778 * destroy_worker - destroy a workqueue worker
1779 * @worker: worker to be destroyed
1780 *
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001781 * Destroy @worker and adjust @pool stats accordingly. The worker should
1782 * be idle.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783 *
1784 * CONTEXT:
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001785 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001786 */
1787static void destroy_worker(struct worker *worker)
1788{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001789 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001790
Tejun Heocd549682013-03-13 19:47:39 -07001791 lockdep_assert_held(&pool->lock);
1792
Tejun Heoc34056a2010-06-29 10:07:11 +02001793 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001794 if (WARN_ON(worker->current_work) ||
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001795 WARN_ON(!list_empty(&worker->scheduled)) ||
1796 WARN_ON(!(worker->flags & WORKER_IDLE)))
Tejun Heo6183c002013-03-12 11:29:57 -07001797 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001798
Lai Jiangshan73eb7fe2014-05-20 17:46:28 +08001799 pool->nr_workers--;
1800 pool->nr_idle--;
Lai Jiangshan5bdfff92014-02-15 22:02:28 +08001801
Tejun Heoc8e55f32010-06-29 10:07:12 +02001802 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001803 worker->flags |= WORKER_DIE;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08001804 wake_up_process(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001805}
1806
Tejun Heo63d95a92012-07-12 14:46:37 -07001807static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001808{
Tejun Heo63d95a92012-07-12 14:46:37 -07001809 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001810
Tejun Heod565ed62013-01-24 11:01:33 -08001811 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001812
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001813 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001814 struct worker *worker;
1815 unsigned long expires;
1816
1817 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001818 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001819 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1820
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001821 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001822 mod_timer(&pool->idle_timer, expires);
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001823 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02001824 }
Lai Jiangshan3347fc92014-05-20 17:46:30 +08001825
1826 destroy_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02001827 }
1828
Tejun Heod565ed62013-01-24 11:01:33 -08001829 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001830}
1831
Tejun Heo493a1722013-03-12 11:29:59 -07001832static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001833{
Tejun Heo112202d2013-02-13 19:29:12 -08001834 struct pool_workqueue *pwq = get_work_pwq(work);
1835 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001836
Tejun Heo2e109a22013-03-13 19:47:40 -07001837 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001838
Tejun Heo493008a2013-03-12 11:30:03 -07001839 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001840 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001841
1842 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001843 if (list_empty(&pwq->mayday_node)) {
Lai Jiangshan77668c82014-04-18 11:04:16 -04001844 /*
1845 * If @pwq is for an unbound wq, its base ref may be put at
1846 * any time due to an attribute change. Pin @pwq until the
1847 * rescuer is done with it.
1848 */
1849 get_pwq(pwq);
Tejun Heo493a1722013-03-12 11:29:59 -07001850 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001851 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001852 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001853}
1854
Tejun Heo706026c2013-01-24 11:01:34 -08001855static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001856{
Tejun Heo63d95a92012-07-12 14:46:37 -07001857 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001858 struct work_struct *work;
1859
Tejun Heob2d82902014-12-08 12:39:16 -05001860 spin_lock_irq(&pool->lock);
1861 spin_lock(&wq_mayday_lock); /* for wq->maydays */
Tejun Heoe22bee72010-06-29 10:07:14 +02001862
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 /*
1865 * We've been trying to create a new worker but
1866 * haven't been successful. We might be hitting an
1867 * allocation deadlock. Send distress signals to
1868 * rescuers.
1869 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001870 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001871 send_mayday(work);
1872 }
1873
Tejun Heob2d82902014-12-08 12:39:16 -05001874 spin_unlock(&wq_mayday_lock);
1875 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001876
Tejun Heo63d95a92012-07-12 14:46:37 -07001877 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001878}
1879
1880/**
1881 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001882 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001883 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001884 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001885 * have at least one idle worker on return from this function. If
1886 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001887 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 * possible allocation deadlock.
1889 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001890 * On return, need_to_create_worker() is guaranteed to be %false and
1891 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001892 *
1893 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001894 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 * multiple times. Does GFP_KERNEL allocations. Called only from
1896 * manager.
Tejun Heoe22bee72010-06-29 10:07:14 +02001897 */
Tejun Heo29187a92015-01-16 14:21:16 -05001898static void maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001899__releases(&pool->lock)
1900__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001901{
Tejun Heoe22bee72010-06-29 10:07:14 +02001902restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001903 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001904
Tejun Heoe22bee72010-06-29 10:07:14 +02001905 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001906 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001907
1908 while (true) {
Lai Jiangshan051e1852014-07-22 13:03:02 +08001909 if (create_worker(pool) || !need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001910 break;
1911
Lai Jiangshane212f362014-06-03 15:32:17 +08001912 schedule_timeout_interruptible(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001913
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 break;
1916 }
1917
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001919 spin_lock_irq(&pool->lock);
Lai Jiangshan051e1852014-07-22 13:03:02 +08001920 /*
1921 * This is necessary even after a new worker was just successfully
1922 * created as @pool->lock was dropped and the new worker might have
1923 * already become busy.
1924 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001925 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001926 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001927}
1928
1929/**
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 * manage_workers - manage worker pool
1931 * @worker: self
1932 *
Tejun Heo706026c2013-01-24 11:01:34 -08001933 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001935 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 *
1937 * The caller can safely start processing works on false return. On
1938 * true return, it's guaranteed that need_to_create_worker() is false
1939 * and may_start_working() is true.
1940 *
1941 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001942 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001943 * multiple times. Does GFP_KERNEL allocations.
1944 *
Yacine Belkadid185af32013-07-31 14:59:24 -07001945 * Return:
Tejun Heo29187a92015-01-16 14:21:16 -05001946 * %false if the pool doesn't need management and the caller can safely
1947 * start processing works, %true if management function was performed and
1948 * the conditions that the caller verified before calling the function may
1949 * no longer be true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001950 */
1951static bool manage_workers(struct worker *worker)
1952{
Tejun Heo63d95a92012-07-12 14:46:37 -07001953 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001954
Tejun Heobc3a1af2013-03-13 19:47:39 -07001955 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07001956 * Anyone who successfully grabs manager_arb wins the arbitration
1957 * and becomes the manager. mutex_trylock() on pool->manager_arb
1958 * failure while holding pool->lock reliably indicates that someone
1959 * else is managing the pool and the worker which failed trylock
1960 * can proceed to executing work items. This means that anyone
1961 * grabbing manager_arb is responsible for actually performing
1962 * manager duties. If manager_arb is grabbed and released without
1963 * actual management, the pool may stall indefinitely.
Tejun Heobc3a1af2013-03-13 19:47:39 -07001964 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07001965 if (!mutex_trylock(&pool->manager_arb))
Tejun Heo29187a92015-01-16 14:21:16 -05001966 return false;
Tejun Heo2607d7a2015-03-09 09:22:28 -04001967 pool->manager = worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02001968
Tejun Heo29187a92015-01-16 14:21:16 -05001969 maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001970
Tejun Heo2607d7a2015-03-09 09:22:28 -04001971 pool->manager = NULL;
Tejun Heo34a06bd2013-03-12 11:30:00 -07001972 mutex_unlock(&pool->manager_arb);
Tejun Heo29187a92015-01-16 14:21:16 -05001973 return true;
Tejun Heoe22bee72010-06-29 10:07:14 +02001974}
1975
Tejun Heoa62428c2010-06-29 10:07:10 +02001976/**
1977 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001978 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001979 * @work: work to process
1980 *
1981 * Process @work. This function contains all the logics necessary to
1982 * process a single work including synchronization against and
1983 * interaction with other workers on the same cpu, queueing and
1984 * flushing. As long as context requirement is met, any worker can
1985 * call this function to process a work.
1986 *
1987 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001988 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001989 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001990static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08001991__releases(&pool->lock)
1992__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001993{
Tejun Heo112202d2013-02-13 19:29:12 -08001994 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001995 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001996 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001997 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001998 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001999#ifdef CONFIG_LOCKDEP
2000 /*
2001 * It is permissible to free the struct work_struct from
2002 * inside the function that is called from it, this we need to
2003 * take into account for lockdep too. To avoid bogus "held
2004 * lock freed" warnings as well as problems when looking into
2005 * work->lockdep_map, make a copy and use that here.
2006 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002007 struct lockdep_map lockdep_map;
2008
2009 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002010#endif
Lai Jiangshan807407c2014-06-03 15:33:28 +08002011 /* ensure we're on the correct CPU */
Lai Jiangshan85327af2014-06-03 15:33:28 +08002012 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002013 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002014
Tejun Heo7e116292010-06-29 10:07:13 +02002015 /*
2016 * A single work shouldn't be executed concurrently by
2017 * multiple workers on a single cpu. Check whether anyone is
2018 * already processing the work. If so, defer the work to the
2019 * currently executing one.
2020 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002021 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002022 if (unlikely(collision)) {
2023 move_linked_works(work, &collision->scheduled, NULL);
2024 return;
2025 }
2026
Tejun Heo8930cab2012-08-03 10:30:45 -07002027 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002028 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002029 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002030 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002031 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002032 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002033 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002034
Tejun Heoa62428c2010-06-29 10:07:10 +02002035 list_del_init(&work->entry);
2036
Tejun Heo649027d2010-06-29 10:07:14 +02002037 /*
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002038 * CPU intensive works don't participate in concurrency management.
2039 * They're the scheduler's responsibility. This takes @worker out
2040 * of concurrency management and the next code block will chain
2041 * execution of the pending work items.
Tejun Heofb0e7be2010-06-29 10:07:15 +02002042 */
2043 if (unlikely(cpu_intensive))
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002044 worker_set_flags(worker, WORKER_CPU_INTENSIVE);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002045
Tejun Heo974271c2012-07-12 14:46:37 -07002046 /*
Lai Jiangshana489a032014-07-22 13:01:59 +08002047 * Wake up another worker if necessary. The condition is always
2048 * false for normal per-cpu workers since nr_running would always
2049 * be >= 1 at this point. This is used to chain execution of the
2050 * pending work items for WORKER_NOT_RUNNING workers such as the
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002051 * UNBOUND and CPU_INTENSIVE ones.
Tejun Heo974271c2012-07-12 14:46:37 -07002052 */
Lai Jiangshana489a032014-07-22 13:01:59 +08002053 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002054 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002055
Tejun Heo8930cab2012-08-03 10:30:45 -07002056 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002057 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002058 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002059 * PENDING and queued state changes happen together while IRQ is
2060 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002061 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002062 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002063
Tejun Heod565ed62013-01-24 11:01:33 -08002064 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002065
Tejun Heo112202d2013-02-13 19:29:12 -08002066 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002067 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002068 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002069 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002070 /*
2071 * While we must be careful to not use "work" after this, the trace
2072 * point will only record its address.
2073 */
2074 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002075 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002076 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002077
2078 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002079 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2080 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002081 current->comm, preempt_count(), task_pid_nr(current),
2082 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002083 debug_show_held_locks(current);
2084 dump_stack();
2085 }
2086
Tejun Heob22ce272013-08-28 17:33:37 -04002087 /*
2088 * The following prevents a kworker from hogging CPU on !PREEMPT
2089 * kernels, where a requeueing work item waiting for something to
2090 * happen could deadlock with stop_machine as such work item could
2091 * indefinitely requeue itself while all other CPUs are trapped in
Joe Lawrence789cbbe2014-10-05 13:24:21 -04002092 * stop_machine. At the same time, report a quiescent RCU state so
2093 * the same condition doesn't freeze RCU.
Tejun Heob22ce272013-08-28 17:33:37 -04002094 */
Joe Lawrence3e28e372014-10-05 13:24:22 -04002095 cond_resched_rcu_qs();
Tejun Heob22ce272013-08-28 17:33:37 -04002096
Tejun Heod565ed62013-01-24 11:01:33 -08002097 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002098
Tejun Heofb0e7be2010-06-29 10:07:15 +02002099 /* clear cpu intensive status */
2100 if (unlikely(cpu_intensive))
2101 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2102
Tejun Heoa62428c2010-06-29 10:07:10 +02002103 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002104 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002105 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002106 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002107 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002108 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002109 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002110}
2111
Tejun Heoaffee4b2010-06-29 10:07:12 +02002112/**
2113 * process_scheduled_works - process scheduled works
2114 * @worker: self
2115 *
2116 * Process all scheduled works. Please note that the scheduled list
2117 * may change while processing a work, so this function repeatedly
2118 * fetches a work from the top and executes it.
2119 *
2120 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002121 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002122 * multiple times.
2123 */
2124static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002126 while (!list_empty(&worker->scheduled)) {
2127 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002129 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Tejun Heo4690c4a2010-06-29 10:07:10 +02002133/**
2134 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002135 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002136 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002137 * The worker thread function. All workers belong to a worker_pool -
2138 * either a per-cpu one or dynamic unbound one. These workers process all
2139 * work items regardless of their specific target workqueue. The only
2140 * exception is work items which belong to workqueues with a rescuer which
2141 * will be explained in rescuer_thread().
Yacine Belkadid185af32013-07-31 14:59:24 -07002142 *
2143 * Return: 0
Tejun Heo4690c4a2010-06-29 10:07:10 +02002144 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002145static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Tejun Heoc34056a2010-06-29 10:07:11 +02002147 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002148 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Tejun Heoe22bee72010-06-29 10:07:14 +02002150 /* tell the scheduler that this is a workqueue worker */
2151 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002152woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002153 spin_lock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002154
Tejun Heoa9ab7752013-03-19 13:45:21 -07002155 /* am I supposed to die? */
2156 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002157 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002158 WARN_ON_ONCE(!list_empty(&worker->entry));
2159 worker->task->flags &= ~PF_WQ_WORKER;
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002160
2161 set_task_comm(worker->task, "kworker/dying");
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08002162 ida_simple_remove(&pool->worker_ida, worker->id);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08002163 worker_detach_from_pool(worker, pool);
2164 kfree(worker);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002167
Tejun Heoc8e55f32010-06-29 10:07:12 +02002168 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002169recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002170 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002171 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002172 goto sleep;
2173
2174 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002175 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002176 goto recheck;
2177
Tejun Heoc8e55f32010-06-29 10:07:12 +02002178 /*
2179 * ->scheduled list can only be filled while a worker is
2180 * preparing to process a work or actually processing it.
2181 * Make sure nobody diddled with it while I was sleeping.
2182 */
Tejun Heo6183c002013-03-12 11:29:57 -07002183 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002184
Tejun Heoe22bee72010-06-29 10:07:14 +02002185 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002186 * Finish PREP stage. We're guaranteed to have at least one idle
2187 * worker or that someone else has already assumed the manager
2188 * role. This is where @worker starts participating in concurrency
2189 * management if applicable and concurrency management is restored
2190 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002191 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002192 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002193
2194 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002195 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002196 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002197 struct work_struct, entry);
2198
2199 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2200 /* optimization path, not strictly necessary */
2201 process_one_work(worker, work);
2202 if (unlikely(!list_empty(&worker->scheduled)))
2203 process_scheduled_works(worker);
2204 } else {
2205 move_linked_works(work, &worker->scheduled, NULL);
2206 process_scheduled_works(worker);
2207 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002208 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002209
Lai Jiangshan228f1d02014-07-22 13:02:00 +08002210 worker_set_flags(worker, WORKER_PREP);
Tejun Heod313dd82010-07-02 10:03:51 +02002211sleep:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002212 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002213 * pool->lock is held and there's no work to process and no need to
2214 * manage, sleep. Workers are woken up only while holding
2215 * pool->lock or from local cpu, so setting the current state
2216 * before releasing pool->lock is enough to prevent losing any
2217 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002218 */
2219 worker_enter_idle(worker);
2220 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002221 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002222 schedule();
2223 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Tejun Heoe22bee72010-06-29 10:07:14 +02002226/**
2227 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002228 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002229 *
2230 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002231 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002232 *
Tejun Heo706026c2013-01-24 11:01:34 -08002233 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002234 * worker which uses GFP_KERNEL allocation which has slight chance of
2235 * developing into deadlock if some works currently on the same queue
2236 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2237 * the problem rescuer solves.
2238 *
Tejun Heo706026c2013-01-24 11:01:34 -08002239 * When such condition is possible, the pool summons rescuers of all
2240 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002241 * those works so that forward progress can be guaranteed.
2242 *
2243 * This should happen rarely.
Yacine Belkadid185af32013-07-31 14:59:24 -07002244 *
2245 * Return: 0
Tejun Heoe22bee72010-06-29 10:07:14 +02002246 */
Tejun Heo111c2252013-01-17 17:16:24 -08002247static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002248{
Tejun Heo111c2252013-01-17 17:16:24 -08002249 struct worker *rescuer = __rescuer;
2250 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002251 struct list_head *scheduled = &rescuer->scheduled;
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002252 bool should_stop;
Tejun Heoe22bee72010-06-29 10:07:14 +02002253
2254 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002255
2256 /*
2257 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2258 * doesn't participate in concurrency management.
2259 */
2260 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002261repeat:
2262 set_current_state(TASK_INTERRUPTIBLE);
2263
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002264 /*
2265 * By the time the rescuer is requested to stop, the workqueue
2266 * shouldn't have any work pending, but @wq->maydays may still have
2267 * pwq(s) queued. This can happen by non-rescuer workers consuming
2268 * all the work items before the rescuer got to them. Go through
2269 * @wq->maydays processing before acting on should_stop so that the
2270 * list is always empty on exit.
2271 */
2272 should_stop = kthread_should_stop();
Tejun Heoe22bee72010-06-29 10:07:14 +02002273
Tejun Heo493a1722013-03-12 11:29:59 -07002274 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002275 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002276
2277 while (!list_empty(&wq->maydays)) {
2278 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2279 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002280 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002281 struct work_struct *work, *n;
2282
2283 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002284 list_del_init(&pwq->mayday_node);
2285
Tejun Heo2e109a22013-03-13 19:47:40 -07002286 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002287
Lai Jiangshan51697d392014-05-20 17:46:36 +08002288 worker_attach_to_pool(rescuer, pool);
2289
2290 spin_lock_irq(&pool->lock);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002291 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002292
2293 /*
2294 * Slurp in all works issued via this workqueue and
2295 * process'em.
2296 */
Tejun Heo0479c8c2014-12-04 10:14:13 -05002297 WARN_ON_ONCE(!list_empty(scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002298 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002299 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002300 move_linked_works(work, scheduled, &n);
2301
NeilBrown008847f2014-12-08 12:39:16 -05002302 if (!list_empty(scheduled)) {
2303 process_scheduled_works(rescuer);
2304
2305 /*
2306 * The above execution of rescued work items could
2307 * have created more to rescue through
2308 * pwq_activate_first_delayed() or chained
2309 * queueing. Let's put @pwq back on mayday list so
2310 * that such back-to-back work items, which may be
2311 * being used to relieve memory pressure, don't
2312 * incur MAYDAY_INTERVAL delay inbetween.
2313 */
2314 if (need_to_create_worker(pool)) {
2315 spin_lock(&wq_mayday_lock);
2316 get_pwq(pwq);
2317 list_move_tail(&pwq->mayday_node, &wq->maydays);
2318 spin_unlock(&wq_mayday_lock);
2319 }
2320 }
Tejun Heo75769582011-02-14 14:04:46 +01002321
2322 /*
Lai Jiangshan77668c82014-04-18 11:04:16 -04002323 * Put the reference grabbed by send_mayday(). @pool won't
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002324 * go away while we're still attached to it.
Lai Jiangshan77668c82014-04-18 11:04:16 -04002325 */
2326 put_pwq(pwq);
2327
2328 /*
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002329 * Leave this pool. If need_more_worker() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002330 * regular worker; otherwise, we end up with 0 concurrency
2331 * and stalling the execution.
2332 */
Lai Jiangshand8ca83e2014-07-16 14:56:36 +08002333 if (need_more_worker(pool))
Tejun Heo63d95a92012-07-12 14:46:37 -07002334 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002335
Lai Jiangshanb3104102013-02-19 12:17:02 -08002336 rescuer->pool = NULL;
Lai Jiangshan13b1d622014-07-22 13:03:47 +08002337 spin_unlock_irq(&pool->lock);
2338
2339 worker_detach_from_pool(rescuer, pool);
2340
2341 spin_lock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002342 }
2343
Tejun Heo2e109a22013-03-13 19:47:40 -07002344 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002345
Lai Jiangshan4d595b82014-04-18 11:04:16 -04002346 if (should_stop) {
2347 __set_current_state(TASK_RUNNING);
2348 rescuer->task->flags &= ~PF_WQ_WORKER;
2349 return 0;
2350 }
2351
Tejun Heo111c2252013-01-17 17:16:24 -08002352 /* rescuers should never participate in concurrency management */
2353 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002354 schedule();
2355 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002358struct wq_barrier {
2359 struct work_struct work;
2360 struct completion done;
Tejun Heo2607d7a2015-03-09 09:22:28 -04002361 struct task_struct *task; /* purely informational */
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002362};
2363
2364static void wq_barrier_func(struct work_struct *work)
2365{
2366 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2367 complete(&barr->done);
2368}
2369
Tejun Heo4690c4a2010-06-29 10:07:10 +02002370/**
2371 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002372 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002373 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002374 * @target: target work to attach @barr to
2375 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002376 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002377 * @barr is linked to @target such that @barr is completed only after
2378 * @target finishes execution. Please note that the ordering
2379 * guarantee is observed only with respect to @target and on the local
2380 * cpu.
2381 *
2382 * Currently, a queued barrier can't be canceled. This is because
2383 * try_to_grab_pending() can't determine whether the work to be
2384 * grabbed is at the head of the queue and thus can't clear LINKED
2385 * flag of the previous work while there must be a valid next work
2386 * after a work with LINKED flag set.
2387 *
2388 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002389 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002390 *
2391 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002392 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002393 */
Tejun Heo112202d2013-02-13 19:29:12 -08002394static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002395 struct wq_barrier *barr,
2396 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002397{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002398 struct list_head *head;
2399 unsigned int linked = 0;
2400
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002401 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002402 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002403 * as we know for sure that this will not trigger any of the
2404 * checks and call back into the fixup functions where we
2405 * might deadlock.
2406 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002407 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002408 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002409 init_completion(&barr->done);
Tejun Heo2607d7a2015-03-09 09:22:28 -04002410 barr->task = current;
Oleg Nesterov83c22522007-05-09 02:33:54 -07002411
Tejun Heoaffee4b2010-06-29 10:07:12 +02002412 /*
2413 * If @target is currently being executed, schedule the
2414 * barrier to the worker; otherwise, put it after @target.
2415 */
2416 if (worker)
2417 head = worker->scheduled.next;
2418 else {
2419 unsigned long *bits = work_data_bits(target);
2420
2421 head = target->entry.next;
2422 /* there can already be other linked works, inherit and set */
2423 linked = *bits & WORK_STRUCT_LINKED;
2424 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2425 }
2426
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002427 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002428 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002429 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002430}
2431
Tejun Heo73f53c42010-06-29 10:07:11 +02002432/**
Tejun Heo112202d2013-02-13 19:29:12 -08002433 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002434 * @wq: workqueue being flushed
2435 * @flush_color: new flush color, < 0 for no-op
2436 * @work_color: new work color, < 0 for no-op
2437 *
Tejun Heo112202d2013-02-13 19:29:12 -08002438 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002439 *
Tejun Heo112202d2013-02-13 19:29:12 -08002440 * If @flush_color is non-negative, flush_color on all pwqs should be
2441 * -1. If no pwq has in-flight commands at the specified color, all
2442 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2443 * has in flight commands, its pwq->flush_color is set to
2444 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002445 * wakeup logic is armed and %true is returned.
2446 *
2447 * The caller should have initialized @wq->first_flusher prior to
2448 * calling this function with non-negative @flush_color. If
2449 * @flush_color is negative, no flush color update is done and %false
2450 * is returned.
2451 *
Tejun Heo112202d2013-02-13 19:29:12 -08002452 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002453 * work_color which is previous to @work_color and all will be
2454 * advanced to @work_color.
2455 *
2456 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002457 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002458 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002459 * Return:
Tejun Heo73f53c42010-06-29 10:07:11 +02002460 * %true if @flush_color >= 0 and there's something to flush. %false
2461 * otherwise.
2462 */
Tejun Heo112202d2013-02-13 19:29:12 -08002463static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002464 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Tejun Heo73f53c42010-06-29 10:07:11 +02002466 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002467 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002468
Tejun Heo73f53c42010-06-29 10:07:11 +02002469 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002470 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002471 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002472 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002473
Tejun Heo49e3cf42013-03-12 11:29:58 -07002474 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002475 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002476
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002477 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002478
2479 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002480 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002481
Tejun Heo112202d2013-02-13 19:29:12 -08002482 if (pwq->nr_in_flight[flush_color]) {
2483 pwq->flush_color = flush_color;
2484 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002485 wait = true;
2486 }
2487 }
2488
2489 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002490 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002491 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002492 }
2493
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002494 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002495 }
2496
Tejun Heo112202d2013-02-13 19:29:12 -08002497 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002498 complete(&wq->first_flusher->done);
2499
2500 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002503/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002505 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002507 * This function sleeps until all work items which were queued on entry
2508 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002510void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Tejun Heo73f53c42010-06-29 10:07:11 +02002512 struct wq_flusher this_flusher = {
2513 .list = LIST_HEAD_INIT(this_flusher.list),
2514 .flush_color = -1,
2515 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2516 };
2517 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002518
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002519 lock_map_acquire(&wq->lockdep_map);
2520 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002521
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002522 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002523
2524 /*
2525 * Start-to-wait phase
2526 */
2527 next_color = work_next_color(wq->work_color);
2528
2529 if (next_color != wq->flush_color) {
2530 /*
2531 * Color space is not full. The current work_color
2532 * becomes our flush_color and work_color is advanced
2533 * by one.
2534 */
Tejun Heo6183c002013-03-12 11:29:57 -07002535 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002536 this_flusher.flush_color = wq->work_color;
2537 wq->work_color = next_color;
2538
2539 if (!wq->first_flusher) {
2540 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002541 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002542
2543 wq->first_flusher = &this_flusher;
2544
Tejun Heo112202d2013-02-13 19:29:12 -08002545 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002546 wq->work_color)) {
2547 /* nothing to flush, done */
2548 wq->flush_color = next_color;
2549 wq->first_flusher = NULL;
2550 goto out_unlock;
2551 }
2552 } else {
2553 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002554 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002555 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002556 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 }
2558 } else {
2559 /*
2560 * Oops, color space is full, wait on overflow queue.
2561 * The next flush completion will assign us
2562 * flush_color and transfer to flusher_queue.
2563 */
2564 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2565 }
2566
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002567 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002568
2569 wait_for_completion(&this_flusher.done);
2570
2571 /*
2572 * Wake-up-and-cascade phase
2573 *
2574 * First flushers are responsible for cascading flushes and
2575 * handling overflow. Non-first flushers can simply return.
2576 */
2577 if (wq->first_flusher != &this_flusher)
2578 return;
2579
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002580 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002581
Tejun Heo4ce48b32010-07-02 10:03:51 +02002582 /* we might have raced, check again with mutex held */
2583 if (wq->first_flusher != &this_flusher)
2584 goto out_unlock;
2585
Tejun Heo73f53c42010-06-29 10:07:11 +02002586 wq->first_flusher = NULL;
2587
Tejun Heo6183c002013-03-12 11:29:57 -07002588 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2589 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002590
2591 while (true) {
2592 struct wq_flusher *next, *tmp;
2593
2594 /* complete all the flushers sharing the current flush color */
2595 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2596 if (next->flush_color != wq->flush_color)
2597 break;
2598 list_del_init(&next->list);
2599 complete(&next->done);
2600 }
2601
Tejun Heo6183c002013-03-12 11:29:57 -07002602 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2603 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002604
2605 /* this flush_color is finished, advance by one */
2606 wq->flush_color = work_next_color(wq->flush_color);
2607
2608 /* one color has been freed, handle overflow queue */
2609 if (!list_empty(&wq->flusher_overflow)) {
2610 /*
2611 * Assign the same color to all overflowed
2612 * flushers, advance work_color and append to
2613 * flusher_queue. This is the start-to-wait
2614 * phase for these overflowed flushers.
2615 */
2616 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2617 tmp->flush_color = wq->work_color;
2618
2619 wq->work_color = work_next_color(wq->work_color);
2620
2621 list_splice_tail_init(&wq->flusher_overflow,
2622 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002623 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002624 }
2625
2626 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002627 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002628 break;
2629 }
2630
2631 /*
2632 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002633 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002634 */
Tejun Heo6183c002013-03-12 11:29:57 -07002635 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2636 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002637
2638 list_del_init(&next->list);
2639 wq->first_flusher = next;
2640
Tejun Heo112202d2013-02-13 19:29:12 -08002641 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002642 break;
2643
2644 /*
2645 * Meh... this color is already done, clear first
2646 * flusher and repeat cascading.
2647 */
2648 wq->first_flusher = NULL;
2649 }
2650
2651out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002652 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
Tim Gardner1dadafa2015-08-04 11:26:04 -06002654EXPORT_SYMBOL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002656/**
2657 * drain_workqueue - drain a workqueue
2658 * @wq: workqueue to drain
2659 *
2660 * Wait until the workqueue becomes empty. While draining is in progress,
2661 * only chain queueing is allowed. IOW, only currently pending or running
2662 * work items on @wq can queue further work items on it. @wq is flushed
Chen Hanxiaob749b1b2015-05-13 06:10:05 -04002663 * repeatedly until it becomes empty. The number of flushing is determined
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002664 * by the depth of chaining and should be relatively short. Whine if it
2665 * takes too long.
2666 */
2667void drain_workqueue(struct workqueue_struct *wq)
2668{
2669 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002670 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002671
2672 /*
2673 * __queue_work() needs to test whether there are drainers, is much
2674 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002675 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002676 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002677 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002678 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002679 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002680 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002681reflush:
2682 flush_workqueue(wq);
2683
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002684 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002685
Tejun Heo49e3cf42013-03-12 11:29:58 -07002686 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002687 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002688
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002689 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002690 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002691 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002692
2693 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002694 continue;
2695
2696 if (++flush_cnt == 10 ||
2697 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002698 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002699 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002700
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002701 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002702 goto reflush;
2703 }
2704
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002705 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002706 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002707 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002708}
2709EXPORT_SYMBOL_GPL(drain_workqueue);
2710
Tejun Heo606a5022012-08-20 14:51:23 -07002711static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002712{
2713 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002714 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002715 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002716
2717 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002718
Tejun Heofa1b54e2013-03-12 11:30:00 -07002719 local_irq_disable();
2720 pool = get_work_pool(work);
2721 if (!pool) {
2722 local_irq_enable();
2723 return false;
2724 }
2725
2726 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002727 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002728 pwq = get_work_pwq(work);
2729 if (pwq) {
2730 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002731 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002732 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002733 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002734 if (!worker)
2735 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002736 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002737 }
Tejun Heobaf59022010-09-16 10:42:16 +02002738
Tejun Heo112202d2013-02-13 19:29:12 -08002739 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002740 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002741
Tejun Heoe1594892011-01-09 23:32:15 +01002742 /*
2743 * If @max_active is 1 or rescuer is in use, flushing another work
2744 * item on the same workqueue may lead to deadlock. Make sure the
2745 * flusher is not running on the same workqueue by verifying write
2746 * access.
2747 */
Tejun Heo493008a2013-03-12 11:30:03 -07002748 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002749 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002750 else
Tejun Heo112202d2013-02-13 19:29:12 -08002751 lock_map_acquire_read(&pwq->wq->lockdep_map);
2752 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002753
Tejun Heobaf59022010-09-16 10:42:16 +02002754 return true;
2755already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002756 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002757 return false;
2758}
2759
Oleg Nesterovdb700892008-07-25 01:47:49 -07002760/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002761 * flush_work - wait for a work to finish executing the last queueing instance
2762 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002763 *
Tejun Heo606a5022012-08-20 14:51:23 -07002764 * Wait until @work has finished execution. @work is guaranteed to be idle
2765 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002766 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002767 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002768 * %true if flush_work() waited for the work to finish execution,
2769 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002770 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002771bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002772{
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002773 struct wq_barrier barr;
2774
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002775 lock_map_acquire(&work->lockdep_map);
2776 lock_map_release(&work->lockdep_map);
2777
Bjorn Helgaas12997d12013-11-18 11:00:29 -07002778 if (start_flush_work(work, &barr)) {
2779 wait_for_completion(&barr.done);
2780 destroy_work_on_stack(&barr.work);
2781 return true;
2782 } else {
2783 return false;
2784 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002785}
2786EXPORT_SYMBOL_GPL(flush_work);
2787
Tejun Heo8603e1b32015-03-05 08:04:13 -05002788struct cwt_wait {
2789 wait_queue_t wait;
2790 struct work_struct *work;
2791};
2792
2793static int cwt_wakefn(wait_queue_t *wait, unsigned mode, int sync, void *key)
2794{
2795 struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait);
2796
2797 if (cwait->work != key)
2798 return 0;
2799 return autoremove_wake_function(wait, mode, sync, key);
2800}
2801
Tejun Heo36e227d2012-08-03 10:30:46 -07002802static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002803{
Tejun Heo8603e1b32015-03-05 08:04:13 -05002804 static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq);
Tejun Heobbb68df2012-08-03 10:30:46 -07002805 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002806 int ret;
2807
2808 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002809 ret = try_to_grab_pending(work, is_dwork, &flags);
2810 /*
Tejun Heo8603e1b32015-03-05 08:04:13 -05002811 * If someone else is already canceling, wait for it to
2812 * finish. flush_work() doesn't work for PREEMPT_NONE
2813 * because we may get scheduled between @work's completion
2814 * and the other canceling task resuming and clearing
2815 * CANCELING - flush_work() will return false immediately
2816 * as @work is no longer busy, try_to_grab_pending() will
2817 * return -ENOENT as @work is still being canceled and the
2818 * other canceling task won't be able to clear CANCELING as
2819 * we're hogging the CPU.
2820 *
2821 * Let's wait for completion using a waitqueue. As this
2822 * may lead to the thundering herd problem, use a custom
2823 * wake function which matches @work along with exclusive
2824 * wait and wakeup.
Tejun Heobbb68df2012-08-03 10:30:46 -07002825 */
Tejun Heo8603e1b32015-03-05 08:04:13 -05002826 if (unlikely(ret == -ENOENT)) {
2827 struct cwt_wait cwait;
2828
2829 init_wait(&cwait.wait);
2830 cwait.wait.func = cwt_wakefn;
2831 cwait.work = work;
2832
2833 prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait,
2834 TASK_UNINTERRUPTIBLE);
2835 if (work_is_canceling(work))
2836 schedule();
2837 finish_wait(&cancel_waitq, &cwait.wait);
2838 }
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002839 } while (unlikely(ret < 0));
2840
Tejun Heobbb68df2012-08-03 10:30:46 -07002841 /* tell other tasks trying to grab @work to back off */
2842 mark_work_canceling(work);
2843 local_irq_restore(flags);
2844
Tejun Heo606a5022012-08-20 14:51:23 -07002845 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002846 clear_work_data(work);
Tejun Heo8603e1b32015-03-05 08:04:13 -05002847
2848 /*
2849 * Paired with prepare_to_wait() above so that either
2850 * waitqueue_active() is visible here or !work_is_canceling() is
2851 * visible there.
2852 */
2853 smp_mb();
2854 if (waitqueue_active(&cancel_waitq))
2855 __wake_up(&cancel_waitq, TASK_NORMAL, 1, work);
2856
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002857 return ret;
2858}
2859
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002860/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002861 * cancel_work_sync - cancel a work and wait for it to finish
2862 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002863 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 * Cancel @work and wait for its execution to finish. This function
2865 * can be used even if the work re-queues itself or migrates to
2866 * another workqueue. On return from this function, @work is
2867 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002868 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002869 * cancel_work_sync(&delayed_work->work) must not be used for
2870 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002871 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002872 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002873 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002875 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002876 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002877 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002878bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002879{
Tejun Heo36e227d2012-08-03 10:30:46 -07002880 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002881}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002882EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002883
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002884/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002885 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2886 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002887 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * Delayed timer is cancelled and the pending work is queued for
2889 * immediate execution. Like flush_work(), this function only
2890 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002891 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002892 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002893 * %true if flush_work() waited for the work to finish execution,
2894 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002895 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002896bool flush_delayed_work(struct delayed_work *dwork)
2897{
Tejun Heo8930cab2012-08-03 10:30:45 -07002898 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002899 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002900 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002901 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002902 return flush_work(&dwork->work);
2903}
2904EXPORT_SYMBOL(flush_delayed_work);
2905
2906/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002907 * cancel_delayed_work - cancel a delayed work
2908 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002909 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002910 * Kill off a pending delayed_work.
2911 *
2912 * Return: %true if @dwork was pending and canceled; %false if it wasn't
2913 * pending.
2914 *
2915 * Note:
2916 * The work callback function may still be running on return, unless
2917 * it returns %true and the work doesn't re-arm itself. Explicitly flush or
2918 * use cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002919 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002920 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002921 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002922bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002923{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002924 unsigned long flags;
2925 int ret;
2926
2927 do {
2928 ret = try_to_grab_pending(&dwork->work, true, &flags);
2929 } while (unlikely(ret == -EAGAIN));
2930
2931 if (unlikely(ret < 0))
2932 return false;
2933
Tejun Heo7c3eed52013-01-24 11:01:33 -08002934 set_work_pool_and_clear_pending(&dwork->work,
2935 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002936 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002937 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002938}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002939EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002940
2941/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002942 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2943 * @dwork: the delayed work cancel
2944 *
2945 * This is cancel_work_sync() for delayed works.
2946 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002947 * Return:
Tejun Heo401a8d02010-09-16 10:36:00 +02002948 * %true if @dwork was pending, %false otherwise.
2949 */
2950bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002951{
Tejun Heo36e227d2012-08-03 10:30:46 -07002952 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002953}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002954EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002956/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002957 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002958 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002959 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002960 * schedule_on_each_cpu() executes @func on each online CPU using the
2961 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002962 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002963 *
Yacine Belkadid185af32013-07-31 14:59:24 -07002964 * Return:
Tejun Heo31ddd872010-10-19 11:14:49 +02002965 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002966 */
David Howells65f27f32006-11-22 14:55:48 +00002967int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002968{
2969 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002970 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002971
Andrew Mortonb6136772006-06-25 05:47:49 -07002972 works = alloc_percpu(struct work_struct);
2973 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002974 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002975
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002976 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002977
Christoph Lameter15316ba2006-01-08 01:00:43 -08002978 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002979 struct work_struct *work = per_cpu_ptr(works, cpu);
2980
2981 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002982 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002983 }
Tejun Heo93981802009-11-17 14:06:20 -08002984
2985 for_each_online_cpu(cpu)
2986 flush_work(per_cpu_ptr(works, cpu));
2987
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002988 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002989 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002990 return 0;
2991}
2992
Alan Sterneef6a7d2010-02-12 17:39:21 +09002993/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002994 * execute_in_process_context - reliably execute the routine with user context
2995 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002996 * @ew: guaranteed storage for the execute work structure (must
2997 * be available when the work executes)
2998 *
2999 * Executes the function immediately if process context is available,
3000 * otherwise schedules the function for delayed execution.
3001 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003002 * Return: 0 - function was executed
James Bottomley1fa44ec2006-02-23 12:43:43 -06003003 * 1 - function was scheduled for execution
3004 */
David Howells65f27f32006-11-22 14:55:48 +00003005int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003006{
3007 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003008 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003009 return 0;
3010 }
3011
David Howells65f27f32006-11-22 14:55:48 +00003012 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003013 schedule_work(&ew->work);
3014
3015 return 1;
3016}
3017EXPORT_SYMBOL_GPL(execute_in_process_context);
3018
Tejun Heo7a4e3442013-03-12 11:30:00 -07003019/**
3020 * free_workqueue_attrs - free a workqueue_attrs
3021 * @attrs: workqueue_attrs to free
3022 *
3023 * Undo alloc_workqueue_attrs().
3024 */
3025void free_workqueue_attrs(struct workqueue_attrs *attrs)
3026{
3027 if (attrs) {
3028 free_cpumask_var(attrs->cpumask);
3029 kfree(attrs);
3030 }
3031}
3032
3033/**
3034 * alloc_workqueue_attrs - allocate a workqueue_attrs
3035 * @gfp_mask: allocation mask to use
3036 *
3037 * Allocate a new workqueue_attrs, initialize with default settings and
Yacine Belkadid185af32013-07-31 14:59:24 -07003038 * return it.
3039 *
3040 * Return: The allocated new workqueue_attr on success. %NULL on failure.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003041 */
3042struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3043{
3044 struct workqueue_attrs *attrs;
3045
3046 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3047 if (!attrs)
3048 goto fail;
3049 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3050 goto fail;
3051
Tejun Heo13e2e552013-04-01 11:23:31 -07003052 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003053 return attrs;
3054fail:
3055 free_workqueue_attrs(attrs);
3056 return NULL;
3057}
3058
Tejun Heo29c91e92013-03-12 11:30:03 -07003059static void copy_workqueue_attrs(struct workqueue_attrs *to,
3060 const struct workqueue_attrs *from)
3061{
3062 to->nice = from->nice;
3063 cpumask_copy(to->cpumask, from->cpumask);
Shaohua Li2865a8f2013-08-01 09:56:36 +08003064 /*
3065 * Unlike hash and equality test, this function doesn't ignore
3066 * ->no_numa as it is used for both pool and wq attrs. Instead,
3067 * get_unbound_pool() explicitly clears ->no_numa after copying.
3068 */
3069 to->no_numa = from->no_numa;
Tejun Heo29c91e92013-03-12 11:30:03 -07003070}
3071
Tejun Heo29c91e92013-03-12 11:30:03 -07003072/* hash value of the content of @attr */
3073static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3074{
3075 u32 hash = 0;
3076
3077 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003078 hash = jhash(cpumask_bits(attrs->cpumask),
3079 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003080 return hash;
3081}
3082
3083/* content equality test */
3084static bool wqattrs_equal(const struct workqueue_attrs *a,
3085 const struct workqueue_attrs *b)
3086{
3087 if (a->nice != b->nice)
3088 return false;
3089 if (!cpumask_equal(a->cpumask, b->cpumask))
3090 return false;
3091 return true;
3092}
3093
Tejun Heo7a4e3442013-03-12 11:30:00 -07003094/**
3095 * init_worker_pool - initialize a newly zalloc'd worker_pool
3096 * @pool: worker_pool to initialize
3097 *
Shailendra Verma402dd892015-05-23 10:38:14 +05303098 * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs.
Yacine Belkadid185af32013-07-31 14:59:24 -07003099 *
3100 * Return: 0 on success, -errno on failure. Even on failure, all fields
Tejun Heo29c91e92013-03-12 11:30:03 -07003101 * inside @pool proper are initialized and put_unbound_pool() can be called
3102 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003103 */
3104static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003105{
3106 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003107 pool->id = -1;
3108 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003109 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003110 pool->flags |= POOL_DISASSOCIATED;
3111 INIT_LIST_HEAD(&pool->worklist);
3112 INIT_LIST_HEAD(&pool->idle_list);
3113 hash_init(pool->busy_hash);
3114
3115 init_timer_deferrable(&pool->idle_timer);
3116 pool->idle_timer.function = idle_worker_timeout;
3117 pool->idle_timer.data = (unsigned long)pool;
3118
3119 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3120 (unsigned long)pool);
3121
3122 mutex_init(&pool->manager_arb);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003123 mutex_init(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003124 INIT_LIST_HEAD(&pool->workers);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003125
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003126 ida_init(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003127 INIT_HLIST_NODE(&pool->hash_node);
3128 pool->refcnt = 1;
3129
3130 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003131 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3132 if (!pool->attrs)
3133 return -ENOMEM;
3134 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003135}
3136
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003137static void rcu_free_wq(struct rcu_head *rcu)
3138{
3139 struct workqueue_struct *wq =
3140 container_of(rcu, struct workqueue_struct, rcu);
3141
3142 if (!(wq->flags & WQ_UNBOUND))
3143 free_percpu(wq->cpu_pwqs);
3144 else
3145 free_workqueue_attrs(wq->unbound_attrs);
3146
3147 kfree(wq->rescuer);
3148 kfree(wq);
3149}
3150
Tejun Heo29c91e92013-03-12 11:30:03 -07003151static void rcu_free_pool(struct rcu_head *rcu)
3152{
3153 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3154
Lai Jiangshan7cda9aa2014-05-20 17:46:32 +08003155 ida_destroy(&pool->worker_ida);
Tejun Heo29c91e92013-03-12 11:30:03 -07003156 free_workqueue_attrs(pool->attrs);
3157 kfree(pool);
3158}
3159
3160/**
3161 * put_unbound_pool - put a worker_pool
3162 * @pool: worker_pool to put
3163 *
3164 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003165 * safe manner. get_unbound_pool() calls this function on its failure path
3166 * and this function should be able to release pools which went through,
3167 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003168 *
3169 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003170 */
3171static void put_unbound_pool(struct worker_pool *pool)
3172{
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003173 DECLARE_COMPLETION_ONSTACK(detach_completion);
Tejun Heo29c91e92013-03-12 11:30:03 -07003174 struct worker *worker;
3175
Tejun Heoa892cac2013-04-01 11:23:32 -07003176 lockdep_assert_held(&wq_pool_mutex);
3177
3178 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003179 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003180
3181 /* sanity checks */
Lai Jiangshan61d0fbb2014-06-03 15:31:45 +08003182 if (WARN_ON(!(pool->cpu < 0)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003183 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003184 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003185
3186 /* release id and unhash */
3187 if (pool->id >= 0)
3188 idr_remove(&worker_pool_idr, pool->id);
3189 hash_del(&pool->hash_node);
3190
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003191 /*
3192 * Become the manager and destroy all workers. Grabbing
3193 * manager_arb prevents @pool's workers from blocking on
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003194 * attach_mutex.
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003195 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003196 mutex_lock(&pool->manager_arb);
Tejun Heo29c91e92013-03-12 11:30:03 -07003197
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003198 spin_lock_irq(&pool->lock);
Lai Jiangshan1037de32014-05-22 16:44:07 +08003199 while ((worker = first_idle_worker(pool)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003200 destroy_worker(worker);
3201 WARN_ON(pool->nr_workers || pool->nr_idle);
Tejun Heo29c91e92013-03-12 11:30:03 -07003202 spin_unlock_irq(&pool->lock);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003203
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003204 mutex_lock(&pool->attach_mutex);
Lai Jiangshanda028462014-05-20 17:46:31 +08003205 if (!list_empty(&pool->workers))
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003206 pool->detach_completion = &detach_completion;
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08003207 mutex_unlock(&pool->attach_mutex);
Lai Jiangshan60f5a4b2014-05-20 17:46:29 +08003208
3209 if (pool->detach_completion)
3210 wait_for_completion(pool->detach_completion);
3211
Tejun Heo29c91e92013-03-12 11:30:03 -07003212 mutex_unlock(&pool->manager_arb);
3213
3214 /* shut down the timers */
3215 del_timer_sync(&pool->idle_timer);
3216 del_timer_sync(&pool->mayday_timer);
3217
3218 /* sched-RCU protected to allow dereferences from get_work_pool() */
3219 call_rcu_sched(&pool->rcu, rcu_free_pool);
3220}
3221
3222/**
3223 * get_unbound_pool - get a worker_pool with the specified attributes
3224 * @attrs: the attributes of the worker_pool to get
3225 *
3226 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3227 * reference count and return it. If there already is a matching
3228 * worker_pool, it will be used; otherwise, this function attempts to
Yacine Belkadid185af32013-07-31 14:59:24 -07003229 * create a new one.
Tejun Heoa892cac2013-04-01 11:23:32 -07003230 *
3231 * Should be called with wq_pool_mutex held.
Yacine Belkadid185af32013-07-31 14:59:24 -07003232 *
3233 * Return: On success, a worker_pool with the same attributes as @attrs.
3234 * On failure, %NULL.
Tejun Heo29c91e92013-03-12 11:30:03 -07003235 */
3236static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3237{
Tejun Heo29c91e92013-03-12 11:30:03 -07003238 u32 hash = wqattrs_hash(attrs);
3239 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003240 int node;
Xunlei Pange2273582015-10-09 11:53:12 +08003241 int target_node = NUMA_NO_NODE;
Tejun Heo29c91e92013-03-12 11:30:03 -07003242
Tejun Heoa892cac2013-04-01 11:23:32 -07003243 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003244
3245 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003246 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3247 if (wqattrs_equal(pool->attrs, attrs)) {
3248 pool->refcnt++;
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003249 return pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003250 }
3251 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003252
Xunlei Pange2273582015-10-09 11:53:12 +08003253 /* if cpumask is contained inside a NUMA node, we belong to that node */
3254 if (wq_numa_enabled) {
3255 for_each_node(node) {
3256 if (cpumask_subset(attrs->cpumask,
3257 wq_numa_possible_cpumask[node])) {
3258 target_node = node;
3259 break;
3260 }
3261 }
3262 }
3263
Tejun Heo29c91e92013-03-12 11:30:03 -07003264 /* nope, create a new one */
Xunlei Pange2273582015-10-09 11:53:12 +08003265 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node);
Tejun Heo29c91e92013-03-12 11:30:03 -07003266 if (!pool || init_worker_pool(pool) < 0)
3267 goto fail;
3268
Tejun Heo8864b4e2013-03-12 11:30:04 -07003269 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003270 copy_workqueue_attrs(pool->attrs, attrs);
Xunlei Pange2273582015-10-09 11:53:12 +08003271 pool->node = target_node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003272
Shaohua Li2865a8f2013-08-01 09:56:36 +08003273 /*
3274 * no_numa isn't a worker_pool attribute, always clear it. See
3275 * 'struct workqueue_attrs' comments for detail.
3276 */
3277 pool->attrs->no_numa = false;
3278
Tejun Heo29c91e92013-03-12 11:30:03 -07003279 if (worker_pool_assign_id(pool) < 0)
3280 goto fail;
3281
3282 /* create and start the initial worker */
Lai Jiangshan051e1852014-07-22 13:03:02 +08003283 if (!create_worker(pool))
Tejun Heo29c91e92013-03-12 11:30:03 -07003284 goto fail;
3285
Tejun Heo29c91e92013-03-12 11:30:03 -07003286 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003287 hash_add(unbound_pool_hash, &pool->hash_node, hash);
Lai Jiangshan3fb18232014-07-22 13:04:49 +08003288
Tejun Heo29c91e92013-03-12 11:30:03 -07003289 return pool;
3290fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003291 if (pool)
3292 put_unbound_pool(pool);
3293 return NULL;
3294}
3295
Tejun Heo8864b4e2013-03-12 11:30:04 -07003296static void rcu_free_pwq(struct rcu_head *rcu)
3297{
3298 kmem_cache_free(pwq_cache,
3299 container_of(rcu, struct pool_workqueue, rcu));
3300}
3301
3302/*
3303 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3304 * and needs to be destroyed.
3305 */
3306static void pwq_unbound_release_workfn(struct work_struct *work)
3307{
3308 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3309 unbound_release_work);
3310 struct workqueue_struct *wq = pwq->wq;
3311 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003312 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003313
3314 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3315 return;
3316
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003317 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003318 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003319 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003320 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003321
Tejun Heoa892cac2013-04-01 11:23:32 -07003322 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003323 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003324 mutex_unlock(&wq_pool_mutex);
3325
Tejun Heo8864b4e2013-03-12 11:30:04 -07003326 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3327
3328 /*
3329 * If we're the last pwq going away, @wq is already dead and no one
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003330 * is gonna access it anymore. Schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003331 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003332 if (is_last)
3333 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003334}
3335
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003336/**
Tejun Heo699ce092013-03-13 16:51:35 -07003337 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003338 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003339 *
Tejun Heo699ce092013-03-13 16:51:35 -07003340 * If @pwq isn't freezing, set @pwq->max_active to the associated
3341 * workqueue's saved_max_active and activate delayed work items
3342 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003343 */
Tejun Heo699ce092013-03-13 16:51:35 -07003344static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003345{
Tejun Heo699ce092013-03-13 16:51:35 -07003346 struct workqueue_struct *wq = pwq->wq;
3347 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003348
Tejun Heo699ce092013-03-13 16:51:35 -07003349 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003350 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003351
3352 /* fast exit for non-freezable wqs */
3353 if (!freezable && pwq->max_active == wq->saved_max_active)
3354 return;
3355
Lai Jiangshana357fc02013-03-25 16:57:19 -07003356 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003357
Lai Jiangshan74b414e2014-05-22 19:01:16 +08003358 /*
3359 * During [un]freezing, the caller is responsible for ensuring that
3360 * this function is called at least once after @workqueue_freezing
3361 * is updated and visible.
3362 */
3363 if (!freezable || !workqueue_freezing) {
Tejun Heo699ce092013-03-13 16:51:35 -07003364 pwq->max_active = wq->saved_max_active;
3365
3366 while (!list_empty(&pwq->delayed_works) &&
3367 pwq->nr_active < pwq->max_active)
3368 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003369
3370 /*
3371 * Need to kick a worker after thawed or an unbound wq's
3372 * max_active is bumped. It's a slow path. Do it always.
3373 */
3374 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003375 } else {
3376 pwq->max_active = 0;
3377 }
3378
Lai Jiangshana357fc02013-03-25 16:57:19 -07003379 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003380}
3381
Tejun Heoe50aba92013-04-01 11:23:35 -07003382/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003383static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3384 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003385{
3386 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3387
Tejun Heoe50aba92013-04-01 11:23:35 -07003388 memset(pwq, 0, sizeof(*pwq));
3389
Tejun Heod2c1d402013-03-12 11:30:04 -07003390 pwq->pool = pool;
3391 pwq->wq = wq;
3392 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003393 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003394 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003395 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003396 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003397 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003398}
Tejun Heod2c1d402013-03-12 11:30:04 -07003399
Tejun Heof147f292013-04-01 11:23:35 -07003400/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003401static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003402{
3403 struct workqueue_struct *wq = pwq->wq;
3404
3405 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003406
Tejun Heo1befcf32013-04-01 11:23:35 -07003407 /* may be called multiple times, ignore if already linked */
3408 if (!list_empty(&pwq->pwqs_node))
3409 return;
3410
Lai Jiangshan29b1cb42014-07-22 13:04:27 +08003411 /* set the matching work_color */
Tejun Heo75ccf592013-03-12 11:30:04 -07003412 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003413
3414 /* sync max_active to the current setting */
3415 pwq_adjust_max_active(pwq);
3416
3417 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003418 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003419}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003420
Tejun Heof147f292013-04-01 11:23:35 -07003421/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3422static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3423 const struct workqueue_attrs *attrs)
3424{
3425 struct worker_pool *pool;
3426 struct pool_workqueue *pwq;
3427
3428 lockdep_assert_held(&wq_pool_mutex);
3429
3430 pool = get_unbound_pool(attrs);
3431 if (!pool)
3432 return NULL;
3433
Tejun Heoe50aba92013-04-01 11:23:35 -07003434 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003435 if (!pwq) {
3436 put_unbound_pool(pool);
3437 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003438 }
Tejun Heo6029a912013-04-01 11:23:34 -07003439
Tejun Heof147f292013-04-01 11:23:35 -07003440 init_pwq(pwq, wq, pool);
3441 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003442}
3443
Tejun Heo4c16bd32013-04-01 11:23:36 -07003444/**
Gong Zhaogang30186c62015-05-11 11:02:47 -04003445 * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003446 * @attrs: the wq_attrs of the default pwq of the target workqueue
Tejun Heo4c16bd32013-04-01 11:23:36 -07003447 * @node: the target NUMA node
3448 * @cpu_going_down: if >= 0, the CPU to consider as offline
3449 * @cpumask: outarg, the resulting cpumask
3450 *
3451 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3452 * @cpu_going_down is >= 0, that cpu is considered offline during
Yacine Belkadid185af32013-07-31 14:59:24 -07003453 * calculation. The result is stored in @cpumask.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003454 *
3455 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3456 * enabled and @node has online CPUs requested by @attrs, the returned
3457 * cpumask is the intersection of the possible CPUs of @node and
3458 * @attrs->cpumask.
3459 *
3460 * The caller is responsible for ensuring that the cpumask of @node stays
3461 * stable.
Yacine Belkadid185af32013-07-31 14:59:24 -07003462 *
3463 * Return: %true if the resulting @cpumask is different from @attrs->cpumask,
3464 * %false if equal.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003465 */
3466static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3467 int cpu_going_down, cpumask_t *cpumask)
3468{
Tejun Heod55262c2013-04-01 11:23:38 -07003469 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003470 goto use_dfl;
3471
3472 /* does @node have any online CPUs @attrs wants? */
3473 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3474 if (cpu_going_down >= 0)
3475 cpumask_clear_cpu(cpu_going_down, cpumask);
3476
3477 if (cpumask_empty(cpumask))
3478 goto use_dfl;
3479
3480 /* yeap, return possible CPUs in @node that @attrs wants */
3481 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3482 return !cpumask_equal(cpumask, attrs->cpumask);
3483
3484use_dfl:
3485 cpumask_copy(cpumask, attrs->cpumask);
3486 return false;
3487}
3488
Tejun Heo1befcf32013-04-01 11:23:35 -07003489/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3490static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3491 int node,
3492 struct pool_workqueue *pwq)
3493{
3494 struct pool_workqueue *old_pwq;
3495
Lai Jiangshan5b95e1a2015-05-12 20:32:29 +08003496 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07003497 lockdep_assert_held(&wq->mutex);
3498
3499 /* link_pwq() can handle duplicate calls */
3500 link_pwq(pwq);
3501
3502 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3503 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3504 return old_pwq;
3505}
3506
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003507/* context to store the prepared attrs & pwqs before applying */
3508struct apply_wqattrs_ctx {
3509 struct workqueue_struct *wq; /* target workqueue */
3510 struct workqueue_attrs *attrs; /* attrs to apply */
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003511 struct list_head list; /* queued for batching commit */
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003512 struct pool_workqueue *dfl_pwq;
3513 struct pool_workqueue *pwq_tbl[];
3514};
3515
3516/* free the resources after success or abort */
3517static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx)
3518{
3519 if (ctx) {
3520 int node;
3521
3522 for_each_node(node)
3523 put_pwq_unlocked(ctx->pwq_tbl[node]);
3524 put_pwq_unlocked(ctx->dfl_pwq);
3525
3526 free_workqueue_attrs(ctx->attrs);
3527
3528 kfree(ctx);
3529 }
3530}
3531
3532/* allocate the attrs and pwqs for later installation */
3533static struct apply_wqattrs_ctx *
3534apply_wqattrs_prepare(struct workqueue_struct *wq,
3535 const struct workqueue_attrs *attrs)
3536{
3537 struct apply_wqattrs_ctx *ctx;
3538 struct workqueue_attrs *new_attrs, *tmp_attrs;
3539 int node;
3540
3541 lockdep_assert_held(&wq_pool_mutex);
3542
3543 ctx = kzalloc(sizeof(*ctx) + nr_node_ids * sizeof(ctx->pwq_tbl[0]),
3544 GFP_KERNEL);
3545
3546 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3547 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3548 if (!ctx || !new_attrs || !tmp_attrs)
3549 goto out_free;
3550
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003551 /*
3552 * Calculate the attrs of the default pwq.
3553 * If the user configured cpumask doesn't overlap with the
3554 * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask.
3555 */
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003556 copy_workqueue_attrs(new_attrs, attrs);
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08003557 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask);
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003558 if (unlikely(cpumask_empty(new_attrs->cpumask)))
3559 cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask);
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003560
3561 /*
3562 * We may create multiple pwqs with differing cpumasks. Make a
3563 * copy of @new_attrs which will be modified and used to obtain
3564 * pools.
3565 */
3566 copy_workqueue_attrs(tmp_attrs, new_attrs);
3567
3568 /*
3569 * If something goes wrong during CPU up/down, we'll fall back to
3570 * the default pwq covering whole @attrs->cpumask. Always create
3571 * it even if we don't use it immediately.
3572 */
3573 ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3574 if (!ctx->dfl_pwq)
3575 goto out_free;
3576
3577 for_each_node(node) {
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003578 if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) {
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003579 ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3580 if (!ctx->pwq_tbl[node])
3581 goto out_free;
3582 } else {
3583 ctx->dfl_pwq->refcnt++;
3584 ctx->pwq_tbl[node] = ctx->dfl_pwq;
3585 }
3586 }
3587
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003588 /* save the user configured attrs and sanitize it. */
3589 copy_workqueue_attrs(new_attrs, attrs);
3590 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003591 ctx->attrs = new_attrs;
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003592
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003593 ctx->wq = wq;
3594 free_workqueue_attrs(tmp_attrs);
3595 return ctx;
3596
3597out_free:
3598 free_workqueue_attrs(tmp_attrs);
3599 free_workqueue_attrs(new_attrs);
3600 apply_wqattrs_cleanup(ctx);
3601 return NULL;
3602}
3603
3604/* set attrs and install prepared pwqs, @ctx points to old pwqs on return */
3605static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)
3606{
3607 int node;
3608
3609 /* all pwqs have been created successfully, let's install'em */
3610 mutex_lock(&ctx->wq->mutex);
3611
3612 copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs);
3613
3614 /* save the previous pwq and install the new one */
3615 for_each_node(node)
3616 ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node,
3617 ctx->pwq_tbl[node]);
3618
3619 /* @dfl_pwq might not have been used, ensure it's linked */
3620 link_pwq(ctx->dfl_pwq);
3621 swap(ctx->wq->dfl_pwq, ctx->dfl_pwq);
3622
3623 mutex_unlock(&ctx->wq->mutex);
3624}
3625
Lai Jiangshana0111cf2015-05-19 18:03:47 +08003626static void apply_wqattrs_lock(void)
3627{
3628 /* CPUs should stay stable across pwq creations and installations */
3629 get_online_cpus();
3630 mutex_lock(&wq_pool_mutex);
3631}
3632
3633static void apply_wqattrs_unlock(void)
3634{
3635 mutex_unlock(&wq_pool_mutex);
3636 put_online_cpus();
3637}
3638
3639static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
3640 const struct workqueue_attrs *attrs)
3641{
3642 struct apply_wqattrs_ctx *ctx;
3643 int ret = -ENOMEM;
3644
3645 /* only unbound workqueues can change attributes */
3646 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3647 return -EINVAL;
3648
3649 /* creating multiple pwqs breaks ordering guarantee */
3650 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3651 return -EINVAL;
3652
3653 ctx = apply_wqattrs_prepare(wq, attrs);
3654
3655 /* the ctx has been prepared successfully, let's commit it */
3656 if (ctx) {
3657 apply_wqattrs_commit(ctx);
3658 ret = 0;
3659 }
3660
3661 apply_wqattrs_cleanup(ctx);
3662
3663 return ret;
3664}
3665
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003666/**
3667 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3668 * @wq: the target workqueue
3669 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3670 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003671 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3672 * machines, this function maps a separate pwq to each NUMA node with
3673 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3674 * NUMA node it was issued on. Older pwqs are released as in-flight work
3675 * items finish. Note that a work item which repeatedly requeues itself
3676 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003677 *
Yacine Belkadid185af32013-07-31 14:59:24 -07003678 * Performs GFP_KERNEL allocations.
3679 *
3680 * Return: 0 on success and -errno on failure.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003681 */
3682int apply_workqueue_attrs(struct workqueue_struct *wq,
3683 const struct workqueue_attrs *attrs)
3684{
Lai Jiangshana0111cf2015-05-19 18:03:47 +08003685 int ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003686
Lai Jiangshana0111cf2015-05-19 18:03:47 +08003687 apply_wqattrs_lock();
3688 ret = apply_workqueue_attrs_locked(wq, attrs);
3689 apply_wqattrs_unlock();
Lai Jiangshan2d5f0762015-04-27 17:58:38 +08003690
Tejun Heo48621252013-04-01 11:23:31 -07003691 return ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003692}
3693
Tejun Heo4c16bd32013-04-01 11:23:36 -07003694/**
3695 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3696 * @wq: the target workqueue
3697 * @cpu: the CPU coming up or going down
3698 * @online: whether @cpu is coming up or going down
3699 *
3700 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3701 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3702 * @wq accordingly.
3703 *
3704 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3705 * falls back to @wq->dfl_pwq which may not be optimal but is always
3706 * correct.
3707 *
3708 * Note that when the last allowed CPU of a NUMA node goes offline for a
3709 * workqueue with a cpumask spanning multiple nodes, the workers which were
3710 * already executing the work items for the workqueue will lose their CPU
3711 * affinity and may execute on any CPU. This is similar to how per-cpu
3712 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3713 * affinity, it's the user's responsibility to flush the work item from
3714 * CPU_DOWN_PREPARE.
3715 */
3716static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3717 bool online)
3718{
3719 int node = cpu_to_node(cpu);
3720 int cpu_off = online ? -1 : cpu;
3721 struct pool_workqueue *old_pwq = NULL, *pwq;
3722 struct workqueue_attrs *target_attrs;
3723 cpumask_t *cpumask;
3724
3725 lockdep_assert_held(&wq_pool_mutex);
3726
Lai Jiangshanf7142ed2015-05-12 20:32:30 +08003727 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) ||
3728 wq->unbound_attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003729 return;
3730
3731 /*
3732 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3733 * Let's use a preallocated one. The following buf is protected by
3734 * CPU hotplug exclusion.
3735 */
3736 target_attrs = wq_update_unbound_numa_attrs_buf;
3737 cpumask = target_attrs->cpumask;
3738
Tejun Heo4c16bd32013-04-01 11:23:36 -07003739 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3740 pwq = unbound_pwq_by_node(wq, node);
3741
3742 /*
3743 * Let's determine what needs to be done. If the target cpumask is
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003744 * different from the default pwq's, we need to compare it to @pwq's
3745 * and create a new one if they don't match. If the target cpumask
3746 * equals the default pwq's, the default pwq should be used.
Tejun Heo4c16bd32013-04-01 11:23:36 -07003747 */
Lai Jiangshan042f7df2015-04-30 17:16:12 +08003748 if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) {
Tejun Heo4c16bd32013-04-01 11:23:36 -07003749 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
Lai Jiangshanf7142ed2015-05-12 20:32:30 +08003750 return;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003751 } else {
Daeseok Youn534a3fb2014-04-18 09:08:14 +09003752 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003753 }
3754
Tejun Heo4c16bd32013-04-01 11:23:36 -07003755 /* create a new pwq */
3756 pwq = alloc_unbound_pwq(wq, target_attrs);
3757 if (!pwq) {
Fabian Frederick2d916032014-05-12 13:59:35 -04003758 pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
3759 wq->name);
Daeseok Youn77f300b2014-04-16 14:32:29 +09003760 goto use_dfl_pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003761 }
3762
Lai Jiangshanf7142ed2015-05-12 20:32:30 +08003763 /* Install the new pwq. */
Tejun Heo4c16bd32013-04-01 11:23:36 -07003764 mutex_lock(&wq->mutex);
3765 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
3766 goto out_unlock;
3767
3768use_dfl_pwq:
Lai Jiangshanf7142ed2015-05-12 20:32:30 +08003769 mutex_lock(&wq->mutex);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003770 spin_lock_irq(&wq->dfl_pwq->pool->lock);
3771 get_pwq(wq->dfl_pwq);
3772 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
3773 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
3774out_unlock:
3775 mutex_unlock(&wq->mutex);
3776 put_pwq_unlocked(old_pwq);
3777}
3778
Tejun Heo30cdf242013-03-12 11:29:57 -07003779static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003781 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo8a2b7532013-09-05 12:30:04 -04003782 int cpu, ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003783
Tejun Heo30cdf242013-03-12 11:29:57 -07003784 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003785 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3786 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003787 return -ENOMEM;
3788
3789 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003790 struct pool_workqueue *pwq =
3791 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003792 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003793 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003794
Tejun Heof147f292013-04-01 11:23:35 -07003795 init_pwq(pwq, wq, &cpu_pools[highpri]);
3796
3797 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07003798 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003799 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07003800 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003801 return 0;
Tejun Heo8a2b7532013-09-05 12:30:04 -04003802 } else if (wq->flags & __WQ_ORDERED) {
3803 ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]);
3804 /* there should only be single pwq for ordering guarantee */
3805 WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node ||
3806 wq->pwqs.prev != &wq->dfl_pwq->pwqs_node),
3807 "ordering guarantee broken for workqueue %s\n", wq->name);
3808 return ret;
Tejun Heo30cdf242013-03-12 11:29:57 -07003809 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003810 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003811 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003812}
3813
Tejun Heof3421792010-07-02 10:03:51 +02003814static int wq_clamp_max_active(int max_active, unsigned int flags,
3815 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003816{
Tejun Heof3421792010-07-02 10:03:51 +02003817 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3818
3819 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003820 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3821 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003822
Tejun Heof3421792010-07-02 10:03:51 +02003823 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003824}
3825
Tejun Heob196be82012-01-10 15:11:35 -08003826struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003827 unsigned int flags,
3828 int max_active,
3829 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003830 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003831{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003832 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07003833 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003834 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003835 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003836
Viresh Kumarcee22a12013-04-08 16:45:40 +05303837 /* see the comment above the definition of WQ_POWER_EFFICIENT */
3838 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
3839 flags |= WQ_UNBOUND;
3840
Tejun Heoecf68812013-04-01 11:23:34 -07003841 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003842 if (flags & WQ_UNBOUND)
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08003843 tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003844
3845 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08003846 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003847 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003848
Tejun Heo6029a912013-04-01 11:23:34 -07003849 if (flags & WQ_UNBOUND) {
3850 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3851 if (!wq->unbound_attrs)
3852 goto err_free_wq;
3853 }
3854
Tejun Heoecf68812013-04-01 11:23:34 -07003855 va_start(args, lock_name);
3856 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08003857 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003858
Tejun Heod320c032010-06-29 10:07:14 +02003859 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003860 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003861
Tejun Heob196be82012-01-10 15:11:35 -08003862 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003863 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003864 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003865 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003866 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003867 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003868 INIT_LIST_HEAD(&wq->flusher_queue);
3869 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003870 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003871
Johannes Bergeb13ba82008-01-16 09:51:58 +01003872 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003873 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003874
Tejun Heo30cdf242013-03-12 11:29:57 -07003875 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003876 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003877
Tejun Heo493008a2013-03-12 11:30:03 -07003878 /*
3879 * Workqueues which may be used during memory reclaim should
3880 * have a rescuer to guarantee forward progress.
3881 */
3882 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003883 struct worker *rescuer;
3884
Lai Jiangshanf7537df2014-07-15 17:24:15 +08003885 rescuer = alloc_worker(NUMA_NO_NODE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003886 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003887 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003888
Tejun Heo111c2252013-01-17 17:16:24 -08003889 rescuer->rescue_wq = wq;
3890 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003891 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003892 if (IS_ERR(rescuer->task)) {
3893 kfree(rescuer);
3894 goto err_destroy;
3895 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003896
Tejun Heod2c1d402013-03-12 11:30:04 -07003897 wq->rescuer = rescuer;
Peter Zijlstra25834c72015-05-15 17:43:34 +02003898 kthread_bind_mask(rescuer->task, cpu_possible_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003899 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003900 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003901
Tejun Heo226223a2013-03-12 11:30:05 -07003902 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3903 goto err_destroy;
3904
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003905 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003906 * wq_pool_mutex protects global freeze state and workqueues list.
3907 * Grab it, adjust max_active and add the new @wq to workqueues
3908 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003909 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003910 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003911
Lai Jiangshana357fc02013-03-25 16:57:19 -07003912 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003913 for_each_pwq(pwq, wq)
3914 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003915 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003916
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003917 list_add_tail_rcu(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003918
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003919 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003920
Oleg Nesterov3af244332007-05-09 02:34:09 -07003921 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003922
3923err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07003924 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07003925 kfree(wq);
3926 return NULL;
3927err_destroy:
3928 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003929 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003930}
Tejun Heod320c032010-06-29 10:07:14 +02003931EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003932
3933/**
3934 * destroy_workqueue - safely terminate a workqueue
3935 * @wq: target workqueue
3936 *
3937 * Safely destroy a workqueue. All work currently pending will be done first.
3938 */
3939void destroy_workqueue(struct workqueue_struct *wq)
3940{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003941 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003942 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003943
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003944 /* drain it before proceeding with destruction */
3945 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003946
Tejun Heo6183c002013-03-12 11:29:57 -07003947 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003948 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003949 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003950 int i;
3951
Tejun Heo76af4d92013-03-12 11:30:00 -07003952 for (i = 0; i < WORK_NR_COLORS; i++) {
3953 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003954 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003955 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003956 }
3957 }
3958
Lai Jiangshan5c529592013-04-04 10:05:38 +08003959 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07003960 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003961 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003962 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003963 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003964 }
Tejun Heo6183c002013-03-12 11:29:57 -07003965 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003966 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003967
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003968 /*
3969 * wq list is used to freeze wq, remove from list after
3970 * flushing is complete in case freeze races us.
3971 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003972 mutex_lock(&wq_pool_mutex);
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003973 list_del_rcu(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003974 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003975
Tejun Heo226223a2013-03-12 11:30:05 -07003976 workqueue_sysfs_unregister(wq);
3977
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003978 if (wq->rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02003979 kthread_stop(wq->rescuer->task);
Tejun Heoe22bee72010-06-29 10:07:14 +02003980
Tejun Heo8864b4e2013-03-12 11:30:04 -07003981 if (!(wq->flags & WQ_UNBOUND)) {
3982 /*
3983 * The base ref is never dropped on per-cpu pwqs. Directly
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003984 * schedule RCU free.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003985 */
Tejun Heoe2dca7a2015-03-09 09:22:28 -04003986 call_rcu_sched(&wq->rcu, rcu_free_wq);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003987 } else {
3988 /*
3989 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07003990 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
3991 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003992 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07003993 for_each_node(node) {
3994 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3995 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
3996 put_pwq_unlocked(pwq);
3997 }
3998
3999 /*
4000 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4001 * put. Don't access it afterwards.
4002 */
4003 pwq = wq->dfl_pwq;
4004 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004005 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004006 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004007}
4008EXPORT_SYMBOL_GPL(destroy_workqueue);
4009
Tejun Heodcd989c2010-06-29 10:07:14 +02004010/**
4011 * workqueue_set_max_active - adjust max_active of a workqueue
4012 * @wq: target workqueue
4013 * @max_active: new max_active value.
4014 *
4015 * Set max_active of @wq to @max_active.
4016 *
4017 * CONTEXT:
4018 * Don't call from IRQ context.
4019 */
4020void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4021{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004022 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004023
Tejun Heo8719dce2013-03-12 11:30:04 -07004024 /* disallow meddling with max_active for ordered workqueues */
4025 if (WARN_ON(wq->flags & __WQ_ORDERED))
4026 return;
4027
Tejun Heof3421792010-07-02 10:03:51 +02004028 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004029
Lai Jiangshana357fc02013-03-25 16:57:19 -07004030 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004031
4032 wq->saved_max_active = max_active;
4033
Tejun Heo699ce092013-03-13 16:51:35 -07004034 for_each_pwq(pwq, wq)
4035 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004036
Lai Jiangshana357fc02013-03-25 16:57:19 -07004037 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004038}
4039EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4040
4041/**
Tejun Heoe6267612013-03-12 17:41:37 -07004042 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4043 *
4044 * Determine whether %current is a workqueue rescuer. Can be used from
4045 * work functions to determine whether it's being run off the rescuer task.
Yacine Belkadid185af32013-07-31 14:59:24 -07004046 *
4047 * Return: %true if %current is a workqueue rescuer. %false otherwise.
Tejun Heoe6267612013-03-12 17:41:37 -07004048 */
4049bool current_is_workqueue_rescuer(void)
4050{
4051 struct worker *worker = current_wq_worker();
4052
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004053 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07004054}
4055
4056/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004057 * workqueue_congested - test whether a workqueue is congested
4058 * @cpu: CPU in question
4059 * @wq: target workqueue
4060 *
4061 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4062 * no synchronization around this function and the test result is
4063 * unreliable and only useful as advisory hints or for debugging.
4064 *
Tejun Heod3251852013-05-10 11:10:17 -07004065 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4066 * Note that both per-cpu and unbound workqueues may be associated with
4067 * multiple pool_workqueues which have separate congested states. A
4068 * workqueue being congested on one CPU doesn't mean the workqueue is also
4069 * contested on other CPUs / NUMA nodes.
4070 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004071 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004072 * %true if congested, %false otherwise.
4073 */
Tejun Heod84ff052013-03-12 11:29:59 -07004074bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004075{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004076 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004077 bool ret;
4078
Lai Jiangshan88109452013-03-20 03:28:10 +08004079 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004080
Tejun Heod3251852013-05-10 11:10:17 -07004081 if (cpu == WORK_CPU_UNBOUND)
4082 cpu = smp_processor_id();
4083
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004084 if (!(wq->flags & WQ_UNBOUND))
4085 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4086 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004087 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004088
Tejun Heo76af4d92013-03-12 11:30:00 -07004089 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004090 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004091
4092 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004093}
4094EXPORT_SYMBOL_GPL(workqueue_congested);
4095
4096/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004097 * work_busy - test whether a work is currently pending or running
4098 * @work: the work to be tested
4099 *
4100 * Test whether @work is currently pending or running. There is no
4101 * synchronization around this function and the test result is
4102 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004103 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004104 * Return:
Tejun Heodcd989c2010-06-29 10:07:14 +02004105 * OR'd bitmask of WORK_BUSY_* bits.
4106 */
4107unsigned int work_busy(struct work_struct *work)
4108{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004109 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004110 unsigned long flags;
4111 unsigned int ret = 0;
4112
Tejun Heodcd989c2010-06-29 10:07:14 +02004113 if (work_pending(work))
4114 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004115
Tejun Heofa1b54e2013-03-12 11:30:00 -07004116 local_irq_save(flags);
4117 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004118 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004119 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004120 if (find_worker_executing_work(pool, work))
4121 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004122 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004123 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004124 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004125
4126 return ret;
4127}
4128EXPORT_SYMBOL_GPL(work_busy);
4129
Tejun Heo3d1cb202013-04-30 15:27:22 -07004130/**
4131 * set_worker_desc - set description for the current work item
4132 * @fmt: printf-style format string
4133 * @...: arguments for the format string
4134 *
4135 * This function can be called by a running work function to describe what
4136 * the work item is about. If the worker task gets dumped, this
4137 * information will be printed out together to help debugging. The
4138 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4139 */
4140void set_worker_desc(const char *fmt, ...)
4141{
4142 struct worker *worker = current_wq_worker();
4143 va_list args;
4144
4145 if (worker) {
4146 va_start(args, fmt);
4147 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4148 va_end(args);
4149 worker->desc_valid = true;
4150 }
4151}
4152
4153/**
4154 * print_worker_info - print out worker information and description
4155 * @log_lvl: the log level to use when printing
4156 * @task: target task
4157 *
4158 * If @task is a worker and currently executing a work item, print out the
4159 * name of the workqueue being serviced and worker description set with
4160 * set_worker_desc() by the currently executing work item.
4161 *
4162 * This function can be safely called on any task as long as the
4163 * task_struct itself is accessible. While safe, this function isn't
4164 * synchronized and may print out mixups or garbages of limited length.
4165 */
4166void print_worker_info(const char *log_lvl, struct task_struct *task)
4167{
4168 work_func_t *fn = NULL;
4169 char name[WQ_NAME_LEN] = { };
4170 char desc[WORKER_DESC_LEN] = { };
4171 struct pool_workqueue *pwq = NULL;
4172 struct workqueue_struct *wq = NULL;
4173 bool desc_valid = false;
4174 struct worker *worker;
4175
4176 if (!(task->flags & PF_WQ_WORKER))
4177 return;
4178
4179 /*
4180 * This function is called without any synchronization and @task
4181 * could be in any state. Be careful with dereferences.
4182 */
4183 worker = probe_kthread_data(task);
4184
4185 /*
4186 * Carefully copy the associated workqueue's workfn and name. Keep
4187 * the original last '\0' in case the original contains garbage.
4188 */
4189 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4190 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4191 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4192 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4193
4194 /* copy worker description */
4195 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4196 if (desc_valid)
4197 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4198
4199 if (fn || name[0] || desc[0]) {
4200 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4201 if (desc[0])
4202 pr_cont(" (%s)", desc);
4203 pr_cont("\n");
4204 }
4205}
4206
Tejun Heo3494fc32015-03-09 09:22:28 -04004207static void pr_cont_pool_info(struct worker_pool *pool)
4208{
4209 pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);
4210 if (pool->node != NUMA_NO_NODE)
4211 pr_cont(" node=%d", pool->node);
4212 pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice);
4213}
4214
4215static void pr_cont_work(bool comma, struct work_struct *work)
4216{
4217 if (work->func == wq_barrier_func) {
4218 struct wq_barrier *barr;
4219
4220 barr = container_of(work, struct wq_barrier, work);
4221
4222 pr_cont("%s BAR(%d)", comma ? "," : "",
4223 task_pid_nr(barr->task));
4224 } else {
4225 pr_cont("%s %pf", comma ? "," : "", work->func);
4226 }
4227}
4228
4229static void show_pwq(struct pool_workqueue *pwq)
4230{
4231 struct worker_pool *pool = pwq->pool;
4232 struct work_struct *work;
4233 struct worker *worker;
4234 bool has_in_flight = false, has_pending = false;
4235 int bkt;
4236
4237 pr_info(" pwq %d:", pool->id);
4238 pr_cont_pool_info(pool);
4239
4240 pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active,
4241 !list_empty(&pwq->mayday_node) ? " MAYDAY" : "");
4242
4243 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4244 if (worker->current_pwq == pwq) {
4245 has_in_flight = true;
4246 break;
4247 }
4248 }
4249 if (has_in_flight) {
4250 bool comma = false;
4251
4252 pr_info(" in-flight:");
4253 hash_for_each(pool->busy_hash, bkt, worker, hentry) {
4254 if (worker->current_pwq != pwq)
4255 continue;
4256
4257 pr_cont("%s %d%s:%pf", comma ? "," : "",
4258 task_pid_nr(worker->task),
4259 worker == pwq->wq->rescuer ? "(RESCUER)" : "",
4260 worker->current_func);
4261 list_for_each_entry(work, &worker->scheduled, entry)
4262 pr_cont_work(false, work);
4263 comma = true;
4264 }
4265 pr_cont("\n");
4266 }
4267
4268 list_for_each_entry(work, &pool->worklist, entry) {
4269 if (get_work_pwq(work) == pwq) {
4270 has_pending = true;
4271 break;
4272 }
4273 }
4274 if (has_pending) {
4275 bool comma = false;
4276
4277 pr_info(" pending:");
4278 list_for_each_entry(work, &pool->worklist, entry) {
4279 if (get_work_pwq(work) != pwq)
4280 continue;
4281
4282 pr_cont_work(comma, work);
4283 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4284 }
4285 pr_cont("\n");
4286 }
4287
4288 if (!list_empty(&pwq->delayed_works)) {
4289 bool comma = false;
4290
4291 pr_info(" delayed:");
4292 list_for_each_entry(work, &pwq->delayed_works, entry) {
4293 pr_cont_work(comma, work);
4294 comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);
4295 }
4296 pr_cont("\n");
4297 }
4298}
4299
4300/**
4301 * show_workqueue_state - dump workqueue state
4302 *
Roger Lueb25d9a2016-07-01 11:05:02 +08004303 * Called from a sysrq handler or try_to_freeze_tasks() and prints out
4304 * all busy workqueues and pools.
Tejun Heo3494fc32015-03-09 09:22:28 -04004305 */
4306void show_workqueue_state(void)
4307{
4308 struct workqueue_struct *wq;
4309 struct worker_pool *pool;
4310 unsigned long flags;
4311 int pi;
4312
4313 rcu_read_lock_sched();
4314
4315 pr_info("Showing busy workqueues and worker pools:\n");
4316
4317 list_for_each_entry_rcu(wq, &workqueues, list) {
4318 struct pool_workqueue *pwq;
4319 bool idle = true;
4320
4321 for_each_pwq(pwq, wq) {
4322 if (pwq->nr_active || !list_empty(&pwq->delayed_works)) {
4323 idle = false;
4324 break;
4325 }
4326 }
4327 if (idle)
4328 continue;
4329
4330 pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags);
4331
4332 for_each_pwq(pwq, wq) {
4333 spin_lock_irqsave(&pwq->pool->lock, flags);
4334 if (pwq->nr_active || !list_empty(&pwq->delayed_works))
4335 show_pwq(pwq);
4336 spin_unlock_irqrestore(&pwq->pool->lock, flags);
4337 }
4338 }
4339
4340 for_each_pool(pool, pi) {
4341 struct worker *worker;
4342 bool first = true;
4343
4344 spin_lock_irqsave(&pool->lock, flags);
4345 if (pool->nr_workers == pool->nr_idle)
4346 goto next_pool;
4347
4348 pr_info("pool %d:", pool->id);
4349 pr_cont_pool_info(pool);
4350 pr_cont(" workers=%d", pool->nr_workers);
4351 if (pool->manager)
4352 pr_cont(" manager: %d",
4353 task_pid_nr(pool->manager->task));
4354 list_for_each_entry(worker, &pool->idle_list, entry) {
4355 pr_cont(" %s%d", first ? "idle: " : "",
4356 task_pid_nr(worker->task));
4357 first = false;
4358 }
4359 pr_cont("\n");
4360 next_pool:
4361 spin_unlock_irqrestore(&pool->lock, flags);
4362 }
4363
4364 rcu_read_unlock_sched();
4365}
4366
Tejun Heodb7bccf2010-06-29 10:07:12 +02004367/*
4368 * CPU hotplug.
4369 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004370 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004371 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004372 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004373 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004374 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004375 * blocked draining impractical.
4376 *
Tejun Heo24647572013-01-24 11:01:33 -08004377 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004378 * running as an unbound one and allowing it to be reattached later if the
4379 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004380 */
4381
Tejun Heo706026c2013-01-24 11:01:34 -08004382static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004383{
Tejun Heo38db41d2013-01-24 11:01:34 -08004384 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004385 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004386 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004387
Tejun Heof02ae732013-03-12 11:30:03 -07004388 for_each_cpu_worker_pool(pool, cpu) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004389 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004390 spin_lock_irq(&pool->lock);
4391
4392 /*
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004393 * We've blocked all attach/detach operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004394 * unbound and set DISASSOCIATED. Before this, all workers
4395 * except for the ones which are still executing works from
4396 * before the last CPU down must be on the cpu. After
4397 * this, they may become diasporas.
4398 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004399 for_each_pool_worker(worker, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004400 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004401
Tejun Heo24647572013-01-24 11:01:33 -08004402 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004403
Tejun Heo94cf58b2013-01-24 11:01:33 -08004404 spin_unlock_irq(&pool->lock);
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004405 mutex_unlock(&pool->attach_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004406
Lai Jiangshaneb283422013-03-08 15:18:28 -08004407 /*
4408 * Call schedule() so that we cross rq->lock and thus can
4409 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4410 * This is necessary as scheduler callbacks may be invoked
4411 * from other cpus.
4412 */
4413 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004414
Lai Jiangshaneb283422013-03-08 15:18:28 -08004415 /*
4416 * Sched callbacks are disabled now. Zap nr_running.
4417 * After this, nr_running stays zero and need_more_worker()
4418 * and keep_working() are always true as long as the
4419 * worklist is not empty. This pool now behaves as an
4420 * unbound (in terms of concurrency management) pool which
4421 * are served by workers tied to the pool.
4422 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004423 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004424
4425 /*
4426 * With concurrency management just turned off, a busy
4427 * worker blocking could lead to lengthy stalls. Kick off
4428 * unbound chain execution of currently pending work items.
4429 */
4430 spin_lock_irq(&pool->lock);
4431 wake_up_worker(pool);
4432 spin_unlock_irq(&pool->lock);
4433 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004434}
4435
Tejun Heobd7c0892013-03-19 13:45:21 -07004436/**
4437 * rebind_workers - rebind all workers of a pool to the associated CPU
4438 * @pool: pool of interest
4439 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004440 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004441 */
4442static void rebind_workers(struct worker_pool *pool)
4443{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004444 struct worker *worker;
Tejun Heobd7c0892013-03-19 13:45:21 -07004445
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004446 lockdep_assert_held(&pool->attach_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004447
Tejun Heoa9ab7752013-03-19 13:45:21 -07004448 /*
4449 * Restore CPU affinity of all workers. As all idle workers should
4450 * be on the run-queue of the associated CPU before any local
Shailendra Verma402dd892015-05-23 10:38:14 +05304451 * wake-ups for concurrency management happen, restore CPU affinity
Tejun Heoa9ab7752013-03-19 13:45:21 -07004452 * of all workers first and then clear UNBOUND. As we're called
4453 * from CPU_ONLINE, the following shouldn't fail.
4454 */
Lai Jiangshanda028462014-05-20 17:46:31 +08004455 for_each_pool_worker(worker, pool)
Tejun Heoa9ab7752013-03-19 13:45:21 -07004456 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4457 pool->attrs->cpumask) < 0);
4458
4459 spin_lock_irq(&pool->lock);
Wanpeng Li242bd1b2016-05-11 17:55:18 +08004460
4461 /*
4462 * XXX: CPU hotplug notifiers are weird and can call DOWN_FAILED
4463 * w/o preceding DOWN_PREPARE. Work around it. CPU hotplug is
4464 * being reworked and this can go away in time.
4465 */
4466 if (!(pool->flags & POOL_DISASSOCIATED)) {
4467 spin_unlock_irq(&pool->lock);
4468 return;
4469 }
4470
Lai Jiangshan3de5e882014-06-03 15:33:27 +08004471 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004472
Lai Jiangshanda028462014-05-20 17:46:31 +08004473 for_each_pool_worker(worker, pool) {
Tejun Heoa9ab7752013-03-19 13:45:21 -07004474 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004475
4476 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004477 * A bound idle worker should actually be on the runqueue
4478 * of the associated CPU for local wake-ups targeting it to
4479 * work. Kick all idle workers so that they migrate to the
4480 * associated CPU. Doing this in the same loop as
4481 * replacing UNBOUND with REBOUND is safe as no worker will
4482 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004483 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004484 if (worker_flags & WORKER_IDLE)
4485 wake_up_process(worker->task);
4486
4487 /*
4488 * We want to clear UNBOUND but can't directly call
4489 * worker_clr_flags() or adjust nr_running. Atomically
4490 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4491 * @worker will clear REBOUND using worker_clr_flags() when
4492 * it initiates the next execution cycle thus restoring
4493 * concurrency management. Note that when or whether
4494 * @worker clears REBOUND doesn't affect correctness.
4495 *
4496 * ACCESS_ONCE() is necessary because @worker->flags may be
4497 * tested without holding any lock in
4498 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4499 * fail incorrectly leading to premature concurrency
4500 * management operations.
4501 */
4502 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4503 worker_flags |= WORKER_REBOUND;
4504 worker_flags &= ~WORKER_UNBOUND;
4505 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004506 }
4507
Tejun Heoa9ab7752013-03-19 13:45:21 -07004508 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004509}
4510
Tejun Heo7dbc7252013-03-19 13:45:21 -07004511/**
4512 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4513 * @pool: unbound pool of interest
4514 * @cpu: the CPU which is coming up
4515 *
4516 * An unbound pool may end up with a cpumask which doesn't have any online
4517 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4518 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4519 * online CPU before, cpus_allowed of all its workers should be restored.
4520 */
4521static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4522{
4523 static cpumask_t cpumask;
4524 struct worker *worker;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004525
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004526 lockdep_assert_held(&pool->attach_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004527
4528 /* is @cpu allowed for @pool? */
4529 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4530 return;
4531
4532 /* is @cpu the only online CPU? */
4533 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4534 if (cpumask_weight(&cpumask) != 1)
4535 return;
4536
4537 /* as we're called from CPU_ONLINE, the following shouldn't fail */
Lai Jiangshanda028462014-05-20 17:46:31 +08004538 for_each_pool_worker(worker, pool)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004539 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4540 pool->attrs->cpumask) < 0);
4541}
4542
Tejun Heo8db25e72012-07-17 12:39:28 -07004543/*
4544 * Workqueues should be brought up before normal priority CPU notifiers.
4545 * This will be registered high priority CPU notifier.
4546 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004547static int workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004548 unsigned long action,
4549 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004550{
Tejun Heod84ff052013-03-12 11:29:59 -07004551 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004552 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004553 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004554 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Tejun Heo8db25e72012-07-17 12:39:28 -07004556 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004558 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004559 if (pool->nr_workers)
4560 continue;
Lai Jiangshan051e1852014-07-22 13:03:02 +08004561 if (!create_worker(pool))
Tejun Heo3ce63372012-07-17 12:39:27 -07004562 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004564 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004565
Tejun Heo65758202012-07-17 12:39:26 -07004566 case CPU_DOWN_FAILED:
4567 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004568 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004569
4570 for_each_pool(pool, pi) {
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004571 mutex_lock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004572
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004573 if (pool->cpu == cpu)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004574 rebind_workers(pool);
Lai Jiangshanf05b5582014-06-03 15:33:27 +08004575 else if (pool->cpu < 0)
Tejun Heo7dbc7252013-03-19 13:45:21 -07004576 restore_unbound_workers_cpumask(pool, cpu);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004577
Lai Jiangshan92f9c5c2014-05-20 17:46:34 +08004578 mutex_unlock(&pool->attach_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004579 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004580
Tejun Heo4c16bd32013-04-01 11:23:36 -07004581 /* update NUMA affinity of unbound workqueues */
4582 list_for_each_entry(wq, &workqueues, list)
4583 wq_update_unbound_numa(wq, cpu, true);
4584
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004585 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004586 break;
Tejun Heo65758202012-07-17 12:39:26 -07004587 }
4588 return NOTIFY_OK;
4589}
4590
4591/*
4592 * Workqueues should be brought down after normal priority CPU notifiers.
4593 * This will be registered as low priority CPU notifier.
4594 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004595static int workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004596 unsigned long action,
4597 void *hcpu)
4598{
Tejun Heod84ff052013-03-12 11:29:59 -07004599 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004600 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004601 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004602
Tejun Heo65758202012-07-17 12:39:26 -07004603 switch (action & ~CPU_TASKS_FROZEN) {
4604 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004605 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004606 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004607 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004608
4609 /* update NUMA affinity of unbound workqueues */
4610 mutex_lock(&wq_pool_mutex);
4611 list_for_each_entry(wq, &workqueues, list)
4612 wq_update_unbound_numa(wq, cpu, false);
4613 mutex_unlock(&wq_pool_mutex);
4614
4615 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004616 flush_work(&unbind_work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004617 destroy_work_on_stack(&unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004618 break;
Tejun Heo65758202012-07-17 12:39:26 -07004619 }
4620 return NOTIFY_OK;
4621}
4622
Rusty Russell2d3854a2008-11-05 13:39:10 +11004623#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004624
Rusty Russell2d3854a2008-11-05 13:39:10 +11004625struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004626 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004627 long (*fn)(void *);
4628 void *arg;
4629 long ret;
4630};
4631
Tejun Heoed48ece2012-09-18 12:48:43 -07004632static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004633{
Tejun Heoed48ece2012-09-18 12:48:43 -07004634 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4635
Rusty Russell2d3854a2008-11-05 13:39:10 +11004636 wfc->ret = wfc->fn(wfc->arg);
4637}
4638
4639/**
4640 * work_on_cpu - run a function in user context on a particular cpu
4641 * @cpu: the cpu to run on
4642 * @fn: the function to run
4643 * @arg: the function arg
4644 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004645 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004646 * The caller must not hold any locks which would prevent @fn from completing.
Yacine Belkadid185af32013-07-31 14:59:24 -07004647 *
4648 * Return: The value @fn returns.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004649 */
Tejun Heod84ff052013-03-12 11:29:59 -07004650long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004651{
Tejun Heoed48ece2012-09-18 12:48:43 -07004652 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004653
Tejun Heoed48ece2012-09-18 12:48:43 -07004654 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4655 schedule_work_on(cpu, &wfc.work);
Bjorn Helgaas12997d12013-11-18 11:00:29 -07004656 flush_work(&wfc.work);
Chuansheng Liu440a1132014-01-11 22:26:33 -05004657 destroy_work_on_stack(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004658 return wfc.ret;
4659}
4660EXPORT_SYMBOL_GPL(work_on_cpu);
4661#endif /* CONFIG_SMP */
4662
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004663#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304664
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004665/**
4666 * freeze_workqueues_begin - begin freezing workqueues
4667 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004668 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004669 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004670 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004671 *
4672 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004673 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004674 */
4675void freeze_workqueues_begin(void)
4676{
Tejun Heo24b8a842013-03-12 11:29:58 -07004677 struct workqueue_struct *wq;
4678 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004679
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004680 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004681
Tejun Heo6183c002013-03-12 11:29:57 -07004682 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004683 workqueue_freezing = true;
4684
Tejun Heo24b8a842013-03-12 11:29:58 -07004685 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004686 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004687 for_each_pwq(pwq, wq)
4688 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004689 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004690 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004691
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004692 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004694
4695/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004696 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004697 *
4698 * Check whether freezing is complete. This function must be called
4699 * between freeze_workqueues_begin() and thaw_workqueues().
4700 *
4701 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004702 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004703 *
Yacine Belkadid185af32013-07-31 14:59:24 -07004704 * Return:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004705 * %true if some freezable workqueues are still busy. %false if freezing
4706 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004707 */
4708bool freeze_workqueues_busy(void)
4709{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004710 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004711 struct workqueue_struct *wq;
4712 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004713
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004714 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004715
Tejun Heo6183c002013-03-12 11:29:57 -07004716 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004717
Tejun Heo24b8a842013-03-12 11:29:58 -07004718 list_for_each_entry(wq, &workqueues, list) {
4719 if (!(wq->flags & WQ_FREEZABLE))
4720 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004721 /*
4722 * nr_active is monotonically decreasing. It's safe
4723 * to peek without lock.
4724 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004725 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004726 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004727 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004728 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004729 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004730 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004731 goto out_unlock;
4732 }
4733 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004734 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004735 }
4736out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004737 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004738 return busy;
4739}
4740
4741/**
4742 * thaw_workqueues - thaw workqueues
4743 *
4744 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004745 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004746 *
4747 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004748 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004749 */
4750void thaw_workqueues(void)
4751{
Tejun Heo24b8a842013-03-12 11:29:58 -07004752 struct workqueue_struct *wq;
4753 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004754
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004755 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004756
4757 if (!workqueue_freezing)
4758 goto out_unlock;
4759
Lai Jiangshan74b414e2014-05-22 19:01:16 +08004760 workqueue_freezing = false;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004761
Tejun Heo24b8a842013-03-12 11:29:58 -07004762 /* restore max_active and repopulate worklist */
4763 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004764 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004765 for_each_pwq(pwq, wq)
4766 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004767 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004768 }
4769
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004770out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004771 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004772}
4773#endif /* CONFIG_FREEZER */
4774
Lai Jiangshan042f7df2015-04-30 17:16:12 +08004775static int workqueue_apply_unbound_cpumask(void)
4776{
4777 LIST_HEAD(ctxs);
4778 int ret = 0;
4779 struct workqueue_struct *wq;
4780 struct apply_wqattrs_ctx *ctx, *n;
4781
4782 lockdep_assert_held(&wq_pool_mutex);
4783
4784 list_for_each_entry(wq, &workqueues, list) {
4785 if (!(wq->flags & WQ_UNBOUND))
4786 continue;
4787 /* creating multiple pwqs breaks ordering guarantee */
4788 if (wq->flags & __WQ_ORDERED)
4789 continue;
4790
4791 ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs);
4792 if (!ctx) {
4793 ret = -ENOMEM;
4794 break;
4795 }
4796
4797 list_add_tail(&ctx->list, &ctxs);
4798 }
4799
4800 list_for_each_entry_safe(ctx, n, &ctxs, list) {
4801 if (!ret)
4802 apply_wqattrs_commit(ctx);
4803 apply_wqattrs_cleanup(ctx);
4804 }
4805
4806 return ret;
4807}
4808
4809/**
4810 * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask
4811 * @cpumask: the cpumask to set
4812 *
4813 * The low-level workqueues cpumask is a global cpumask that limits
4814 * the affinity of all unbound workqueues. This function check the @cpumask
4815 * and apply it to all unbound workqueues and updates all pwqs of them.
4816 *
4817 * Retun: 0 - Success
4818 * -EINVAL - Invalid @cpumask
4819 * -ENOMEM - Failed to allocate memory for attrs or pwqs.
4820 */
4821int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
4822{
4823 int ret = -EINVAL;
4824 cpumask_var_t saved_cpumask;
4825
4826 if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL))
4827 return -ENOMEM;
4828
Lai Jiangshan042f7df2015-04-30 17:16:12 +08004829 cpumask_and(cpumask, cpumask, cpu_possible_mask);
4830 if (!cpumask_empty(cpumask)) {
Lai Jiangshana0111cf2015-05-19 18:03:47 +08004831 apply_wqattrs_lock();
Lai Jiangshan042f7df2015-04-30 17:16:12 +08004832
4833 /* save the old wq_unbound_cpumask. */
4834 cpumask_copy(saved_cpumask, wq_unbound_cpumask);
4835
4836 /* update wq_unbound_cpumask at first and apply it to wqs. */
4837 cpumask_copy(wq_unbound_cpumask, cpumask);
4838 ret = workqueue_apply_unbound_cpumask();
4839
4840 /* restore the wq_unbound_cpumask when failed. */
4841 if (ret < 0)
4842 cpumask_copy(wq_unbound_cpumask, saved_cpumask);
4843
Lai Jiangshana0111cf2015-05-19 18:03:47 +08004844 apply_wqattrs_unlock();
Lai Jiangshan042f7df2015-04-30 17:16:12 +08004845 }
Lai Jiangshan042f7df2015-04-30 17:16:12 +08004846
4847 free_cpumask_var(saved_cpumask);
4848 return ret;
4849}
4850
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004851#ifdef CONFIG_SYSFS
4852/*
4853 * Workqueues with WQ_SYSFS flag set is visible to userland via
4854 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
4855 * following attributes.
4856 *
4857 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
4858 * max_active RW int : maximum number of in-flight work items
4859 *
4860 * Unbound workqueues have the following extra attributes.
4861 *
4862 * id RO int : the associated pool ID
4863 * nice RW int : nice value of the workers
4864 * cpumask RW mask : bitmask of allowed CPUs for the workers
4865 */
4866struct wq_device {
4867 struct workqueue_struct *wq;
4868 struct device dev;
4869};
4870
4871static struct workqueue_struct *dev_to_wq(struct device *dev)
4872{
4873 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
4874
4875 return wq_dev->wq;
4876}
4877
4878static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
4879 char *buf)
4880{
4881 struct workqueue_struct *wq = dev_to_wq(dev);
4882
4883 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
4884}
4885static DEVICE_ATTR_RO(per_cpu);
4886
4887static ssize_t max_active_show(struct device *dev,
4888 struct device_attribute *attr, char *buf)
4889{
4890 struct workqueue_struct *wq = dev_to_wq(dev);
4891
4892 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
4893}
4894
4895static ssize_t max_active_store(struct device *dev,
4896 struct device_attribute *attr, const char *buf,
4897 size_t count)
4898{
4899 struct workqueue_struct *wq = dev_to_wq(dev);
4900 int val;
4901
4902 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
4903 return -EINVAL;
4904
4905 workqueue_set_max_active(wq, val);
4906 return count;
4907}
4908static DEVICE_ATTR_RW(max_active);
4909
4910static struct attribute *wq_sysfs_attrs[] = {
4911 &dev_attr_per_cpu.attr,
4912 &dev_attr_max_active.attr,
4913 NULL,
4914};
4915ATTRIBUTE_GROUPS(wq_sysfs);
4916
4917static ssize_t wq_pool_ids_show(struct device *dev,
4918 struct device_attribute *attr, char *buf)
4919{
4920 struct workqueue_struct *wq = dev_to_wq(dev);
4921 const char *delim = "";
4922 int node, written = 0;
4923
4924 rcu_read_lock_sched();
4925 for_each_node(node) {
4926 written += scnprintf(buf + written, PAGE_SIZE - written,
4927 "%s%d:%d", delim, node,
4928 unbound_pwq_by_node(wq, node)->pool->id);
4929 delim = " ";
4930 }
4931 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
4932 rcu_read_unlock_sched();
4933
4934 return written;
4935}
4936
4937static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
4938 char *buf)
4939{
4940 struct workqueue_struct *wq = dev_to_wq(dev);
4941 int written;
4942
4943 mutex_lock(&wq->mutex);
4944 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
4945 mutex_unlock(&wq->mutex);
4946
4947 return written;
4948}
4949
4950/* prepare workqueue_attrs for sysfs store operations */
4951static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
4952{
4953 struct workqueue_attrs *attrs;
4954
Lai Jiangshan899a94f2015-05-20 14:41:18 +08004955 lockdep_assert_held(&wq_pool_mutex);
4956
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004957 attrs = alloc_workqueue_attrs(GFP_KERNEL);
4958 if (!attrs)
4959 return NULL;
4960
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004961 copy_workqueue_attrs(attrs, wq->unbound_attrs);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004962 return attrs;
4963}
4964
4965static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
4966 const char *buf, size_t count)
4967{
4968 struct workqueue_struct *wq = dev_to_wq(dev);
4969 struct workqueue_attrs *attrs;
Lai Jiangshand4d3e252015-05-19 18:03:48 +08004970 int ret = -ENOMEM;
4971
4972 apply_wqattrs_lock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004973
4974 attrs = wq_sysfs_prep_attrs(wq);
4975 if (!attrs)
Lai Jiangshand4d3e252015-05-19 18:03:48 +08004976 goto out_unlock;
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004977
4978 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
4979 attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
Lai Jiangshand4d3e252015-05-19 18:03:48 +08004980 ret = apply_workqueue_attrs_locked(wq, attrs);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004981 else
4982 ret = -EINVAL;
4983
Lai Jiangshand4d3e252015-05-19 18:03:48 +08004984out_unlock:
4985 apply_wqattrs_unlock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08004986 free_workqueue_attrs(attrs);
4987 return ret ?: count;
4988}
4989
4990static ssize_t wq_cpumask_show(struct device *dev,
4991 struct device_attribute *attr, char *buf)
4992{
4993 struct workqueue_struct *wq = dev_to_wq(dev);
4994 int written;
4995
4996 mutex_lock(&wq->mutex);
4997 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
4998 cpumask_pr_args(wq->unbound_attrs->cpumask));
4999 mutex_unlock(&wq->mutex);
5000 return written;
5001}
5002
5003static ssize_t wq_cpumask_store(struct device *dev,
5004 struct device_attribute *attr,
5005 const char *buf, size_t count)
5006{
5007 struct workqueue_struct *wq = dev_to_wq(dev);
5008 struct workqueue_attrs *attrs;
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005009 int ret = -ENOMEM;
5010
5011 apply_wqattrs_lock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005012
5013 attrs = wq_sysfs_prep_attrs(wq);
5014 if (!attrs)
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005015 goto out_unlock;
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005016
5017 ret = cpumask_parse(buf, attrs->cpumask);
5018 if (!ret)
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005019 ret = apply_workqueue_attrs_locked(wq, attrs);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005020
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005021out_unlock:
5022 apply_wqattrs_unlock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005023 free_workqueue_attrs(attrs);
5024 return ret ?: count;
5025}
5026
5027static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
5028 char *buf)
5029{
5030 struct workqueue_struct *wq = dev_to_wq(dev);
5031 int written;
5032
5033 mutex_lock(&wq->mutex);
5034 written = scnprintf(buf, PAGE_SIZE, "%d\n",
5035 !wq->unbound_attrs->no_numa);
5036 mutex_unlock(&wq->mutex);
5037
5038 return written;
5039}
5040
5041static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
5042 const char *buf, size_t count)
5043{
5044 struct workqueue_struct *wq = dev_to_wq(dev);
5045 struct workqueue_attrs *attrs;
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005046 int v, ret = -ENOMEM;
5047
5048 apply_wqattrs_lock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005049
5050 attrs = wq_sysfs_prep_attrs(wq);
5051 if (!attrs)
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005052 goto out_unlock;
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005053
5054 ret = -EINVAL;
5055 if (sscanf(buf, "%d", &v) == 1) {
5056 attrs->no_numa = !v;
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005057 ret = apply_workqueue_attrs_locked(wq, attrs);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005058 }
5059
Lai Jiangshand4d3e252015-05-19 18:03:48 +08005060out_unlock:
5061 apply_wqattrs_unlock();
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005062 free_workqueue_attrs(attrs);
5063 return ret ?: count;
5064}
5065
5066static struct device_attribute wq_sysfs_unbound_attrs[] = {
5067 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
5068 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
5069 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
5070 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
5071 __ATTR_NULL,
5072};
5073
5074static struct bus_type wq_subsys = {
5075 .name = "workqueue",
5076 .dev_groups = wq_sysfs_groups,
5077};
5078
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005079static ssize_t wq_unbound_cpumask_show(struct device *dev,
5080 struct device_attribute *attr, char *buf)
5081{
5082 int written;
5083
Lai Jiangshan042f7df2015-04-30 17:16:12 +08005084 mutex_lock(&wq_pool_mutex);
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005085 written = scnprintf(buf, PAGE_SIZE, "%*pb\n",
5086 cpumask_pr_args(wq_unbound_cpumask));
Lai Jiangshan042f7df2015-04-30 17:16:12 +08005087 mutex_unlock(&wq_pool_mutex);
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005088
5089 return written;
5090}
5091
Lai Jiangshan042f7df2015-04-30 17:16:12 +08005092static ssize_t wq_unbound_cpumask_store(struct device *dev,
5093 struct device_attribute *attr, const char *buf, size_t count)
5094{
5095 cpumask_var_t cpumask;
5096 int ret;
5097
5098 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
5099 return -ENOMEM;
5100
5101 ret = cpumask_parse(buf, cpumask);
5102 if (!ret)
5103 ret = workqueue_set_unbound_cpumask(cpumask);
5104
5105 free_cpumask_var(cpumask);
5106 return ret ? ret : count;
5107}
5108
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005109static struct device_attribute wq_sysfs_cpumask_attr =
Lai Jiangshan042f7df2015-04-30 17:16:12 +08005110 __ATTR(cpumask, 0644, wq_unbound_cpumask_show,
5111 wq_unbound_cpumask_store);
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005112
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005113static int __init wq_sysfs_init(void)
5114{
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005115 int err;
5116
5117 err = subsys_virtual_register(&wq_subsys, NULL);
5118 if (err)
5119 return err;
5120
5121 return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr);
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005122}
5123core_initcall(wq_sysfs_init);
5124
5125static void wq_device_release(struct device *dev)
5126{
5127 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
5128
5129 kfree(wq_dev);
5130}
5131
5132/**
5133 * workqueue_sysfs_register - make a workqueue visible in sysfs
5134 * @wq: the workqueue to register
5135 *
5136 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
5137 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
5138 * which is the preferred method.
5139 *
5140 * Workqueue user should use this function directly iff it wants to apply
5141 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
5142 * apply_workqueue_attrs() may race against userland updating the
5143 * attributes.
5144 *
5145 * Return: 0 on success, -errno on failure.
5146 */
5147int workqueue_sysfs_register(struct workqueue_struct *wq)
5148{
5149 struct wq_device *wq_dev;
5150 int ret;
5151
5152 /*
Shailendra Verma402dd892015-05-23 10:38:14 +05305153 * Adjusting max_active or creating new pwqs by applying
Frederic Weisbecker6ba94422015-04-02 19:14:39 +08005154 * attributes breaks ordering guarantee. Disallow exposing ordered
5155 * workqueues.
5156 */
5157 if (WARN_ON(wq->flags & __WQ_ORDERED))
5158 return -EINVAL;
5159
5160 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
5161 if (!wq_dev)
5162 return -ENOMEM;
5163
5164 wq_dev->wq = wq;
5165 wq_dev->dev.bus = &wq_subsys;
5166 wq_dev->dev.init_name = wq->name;
5167 wq_dev->dev.release = wq_device_release;
5168
5169 /*
5170 * unbound_attrs are created separately. Suppress uevent until
5171 * everything is ready.
5172 */
5173 dev_set_uevent_suppress(&wq_dev->dev, true);
5174
5175 ret = device_register(&wq_dev->dev);
5176 if (ret) {
5177 kfree(wq_dev);
5178 wq->wq_dev = NULL;
5179 return ret;
5180 }
5181
5182 if (wq->flags & WQ_UNBOUND) {
5183 struct device_attribute *attr;
5184
5185 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
5186 ret = device_create_file(&wq_dev->dev, attr);
5187 if (ret) {
5188 device_unregister(&wq_dev->dev);
5189 wq->wq_dev = NULL;
5190 return ret;
5191 }
5192 }
5193 }
5194
5195 dev_set_uevent_suppress(&wq_dev->dev, false);
5196 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
5197 return 0;
5198}
5199
5200/**
5201 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
5202 * @wq: the workqueue to unregister
5203 *
5204 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
5205 */
5206static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
5207{
5208 struct wq_device *wq_dev = wq->wq_dev;
5209
5210 if (!wq->wq_dev)
5211 return;
5212
5213 wq->wq_dev = NULL;
5214 device_unregister(&wq_dev->dev);
5215}
5216#else /* CONFIG_SYSFS */
5217static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
5218#endif /* CONFIG_SYSFS */
5219
Tejun Heobce90382013-04-01 11:23:32 -07005220static void __init wq_numa_init(void)
5221{
5222 cpumask_var_t *tbl;
5223 int node, cpu;
5224
Tejun Heobce90382013-04-01 11:23:32 -07005225 if (num_possible_nodes() <= 1)
5226 return;
5227
Tejun Heod55262c2013-04-01 11:23:38 -07005228 if (wq_disable_numa) {
5229 pr_info("workqueue: NUMA affinity support disabled\n");
5230 return;
5231 }
5232
Tejun Heo4c16bd32013-04-01 11:23:36 -07005233 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
5234 BUG_ON(!wq_update_unbound_numa_attrs_buf);
5235
Tejun Heobce90382013-04-01 11:23:32 -07005236 /*
5237 * We want masks of possible CPUs of each node which isn't readily
5238 * available. Build one from cpu_to_node() which should have been
5239 * fully initialized by now.
5240 */
Lai Jiangshanddcb57e2014-07-22 13:05:40 +08005241 tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL);
Tejun Heobce90382013-04-01 11:23:32 -07005242 BUG_ON(!tbl);
5243
5244 for_each_node(node)
Yasuaki Ishimatsu5a6024f2014-07-07 09:56:48 -04005245 BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
Tejun Heo1be0c252013-05-15 14:24:24 -07005246 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07005247
5248 for_each_possible_cpu(cpu) {
5249 node = cpu_to_node(cpu);
5250 if (WARN_ON(node == NUMA_NO_NODE)) {
5251 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
5252 /* happens iff arch is bonkers, let's just proceed */
Vandana Salve20029c42016-09-13 15:44:03 +05305253 kfree(tbl);
Tejun Heobce90382013-04-01 11:23:32 -07005254 return;
5255 }
5256 cpumask_set_cpu(cpu, tbl[node]);
5257 }
5258
5259 wq_numa_possible_cpumask = tbl;
5260 wq_numa_enabled = true;
5261}
5262
Suresh Siddha6ee05782010-07-30 14:57:37 -07005263static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264{
Tejun Heo7a4e3442013-03-12 11:30:00 -07005265 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
5266 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02005267
Tejun Heoe904e6c2013-03-12 11:29:57 -07005268 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
5269
Frederic Weisbeckerb05a7922015-04-27 17:58:39 +08005270 BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL));
5271 cpumask_copy(wq_unbound_cpumask, cpu_possible_mask);
5272
Tejun Heoe904e6c2013-03-12 11:29:57 -07005273 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
5274
Tejun Heo65758202012-07-17 12:39:26 -07005275 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07005276 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02005277
Tejun Heobce90382013-04-01 11:23:32 -07005278 wq_numa_init();
5279
Tejun Heo706026c2013-01-24 11:01:34 -08005280 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07005281 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005282 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02005283
Tejun Heo7a4e3442013-03-12 11:30:00 -07005284 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07005285 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07005286 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08005287 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07005288 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07005289 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07005290 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07005291
Tejun Heo9daf9e62013-01-24 11:01:33 -08005292 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005293 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08005294 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07005295 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07005296 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02005297 }
5298
Tejun Heoe22bee72010-06-29 10:07:14 +02005299 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07005300 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07005301 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02005302
Tejun Heof02ae732013-03-12 11:30:03 -07005303 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07005304 pool->flags &= ~POOL_DISASSOCIATED;
Lai Jiangshan051e1852014-07-22 13:03:02 +08005305 BUG_ON(!create_worker(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07005306 }
Tejun Heoe22bee72010-06-29 10:07:14 +02005307 }
5308
Tejun Heo8a2b7532013-09-05 12:30:04 -04005309 /* create default unbound and ordered wq attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -07005310 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
5311 struct workqueue_attrs *attrs;
5312
5313 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07005314 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07005315 unbound_std_wq_attrs[i] = attrs;
Tejun Heo8a2b7532013-09-05 12:30:04 -04005316
5317 /*
5318 * An ordered wq should have only one pwq as ordering is
5319 * guaranteed by max_active which is enforced by pwqs.
5320 * Turn off NUMA so that dfl_pwq is used for all nodes.
5321 */
5322 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
5323 attrs->nice = std_nice[i];
5324 attrs->no_numa = true;
5325 ordered_wq_attrs[i] = attrs;
Tejun Heo29c91e92013-03-12 11:30:03 -07005326 }
5327
Tejun Heod320c032010-06-29 10:07:14 +02005328 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005329 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005330 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005331 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5332 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005333 system_freezable_wq = alloc_workqueue("events_freezable",
5334 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305335 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5336 WQ_POWER_EFFICIENT, 0);
5337 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5338 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5339 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005340 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305341 !system_unbound_wq || !system_freezable_wq ||
5342 !system_power_efficient_wq ||
5343 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005346early_initcall(init_workqueues);