blob: d1b5f06626515ac3219da3700abe61db12ea867a [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
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070044#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050045#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070046#include <linux/rculist.h>
Tejun Heobce90382013-04-01 11:23:32 -070047#include <linux/nodemask.h>
Tejun Heo4c16bd32013-04-01 11:23:36 -070048#include <linux/moduleparam.h>
Tejun Heo3d1cb202013-04-30 15:27:22 -070049#include <linux/uaccess.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020050
Tejun Heoea138442013-01-18 14:05:55 -080051#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Tejun Heoc8e55f32010-06-29 10:07:12 +020053enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070054 /*
Tejun Heo24647572013-01-24 11:01:33 -080055 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070056 *
Tejun Heo24647572013-01-24 11:01:33 -080057 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070058 * While associated (!DISASSOCIATED), all workers are bound to the
59 * CPU and none has %WORKER_UNBOUND set and concurrency management
60 * is in effect.
61 *
62 * While DISASSOCIATED, the cpu may be offline and all workers have
63 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080064 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070065 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070066 * Note that DISASSOCIATED should be flipped only while holding
67 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080068 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070069 */
Tejun Heo11ebea52012-07-12 14:46:37 -070070 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080071 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080072 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020073
Tejun Heoc8e55f32010-06-29 10:07:12 +020074 /* worker flags */
75 WORKER_STARTED = 1 << 0, /* started */
76 WORKER_DIE = 1 << 1, /* die die die */
77 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020078 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020079 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020080 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070081 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020082
Tejun Heoa9ab7752013-03-19 13:45:21 -070083 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
84 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020085
Tejun Heoe34cdddb2013-01-24 11:01:33 -080086 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070087
Tejun Heo29c91e92013-03-12 11:30:03 -070088 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020089 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020090
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
92 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
93
Tejun Heo3233cdb2011-02-16 18:10:19 +010094 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
95 /* call for help after 10ms
96 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020097 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
98 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020099
100 /*
101 * Rescue workers are used only on emergencies and shared by
102 * all cpus. Give -20.
103 */
104 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700105 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoecf68812013-04-01 11:23:34 -0700106
107 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200108};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 * Structure fields follow one of the following exclusion rules.
112 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200113 * I: Modifiable by initialization/destruction paths and read-only for
114 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200115 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 * P: Preemption protected. Disabling preemption is enough and should
117 * only be modified and accessed from the local cpu.
118 *
Tejun Heod565ed62013-01-24 11:01:33 -0800119 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 *
Tejun Heod565ed62013-01-24 11:01:33 -0800121 * X: During normal operation, modification requires pool->lock and should
122 * be done only from local cpu. Either disabling preemption on local
123 * cpu or grabbing pool->lock is enough for read access. If
124 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200125 *
Tejun Heo822d8402013-03-19 13:45:21 -0700126 * MG: pool->manager_mutex and pool->lock protected. Writes require both
127 * locks. Reads can happen under either lock.
128 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700129 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700130 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700131 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700132 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700133 * WQ: wq->mutex protected.
134 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700135 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700136 *
137 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200138 */
139
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800140/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141
Tejun Heobd7bdd42012-07-12 14:46:37 -0700142struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800143 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700144 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700145 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800146 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700147 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700148
149 struct list_head worklist; /* L: list of pending works */
150 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700151
152 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700153 int nr_idle; /* L: currently idle ones */
154
155 struct list_head idle_list; /* X: list of idle workers */
156 struct timer_list idle_timer; /* L: worker idle timeout */
157 struct timer_list mayday_timer; /* L: SOS timer for workers */
158
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700159 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800160 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
161 /* L: hash of busy workers */
162
Tejun Heobc3a1af2013-03-13 19:47:39 -0700163 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700164 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700165 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700166 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800167
Tejun Heo7a4e3442013-03-12 11:30:00 -0700168 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700169 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
170 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700171
Tejun Heoe19e3972013-01-24 11:39:44 -0800172 /*
173 * The current concurrency level. As it's likely to be accessed
174 * from other CPUs during try_to_wake_up(), put it in a separate
175 * cacheline.
176 */
177 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700178
179 /*
180 * Destruction of pool is sched-RCU protected to allow dereferences
181 * from get_work_pool().
182 */
183 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184} ____cacheline_aligned_in_smp;
185
186/*
Tejun Heo112202d2013-02-13 19:29:12 -0800187 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
188 * of work_struct->data are used for flags and the remaining high bits
189 * point to the pwq; thus, pwqs need to be aligned at two's power of the
190 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Tejun Heo112202d2013-02-13 19:29:12 -0800192struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700193 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200194 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200195 int work_color; /* L: current color */
196 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700197 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200198 int nr_in_flight[WORK_NR_COLORS];
199 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200201 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200202 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700203 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700204 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700205
206 /*
207 * Release of unbound pwq is punted to system_wq. See put_pwq()
208 * and pwq_unbound_release_workfn() for details. pool_workqueue
209 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700210 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700211 */
212 struct work_struct unbound_release_work;
213 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700214} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200217 * Structure used to wait for workqueue flush.
218 */
219struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700220 struct list_head list; /* WQ: list of flushers */
221 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200222 struct completion done; /* flush completion */
223};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Tejun Heo226223a2013-03-12 11:30:05 -0700225struct wq_device;
226
Tejun Heo73f53c42010-06-29 10:07:11 +0200227/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700228 * The externally visible workqueue. It relays the issued work items to
229 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700232 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700233 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200234
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700235 struct mutex mutex; /* protects this wq */
236 int work_color; /* WQ: current work color */
237 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800238 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700239 struct wq_flusher *first_flusher; /* WQ: first flusher */
240 struct list_head flusher_queue; /* WQ: flush waiters */
241 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200242
Tejun Heo2e109a22013-03-13 19:47:40 -0700243 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200244 struct worker *rescuer; /* I: rescue worker */
245
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700246 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700247 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700248
Tejun Heo6029a912013-04-01 11:23:34 -0700249 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
Tejun Heo4c16bd32013-04-01 11:23:36 -0700250 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700251
Tejun Heo226223a2013-03-12 11:30:05 -0700252#ifdef CONFIG_SYSFS
253 struct wq_device *wq_dev; /* I: for sysfs interface */
254#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700255#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200256 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700257#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700258 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700259
260 /* hot fields used during command issue, aligned to cacheline */
261 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
262 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700263 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
Tejun Heoe904e6c2013-03-12 11:29:57 -0700266static struct kmem_cache *pwq_cache;
267
Tejun Heobce90382013-04-01 11:23:32 -0700268static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
269static cpumask_var_t *wq_numa_possible_cpumask;
270 /* possible CPUs of each node */
271
Tejun Heod55262c2013-04-01 11:23:38 -0700272static bool wq_disable_numa;
273module_param_named(disable_numa, wq_disable_numa, bool, 0444);
274
Viresh Kumarcee22a12013-04-08 16:45:40 +0530275/* see the comment above the definition of WQ_POWER_EFFICIENT */
276#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
277static bool wq_power_efficient = true;
278#else
279static bool wq_power_efficient;
280#endif
281
282module_param_named(power_efficient, wq_power_efficient, bool, 0444);
283
Tejun Heobce90382013-04-01 11:23:32 -0700284static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
285
Tejun Heo4c16bd32013-04-01 11:23:36 -0700286/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
287static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
288
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700289static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700290static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700291
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700292static LIST_HEAD(workqueues); /* PL: list of all workqueues */
293static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700294
295/* the per-cpu worker pools */
296static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
297 cpu_worker_pools);
298
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700299static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700300
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700301/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700302static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
303
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700304/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700305static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
306
Tejun Heod320c032010-06-29 10:07:14 +0200307struct workqueue_struct *system_wq __read_mostly;
Marc Dionnead7b1f82013-05-06 17:44:55 -0400308EXPORT_SYMBOL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300309struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900310EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300311struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200312EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300313struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200314EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300315struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100316EXPORT_SYMBOL_GPL(system_freezable_wq);
Viresh Kumar06681062013-04-24 17:12:54 +0530317struct workqueue_struct *system_power_efficient_wq __read_mostly;
318EXPORT_SYMBOL_GPL(system_power_efficient_wq);
319struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
320EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200321
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700322static int worker_thread(void *__worker);
323static void copy_workqueue_attrs(struct workqueue_attrs *to,
324 const struct workqueue_attrs *from);
325
Tejun Heo97bd2342010-10-05 10:41:14 +0200326#define CREATE_TRACE_POINTS
327#include <trace/events/workqueue.h>
328
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700329#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700330 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700331 lockdep_is_held(&wq_pool_mutex), \
332 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700333
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700334#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700335 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700336 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700337 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700338
Tejun Heo822d8402013-03-19 13:45:21 -0700339#ifdef CONFIG_LOCKDEP
340#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800341 WARN_ONCE(debug_locks && \
342 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700343 !lockdep_is_held(&(pool)->lock), \
344 "pool->manager_mutex or ->lock should be held")
345#else
346#define assert_manager_or_pool_lock(pool) do { } while (0)
347#endif
348
Tejun Heof02ae732013-03-12 11:30:03 -0700349#define for_each_cpu_worker_pool(pool, cpu) \
350 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
351 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700352 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700353
Tejun Heo49e3cf42013-03-12 11:29:58 -0700354/**
Tejun Heo17116962013-03-12 11:29:58 -0700355 * for_each_pool - iterate through all worker_pools in the system
356 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700357 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700358 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700359 * This must be called either with wq_pool_mutex held or sched RCU read
360 * locked. If the pool needs to be used beyond the locking in effect, the
361 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700362 *
363 * The if/else clause exists only for the lockdep assertion and can be
364 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700365 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700366#define for_each_pool(pool, pi) \
367 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700368 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700369 else
Tejun Heo17116962013-03-12 11:29:58 -0700370
371/**
Tejun Heo822d8402013-03-19 13:45:21 -0700372 * for_each_pool_worker - iterate through all workers of a worker_pool
373 * @worker: iteration cursor
374 * @wi: integer used for iteration
375 * @pool: worker_pool to iterate workers of
376 *
377 * This must be called with either @pool->manager_mutex or ->lock held.
378 *
379 * The if/else clause exists only for the lockdep assertion and can be
380 * ignored.
381 */
382#define for_each_pool_worker(worker, wi, pool) \
383 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
384 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
385 else
386
387/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700388 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
389 * @pwq: iteration cursor
390 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700391 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700392 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700393 * If the pwq needs to be used beyond the locking in effect, the caller is
394 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700395 *
396 * The if/else clause exists only for the lockdep assertion and can be
397 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700398 */
399#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700400 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700401 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700402 else
Tejun Heof3421792010-07-02 10:03:51 +0200403
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900404#ifdef CONFIG_DEBUG_OBJECTS_WORK
405
406static struct debug_obj_descr work_debug_descr;
407
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100408static void *work_debug_hint(void *addr)
409{
410 return ((struct work_struct *) addr)->func;
411}
412
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900413/*
414 * fixup_init is called when:
415 * - an active object is initialized
416 */
417static int work_fixup_init(void *addr, enum debug_obj_state state)
418{
419 struct work_struct *work = addr;
420
421 switch (state) {
422 case ODEBUG_STATE_ACTIVE:
423 cancel_work_sync(work);
424 debug_object_init(work, &work_debug_descr);
425 return 1;
426 default:
427 return 0;
428 }
429}
430
431/*
432 * fixup_activate is called when:
433 * - an active object is activated
434 * - an unknown object is activated (might be a statically initialized object)
435 */
436static int work_fixup_activate(void *addr, enum debug_obj_state state)
437{
438 struct work_struct *work = addr;
439
440 switch (state) {
441
442 case ODEBUG_STATE_NOTAVAILABLE:
443 /*
444 * This is not really a fixup. The work struct was
445 * statically initialized. We just make sure that it
446 * is tracked in the object tracker.
447 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200448 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900449 debug_object_init(work, &work_debug_descr);
450 debug_object_activate(work, &work_debug_descr);
451 return 0;
452 }
453 WARN_ON_ONCE(1);
454 return 0;
455
456 case ODEBUG_STATE_ACTIVE:
457 WARN_ON(1);
458
459 default:
460 return 0;
461 }
462}
463
464/*
465 * fixup_free is called when:
466 * - an active object is freed
467 */
468static int work_fixup_free(void *addr, enum debug_obj_state state)
469{
470 struct work_struct *work = addr;
471
472 switch (state) {
473 case ODEBUG_STATE_ACTIVE:
474 cancel_work_sync(work);
475 debug_object_free(work, &work_debug_descr);
476 return 1;
477 default:
478 return 0;
479 }
480}
481
482static struct debug_obj_descr work_debug_descr = {
483 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100484 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900485 .fixup_init = work_fixup_init,
486 .fixup_activate = work_fixup_activate,
487 .fixup_free = work_fixup_free,
488};
489
490static inline void debug_work_activate(struct work_struct *work)
491{
492 debug_object_activate(work, &work_debug_descr);
493}
494
495static inline void debug_work_deactivate(struct work_struct *work)
496{
497 debug_object_deactivate(work, &work_debug_descr);
498}
499
500void __init_work(struct work_struct *work, int onstack)
501{
502 if (onstack)
503 debug_object_init_on_stack(work, &work_debug_descr);
504 else
505 debug_object_init(work, &work_debug_descr);
506}
507EXPORT_SYMBOL_GPL(__init_work);
508
509void destroy_work_on_stack(struct work_struct *work)
510{
511 debug_object_free(work, &work_debug_descr);
512}
513EXPORT_SYMBOL_GPL(destroy_work_on_stack);
514
515#else
516static inline void debug_work_activate(struct work_struct *work) { }
517static inline void debug_work_deactivate(struct work_struct *work) { }
518#endif
519
Tejun Heo9daf9e62013-01-24 11:01:33 -0800520/* allocate ID and assign it to @pool */
521static int worker_pool_assign_id(struct worker_pool *pool)
522{
523 int ret;
524
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700525 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700526
Tejun Heoe68035f2013-03-13 14:59:38 -0700527 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700528 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700529 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700530 return 0;
531 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800532 return ret;
533}
534
Tejun Heo76af4d92013-03-12 11:30:00 -0700535/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700536 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
537 * @wq: the target workqueue
538 * @node: the node ID
539 *
540 * This must be called either with pwq_lock held or sched RCU read locked.
541 * If the pwq needs to be used beyond the locking in effect, the caller is
542 * responsible for guaranteeing that the pwq stays online.
543 */
544static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
545 int node)
546{
547 assert_rcu_or_wq_mutex(wq);
548 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
549}
550
Tejun Heo73f53c42010-06-29 10:07:11 +0200551static unsigned int work_color_to_flags(int color)
552{
553 return color << WORK_STRUCT_COLOR_SHIFT;
554}
555
556static int get_work_color(struct work_struct *work)
557{
558 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
559 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
560}
561
562static int work_next_color(int color)
563{
564 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700565}
566
David Howells4594bf12006-12-07 11:33:26 +0000567/*
Tejun Heo112202d2013-02-13 19:29:12 -0800568 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
569 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800570 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571 *
Tejun Heo112202d2013-02-13 19:29:12 -0800572 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
573 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700574 * work->data. These functions should only be called while the work is
575 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200576 *
Tejun Heo112202d2013-02-13 19:29:12 -0800577 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800578 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800579 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800580 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700581 *
582 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
583 * canceled. While being canceled, a work item may have its PENDING set
584 * but stay off timer and worklist for arbitrarily long and nobody should
585 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000586 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587static inline void set_work_data(struct work_struct *work, unsigned long data,
588 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000589{
Tejun Heo6183c002013-03-12 11:29:57 -0700590 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000592}
David Howells365970a2006-11-22 14:54:49 +0000593
Tejun Heo112202d2013-02-13 19:29:12 -0800594static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200596{
Tejun Heo112202d2013-02-13 19:29:12 -0800597 set_work_data(work, (unsigned long)pwq,
598 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200599}
600
Lai Jiangshan4468a002013-02-06 18:04:53 -0800601static void set_work_pool_and_keep_pending(struct work_struct *work,
602 int pool_id)
603{
604 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
605 WORK_STRUCT_PENDING);
606}
607
Tejun Heo7c3eed52013-01-24 11:01:33 -0800608static void set_work_pool_and_clear_pending(struct work_struct *work,
609 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000610{
Tejun Heo23657bb2012-08-13 17:08:19 -0700611 /*
612 * The following wmb is paired with the implied mb in
613 * test_and_set_bit(PENDING) and ensures all updates to @work made
614 * here are visible to and precede any updates by the next PENDING
615 * owner.
616 */
617 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800618 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200619}
620
621static void clear_work_data(struct work_struct *work)
622{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800623 smp_wmb(); /* see set_work_pool_and_clear_pending() */
624 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200625}
626
Tejun Heo112202d2013-02-13 19:29:12 -0800627static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200628{
Tejun Heoe1201532010-07-22 14:14:25 +0200629 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200630
Tejun Heo112202d2013-02-13 19:29:12 -0800631 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200632 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
633 else
634 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200635}
636
Tejun Heo7c3eed52013-01-24 11:01:33 -0800637/**
638 * get_work_pool - return the worker_pool a given work was associated with
639 * @work: the work item of interest
640 *
641 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700642 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700643 * Pools are created and destroyed under wq_pool_mutex, and allows read
644 * access under sched-RCU read lock. As such, this function should be
645 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700646 *
647 * All fields of the returned pool are accessible as long as the above
648 * mentioned locking is in effect. If the returned pool needs to be used
649 * beyond the critical section, the caller is responsible for ensuring the
650 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800651 */
652static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200653{
Tejun Heoe1201532010-07-22 14:14:25 +0200654 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800655 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200656
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700657 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700658
Tejun Heo112202d2013-02-13 19:29:12 -0800659 if (data & WORK_STRUCT_PWQ)
660 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800661 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200662
Tejun Heo7c3eed52013-01-24 11:01:33 -0800663 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
664 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200665 return NULL;
666
Tejun Heofa1b54e2013-03-12 11:30:00 -0700667 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800668}
669
670/**
671 * get_work_pool_id - return the worker pool ID a given work is associated with
672 * @work: the work item of interest
673 *
674 * Return the worker_pool ID @work was last associated with.
675 * %WORK_OFFQ_POOL_NONE if none.
676 */
677static int get_work_pool_id(struct work_struct *work)
678{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800679 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800680
Tejun Heo112202d2013-02-13 19:29:12 -0800681 if (data & WORK_STRUCT_PWQ)
682 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800683 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
684
685 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800686}
687
Tejun Heobbb68df2012-08-03 10:30:46 -0700688static void mark_work_canceling(struct work_struct *work)
689{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800690 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700691
Tejun Heo7c3eed52013-01-24 11:01:33 -0800692 pool_id <<= WORK_OFFQ_POOL_SHIFT;
693 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700694}
695
696static bool work_is_canceling(struct work_struct *work)
697{
698 unsigned long data = atomic_long_read(&work->data);
699
Tejun Heo112202d2013-02-13 19:29:12 -0800700 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700701}
702
David Howells365970a2006-11-22 14:54:49 +0000703/*
Tejun Heo32704762012-07-13 22:16:45 -0700704 * Policy functions. These define the policies on how the global worker
705 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800706 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000707 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200708
Tejun Heo63d95a92012-07-12 14:46:37 -0700709static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000710{
Tejun Heoe19e3972013-01-24 11:39:44 -0800711 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000712}
713
Tejun Heoe22bee72010-06-29 10:07:14 +0200714/*
715 * Need to wake up a worker? Called from anything but currently
716 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700717 *
718 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800719 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700720 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200721 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700722static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000723{
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000725}
726
Tejun Heoe22bee72010-06-29 10:07:14 +0200727/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700728static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200729{
Tejun Heo63d95a92012-07-12 14:46:37 -0700730 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200731}
732
733/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700734static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200735{
Tejun Heoe19e3972013-01-24 11:39:44 -0800736 return !list_empty(&pool->worklist) &&
737 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200738}
739
740/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700741static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200742{
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200744}
745
746/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700747static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200748{
Tejun Heo63d95a92012-07-12 14:46:37 -0700749 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700750 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200751}
752
753/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700754static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200755{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700756 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700757 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
758 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200759
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700760 /*
761 * nr_idle and idle_list may disagree if idle rebinding is in
762 * progress. Never return %true if idle_list is empty.
763 */
764 if (list_empty(&pool->idle_list))
765 return false;
766
Tejun Heoe22bee72010-06-29 10:07:14 +0200767 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
768}
769
770/*
771 * Wake up functions.
772 */
773
Tejun Heo7e116292010-06-29 10:07:13 +0200774/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700775static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200776{
Tejun Heo63d95a92012-07-12 14:46:37 -0700777 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200778 return NULL;
779
Tejun Heo63d95a92012-07-12 14:46:37 -0700780 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200781}
782
783/**
784 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700785 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200786 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700787 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200788 *
789 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800790 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200791 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700792static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200793{
Tejun Heo63d95a92012-07-12 14:46:37 -0700794 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200795
796 if (likely(worker))
797 wake_up_process(worker->task);
798}
799
Tejun Heo4690c4a2010-06-29 10:07:10 +0200800/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200801 * wq_worker_waking_up - a worker is waking up
802 * @task: task waking up
803 * @cpu: CPU @task is waking up to
804 *
805 * This function is called during try_to_wake_up() when a worker is
806 * being awoken.
807 *
808 * CONTEXT:
809 * spin_lock_irq(rq->lock)
810 */
Tejun Heod84ff052013-03-12 11:29:59 -0700811void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200812{
813 struct worker *worker = kthread_data(task);
814
Joonsoo Kim36576002012-10-26 23:03:49 +0900815 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800816 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800817 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900818 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200819}
820
821/**
822 * wq_worker_sleeping - a worker is going to sleep
823 * @task: task going to sleep
824 * @cpu: CPU in question, must be the current CPU number
825 *
826 * This function is called during schedule() when a busy worker is
827 * going to sleep. Worker on the same cpu can be woken up by
828 * returning pointer to its task.
829 *
830 * CONTEXT:
831 * spin_lock_irq(rq->lock)
832 *
833 * RETURNS:
834 * Worker task on @cpu to wake up, %NULL if none.
835 */
Tejun Heod84ff052013-03-12 11:29:59 -0700836struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200837{
838 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800839 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
Tejun Heo111c2252013-01-17 17:16:24 -0800841 /*
842 * Rescuers, which may not have all the fields set up like normal
843 * workers, also reach here, let's not access anything before
844 * checking NOT_RUNNING.
845 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500846 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200847 return NULL;
848
Tejun Heo111c2252013-01-17 17:16:24 -0800849 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800850
Tejun Heoe22bee72010-06-29 10:07:14 +0200851 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700852 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
853 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200854
855 /*
856 * The counterpart of the following dec_and_test, implied mb,
857 * worklist not empty test sequence is in insert_work().
858 * Please read comment there.
859 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700860 * NOT_RUNNING is clear. This means that we're bound to and
861 * running on the local cpu w/ rq lock held and preemption
862 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800863 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700864 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800866 if (atomic_dec_and_test(&pool->nr_running) &&
867 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700868 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 return to_wakeup ? to_wakeup->task : NULL;
870}
871
872/**
873 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200874 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200875 * @flags: flags to set
876 * @wakeup: wakeup an idle worker if necessary
877 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200878 * Set @flags in @worker->flags and adjust nr_running accordingly. If
879 * nr_running becomes zero and @wakeup is %true, an idle worker is
880 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200881 *
Tejun Heocb444762010-07-02 10:03:50 +0200882 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800883 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200884 */
885static inline void worker_set_flags(struct worker *worker, unsigned int flags,
886 bool wakeup)
887{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700888 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200889
Tejun Heocb444762010-07-02 10:03:50 +0200890 WARN_ON_ONCE(worker->task != current);
891
Tejun Heoe22bee72010-06-29 10:07:14 +0200892 /*
893 * If transitioning into NOT_RUNNING, adjust nr_running and
894 * wake up an idle worker as necessary if requested by
895 * @wakeup.
896 */
897 if ((flags & WORKER_NOT_RUNNING) &&
898 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200899 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800900 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700901 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700902 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200903 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800904 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200905 }
906
Tejun Heod302f012010-06-29 10:07:13 +0200907 worker->flags |= flags;
908}
909
910/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200911 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200912 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200913 * @flags: flags to clear
914 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200915 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200916 *
Tejun Heocb444762010-07-02 10:03:50 +0200917 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800918 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200919 */
920static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
921{
Tejun Heo63d95a92012-07-12 14:46:37 -0700922 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200923 unsigned int oflags = worker->flags;
924
Tejun Heocb444762010-07-02 10:03:50 +0200925 WARN_ON_ONCE(worker->task != current);
926
Tejun Heod302f012010-06-29 10:07:13 +0200927 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200928
Tejun Heo42c025f2011-01-11 15:58:49 +0100929 /*
930 * If transitioning out of NOT_RUNNING, increment nr_running. Note
931 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
932 * of multiple flags, not a single flag.
933 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200934 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
935 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800936 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200937}
938
939/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200940 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800941 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200942 * @work: work to find worker for
943 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800944 * Find a worker which is executing @work on @pool by searching
945 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800946 * to match, its current execution should match the address of @work and
947 * its work function. This is to avoid unwanted dependency between
948 * unrelated work executions through a work item being recycled while still
949 * being executed.
950 *
951 * This is a bit tricky. A work item may be freed once its execution
952 * starts and nothing prevents the freed area from being recycled for
953 * another work item. If the same work item address ends up being reused
954 * before the original execution finishes, workqueue will identify the
955 * recycled work item as currently executing and make it wait until the
956 * current execution finishes, introducing an unwanted dependency.
957 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700958 * This function checks the work item address and work function to avoid
959 * false positives. Note that this isn't complete as one may construct a
960 * work function which can introduce dependency onto itself through a
961 * recycled work item. Well, if somebody wants to shoot oneself in the
962 * foot that badly, there's only so much we can do, and if such deadlock
963 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200964 *
965 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800966 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200967 *
968 * RETURNS:
969 * Pointer to worker which is executing @work if found, NULL
970 * otherwise.
971 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800972static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200973 struct work_struct *work)
974{
Sasha Levin42f85702012-12-17 10:01:23 -0500975 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500976
Sasha Levinb67bfe02013-02-27 17:06:00 -0800977 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800978 (unsigned long)work)
979 if (worker->current_work == work &&
980 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500981 return worker;
982
983 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200984}
985
986/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700987 * move_linked_works - move linked works to a list
988 * @work: start of series of works to be scheduled
989 * @head: target list to append @work to
990 * @nextp: out paramter for nested worklist walking
991 *
992 * Schedule linked works starting from @work to @head. Work series to
993 * be scheduled starts at @work and includes any consecutive work with
994 * WORK_STRUCT_LINKED set in its predecessor.
995 *
996 * If @nextp is not NULL, it's updated to point to the next work of
997 * the last scheduled work. This allows move_linked_works() to be
998 * nested inside outer list_for_each_entry_safe().
999 *
1000 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001001 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001002 */
1003static void move_linked_works(struct work_struct *work, struct list_head *head,
1004 struct work_struct **nextp)
1005{
1006 struct work_struct *n;
1007
1008 /*
1009 * Linked worklist will always end before the end of the list,
1010 * use NULL for list head.
1011 */
1012 list_for_each_entry_safe_from(work, n, NULL, entry) {
1013 list_move_tail(&work->entry, head);
1014 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1015 break;
1016 }
1017
1018 /*
1019 * If we're already inside safe list traversal and have moved
1020 * multiple works to the scheduled queue, the next position
1021 * needs to be updated.
1022 */
1023 if (nextp)
1024 *nextp = n;
1025}
1026
Tejun Heo8864b4e2013-03-12 11:30:04 -07001027/**
1028 * get_pwq - get an extra reference on the specified pool_workqueue
1029 * @pwq: pool_workqueue to get
1030 *
1031 * Obtain an extra reference on @pwq. The caller should guarantee that
1032 * @pwq has positive refcnt and be holding the matching pool->lock.
1033 */
1034static void get_pwq(struct pool_workqueue *pwq)
1035{
1036 lockdep_assert_held(&pwq->pool->lock);
1037 WARN_ON_ONCE(pwq->refcnt <= 0);
1038 pwq->refcnt++;
1039}
1040
1041/**
1042 * put_pwq - put a pool_workqueue reference
1043 * @pwq: pool_workqueue to put
1044 *
1045 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1046 * destruction. The caller should be holding the matching pool->lock.
1047 */
1048static void put_pwq(struct pool_workqueue *pwq)
1049{
1050 lockdep_assert_held(&pwq->pool->lock);
1051 if (likely(--pwq->refcnt))
1052 return;
1053 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1054 return;
1055 /*
1056 * @pwq can't be released under pool->lock, bounce to
1057 * pwq_unbound_release_workfn(). This never recurses on the same
1058 * pool->lock as this path is taken only for unbound workqueues and
1059 * the release work item is scheduled on a per-cpu workqueue. To
1060 * avoid lockdep warning, unbound pool->locks are given lockdep
1061 * subclass of 1 in get_unbound_pool().
1062 */
1063 schedule_work(&pwq->unbound_release_work);
1064}
1065
Tejun Heodce90d42013-04-01 11:23:35 -07001066/**
1067 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1068 * @pwq: pool_workqueue to put (can be %NULL)
1069 *
1070 * put_pwq() with locking. This function also allows %NULL @pwq.
1071 */
1072static void put_pwq_unlocked(struct pool_workqueue *pwq)
1073{
1074 if (pwq) {
1075 /*
1076 * As both pwqs and pools are sched-RCU protected, the
1077 * following lock operations are safe.
1078 */
1079 spin_lock_irq(&pwq->pool->lock);
1080 put_pwq(pwq);
1081 spin_unlock_irq(&pwq->pool->lock);
1082 }
1083}
1084
Tejun Heo112202d2013-02-13 19:29:12 -08001085static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001086{
Tejun Heo112202d2013-02-13 19:29:12 -08001087 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001088
1089 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001090 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001091 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001092 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001093}
1094
Tejun Heo112202d2013-02-13 19:29:12 -08001095static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001096{
Tejun Heo112202d2013-02-13 19:29:12 -08001097 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001098 struct work_struct, entry);
1099
Tejun Heo112202d2013-02-13 19:29:12 -08001100 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001101}
1102
Tejun Heobf4ede02012-08-03 10:30:46 -07001103/**
Tejun Heo112202d2013-02-13 19:29:12 -08001104 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1105 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001106 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001107 *
1108 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001109 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001110 *
1111 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001112 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001113 */
Tejun Heo112202d2013-02-13 19:29:12 -08001114static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001115{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001116 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001117 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001118 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001119
Tejun Heo112202d2013-02-13 19:29:12 -08001120 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001121
Tejun Heo112202d2013-02-13 19:29:12 -08001122 pwq->nr_active--;
1123 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001124 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001125 if (pwq->nr_active < pwq->max_active)
1126 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001127 }
1128
1129 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001130 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001131 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001132
1133 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001134 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001135 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001136
Tejun Heo112202d2013-02-13 19:29:12 -08001137 /* this pwq is done, clear flush_color */
1138 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001139
1140 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001141 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001142 * will handle the rest.
1143 */
Tejun Heo112202d2013-02-13 19:29:12 -08001144 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1145 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001146out_put:
1147 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001148}
1149
Tejun Heo36e227d2012-08-03 10:30:46 -07001150/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001151 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001152 * @work: work item to steal
1153 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001154 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001155 *
1156 * Try to grab PENDING bit of @work. This function can handle @work in any
1157 * stable state - idle, on timer or on worklist. Return values are
1158 *
1159 * 1 if @work was pending and we successfully stole PENDING
1160 * 0 if @work was idle and we claimed PENDING
1161 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001162 * -ENOENT if someone else is canceling @work, this state may persist
1163 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001164 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001165 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001166 * interrupted while holding PENDING and @work off queue, irq must be
1167 * disabled on entry. This, combined with delayed_work->timer being
1168 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001169 *
1170 * On successful return, >= 0, irq is disabled and the caller is
1171 * responsible for releasing it using local_irq_restore(*@flags).
1172 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001173 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001174 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001175static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1176 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001177{
Tejun Heod565ed62013-01-24 11:01:33 -08001178 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001179 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001180
Tejun Heobbb68df2012-08-03 10:30:46 -07001181 local_irq_save(*flags);
1182
Tejun Heo36e227d2012-08-03 10:30:46 -07001183 /* try to steal the timer if it exists */
1184 if (is_dwork) {
1185 struct delayed_work *dwork = to_delayed_work(work);
1186
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001187 /*
1188 * dwork->timer is irqsafe. If del_timer() fails, it's
1189 * guaranteed that the timer is not queued anywhere and not
1190 * running on the local CPU.
1191 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001192 if (likely(del_timer(&dwork->timer)))
1193 return 1;
1194 }
1195
1196 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001197 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1198 return 0;
1199
1200 /*
1201 * The queueing is in progress, or it is already queued. Try to
1202 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1203 */
Tejun Heod565ed62013-01-24 11:01:33 -08001204 pool = get_work_pool(work);
1205 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001206 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001207
Tejun Heod565ed62013-01-24 11:01:33 -08001208 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001209 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001210 * work->data is guaranteed to point to pwq only while the work
1211 * item is queued on pwq->wq, and both updating work->data to point
1212 * to pwq on queueing and to pool on dequeueing are done under
1213 * pwq->pool->lock. This in turn guarantees that, if work->data
1214 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001215 * item is currently queued on that pool.
1216 */
Tejun Heo112202d2013-02-13 19:29:12 -08001217 pwq = get_work_pwq(work);
1218 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001219 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001220
Tejun Heo16062832013-02-06 18:04:53 -08001221 /*
1222 * A delayed work item cannot be grabbed directly because
1223 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001224 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001225 * management later on and cause stall. Make sure the work
1226 * item is activated before grabbing.
1227 */
1228 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001229 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001230
Tejun Heo16062832013-02-06 18:04:53 -08001231 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001232 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001233
Tejun Heo112202d2013-02-13 19:29:12 -08001234 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001235 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001236
Tejun Heo16062832013-02-06 18:04:53 -08001237 spin_unlock(&pool->lock);
1238 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001239 }
Tejun Heod565ed62013-01-24 11:01:33 -08001240 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001241fail:
1242 local_irq_restore(*flags);
1243 if (work_is_canceling(work))
1244 return -ENOENT;
1245 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001246 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001247}
1248
1249/**
Tejun Heo706026c2013-01-24 11:01:34 -08001250 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001251 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001252 * @work: work to insert
1253 * @head: insertion point
1254 * @extra_flags: extra WORK_STRUCT_* flags to set
1255 *
Tejun Heo112202d2013-02-13 19:29:12 -08001256 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001257 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001258 *
1259 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001260 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001261 */
Tejun Heo112202d2013-02-13 19:29:12 -08001262static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1263 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001264{
Tejun Heo112202d2013-02-13 19:29:12 -08001265 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001266
Tejun Heo4690c4a2010-06-29 10:07:10 +02001267 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001268 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001269 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001270 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001271
1272 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001273 * Ensure either wq_worker_sleeping() sees the above
1274 * list_add_tail() or we see zero nr_running to avoid workers lying
1275 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001276 */
1277 smp_mb();
1278
Tejun Heo63d95a92012-07-12 14:46:37 -07001279 if (__need_more_worker(pool))
1280 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001281}
1282
Tejun Heoc8efcc22010-12-20 19:32:04 +01001283/*
1284 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001285 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001286 */
1287static bool is_chained_work(struct workqueue_struct *wq)
1288{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001289 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001290
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001291 worker = current_wq_worker();
1292 /*
1293 * Return %true iff I'm a worker execuing a work item on @wq. If
1294 * I'm @worker, it's safe to dereference it without locking.
1295 */
Tejun Heo112202d2013-02-13 19:29:12 -08001296 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001297}
1298
Tejun Heod84ff052013-03-12 11:29:59 -07001299static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct work_struct *work)
1301{
Tejun Heo112202d2013-02-13 19:29:12 -08001302 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001303 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001304 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001305 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001306 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001307
1308 /*
1309 * While a work item is PENDING && off queue, a task trying to
1310 * steal the PENDING will busy-loop waiting for it to either get
1311 * queued or lose PENDING. Grabbing PENDING and queueing should
1312 * happen with IRQ disabled.
1313 */
1314 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001316 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001317
Tejun Heoc8efcc22010-12-20 19:32:04 +01001318 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001319 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001320 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001321 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001322retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001323 if (req_cpu == WORK_CPU_UNBOUND)
1324 cpu = raw_smp_processor_id();
1325
Tejun Heoc9178082013-03-12 11:30:04 -07001326 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001327 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001328 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001329 else
1330 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001331
Tejun Heoc9178082013-03-12 11:30:04 -07001332 /*
1333 * If @work was previously on a different pool, it might still be
1334 * running there, in which case the work needs to be queued on that
1335 * pool to guarantee non-reentrancy.
1336 */
1337 last_pool = get_work_pool(work);
1338 if (last_pool && last_pool != pwq->pool) {
1339 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001340
Tejun Heoc9178082013-03-12 11:30:04 -07001341 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001342
Tejun Heoc9178082013-03-12 11:30:04 -07001343 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001344
Tejun Heoc9178082013-03-12 11:30:04 -07001345 if (worker && worker->current_pwq->wq == wq) {
1346 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001347 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001348 /* meh... not running there, queue here */
1349 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001350 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001351 }
Tejun Heof3421792010-07-02 10:03:51 +02001352 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001353 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001354 }
1355
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001356 /*
1357 * pwq is determined and locked. For unbound pools, we could have
1358 * raced with pwq release and it could already be dead. If its
1359 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001360 * without another pwq replacing it in the numa_pwq_tbl or while
1361 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001362 * make forward-progress.
1363 */
1364 if (unlikely(!pwq->refcnt)) {
1365 if (wq->flags & WQ_UNBOUND) {
1366 spin_unlock(&pwq->pool->lock);
1367 cpu_relax();
1368 goto retry;
1369 }
1370 /* oops */
1371 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1372 wq->name, cpu);
1373 }
1374
Tejun Heo112202d2013-02-13 19:29:12 -08001375 /* pwq determined, queue */
1376 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001377
Dan Carpenterf5b25522012-04-13 22:06:58 +03001378 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001379 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001380 return;
1381 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001382
Tejun Heo112202d2013-02-13 19:29:12 -08001383 pwq->nr_in_flight[pwq->work_color]++;
1384 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001385
Tejun Heo112202d2013-02-13 19:29:12 -08001386 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001387 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001388 pwq->nr_active++;
1389 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001390 } else {
1391 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001392 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001393 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001394
Tejun Heo112202d2013-02-13 19:29:12 -08001395 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001396
Tejun Heo112202d2013-02-13 19:29:12 -08001397 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001400/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001401 * queue_work_on - queue work on specific cpu
1402 * @cpu: CPU number to execute work on
1403 * @wq: workqueue to use
1404 * @work: work to queue
1405 *
Tejun Heod4283e92012-08-03 10:30:44 -07001406 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001407 *
1408 * We queue the work to a specific CPU, the caller must ensure it
1409 * can't go away.
1410 */
Tejun Heod4283e92012-08-03 10:30:44 -07001411bool queue_work_on(int cpu, struct workqueue_struct *wq,
1412 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001413{
Tejun Heod4283e92012-08-03 10:30:44 -07001414 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001415 unsigned long flags;
1416
1417 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001418
Tejun Heo22df02b2010-06-29 10:07:10 +02001419 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001420 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001421 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001422 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001423
1424 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001425 return ret;
1426}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001427EXPORT_SYMBOL(queue_work_on);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001428
Tejun Heod8e794d2012-08-03 10:30:45 -07001429void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
David Howells52bad642006-11-22 14:54:01 +00001431 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001433 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001434 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001436EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001438static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1439 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001441 struct timer_list *timer = &dwork->timer;
1442 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001444 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1445 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001446 WARN_ON_ONCE(timer_pending(timer));
1447 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001448
Tejun Heo8852aac2012-12-01 16:23:42 -08001449 /*
1450 * If @delay is 0, queue @dwork->work immediately. This is for
1451 * both optimization and correctness. The earliest @timer can
1452 * expire is on the closest next tick and delayed_work users depend
1453 * on that there's no such delay when @delay is 0.
1454 */
1455 if (!delay) {
1456 __queue_work(cpu, wq, &dwork->work);
1457 return;
1458 }
1459
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001460 timer_stats_timer_set_start_info(&dwork->timer);
1461
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001462 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001463 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001464 timer->expires = jiffies + delay;
1465
1466 if (unlikely(cpu != WORK_CPU_UNBOUND))
1467 add_timer_on(timer, cpu);
1468 else
1469 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001472/**
1473 * queue_delayed_work_on - queue work on specific CPU after delay
1474 * @cpu: CPU number to execute work on
1475 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001476 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001477 * @delay: number of jiffies to wait before queueing
1478 *
Tejun Heo715f1302012-08-03 10:30:46 -07001479 * Returns %false if @work was already on a queue, %true otherwise. If
1480 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1481 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001482 */
Tejun Heod4283e92012-08-03 10:30:44 -07001483bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1484 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001485{
David Howells52bad642006-11-22 14:54:01 +00001486 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001487 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001488 unsigned long flags;
1489
1490 /* read the comment in __queue_work() */
1491 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001492
Tejun Heo22df02b2010-06-29 10:07:10 +02001493 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001494 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001495 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001496 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001497
1498 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001499 return ret;
1500}
Marc Dionnead7b1f82013-05-06 17:44:55 -04001501EXPORT_SYMBOL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Tejun Heoc8e55f32010-06-29 10:07:12 +02001503/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001504 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1505 * @cpu: CPU number to execute work on
1506 * @wq: workqueue to use
1507 * @dwork: work to queue
1508 * @delay: number of jiffies to wait before queueing
1509 *
1510 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1511 * modify @dwork's timer so that it expires after @delay. If @delay is
1512 * zero, @work is guaranteed to be scheduled immediately regardless of its
1513 * current state.
1514 *
1515 * Returns %false if @dwork was idle and queued, %true if @dwork was
1516 * pending and its timer was modified.
1517 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001518 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001519 * See try_to_grab_pending() for details.
1520 */
1521bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1522 struct delayed_work *dwork, unsigned long delay)
1523{
1524 unsigned long flags;
1525 int ret;
1526
1527 do {
1528 ret = try_to_grab_pending(&dwork->work, true, &flags);
1529 } while (unlikely(ret == -EAGAIN));
1530
1531 if (likely(ret >= 0)) {
1532 __queue_delayed_work(cpu, wq, dwork, delay);
1533 local_irq_restore(flags);
1534 }
1535
1536 /* -ENOENT from try_to_grab_pending() becomes %true */
1537 return ret;
1538}
1539EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1540
1541/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001542 * worker_enter_idle - enter idle state
1543 * @worker: worker which is entering idle state
1544 *
1545 * @worker is entering idle state. Update stats and idle timer if
1546 * necessary.
1547 *
1548 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001549 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001550 */
1551static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001553 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Tejun Heo6183c002013-03-12 11:29:57 -07001555 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1556 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1557 (worker->hentry.next || worker->hentry.pprev)))
1558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Tejun Heocb444762010-07-02 10:03:50 +02001560 /* can't use worker_set_flags(), also called from start_worker() */
1561 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001562 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001564
Tejun Heoc8e55f32010-06-29 10:07:12 +02001565 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001566 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001567
Tejun Heo628c78e2012-07-17 12:39:27 -07001568 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1569 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001570
Tejun Heo544ecf32012-05-14 15:04:50 -07001571 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001572 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001573 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001574 * nr_running, the warning may trigger spuriously. Check iff
1575 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001576 */
Tejun Heo24647572013-01-24 11:01:33 -08001577 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001578 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001579 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Tejun Heoc8e55f32010-06-29 10:07:12 +02001582/**
1583 * worker_leave_idle - leave idle state
1584 * @worker: worker which is leaving idle state
1585 *
1586 * @worker is leaving idle state. Update stats.
1587 *
1588 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001589 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001590 */
1591static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001593 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Tejun Heo6183c002013-03-12 11:29:57 -07001595 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1596 return;
Tejun Heod302f012010-06-29 10:07:13 +02001597 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001598 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001599 list_del_init(&worker->entry);
1600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Tejun Heoe22bee72010-06-29 10:07:14 +02001602/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001603 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1604 * @pool: target worker_pool
1605 *
1606 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 *
1608 * Works which are scheduled while the cpu is online must at least be
1609 * scheduled to a worker which is bound to the cpu so that if they are
1610 * flushed from cpu callbacks while cpu is going down, they are
1611 * guaranteed to execute on the cpu.
1612 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001613 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 * themselves to the target cpu and may race with cpu going down or
1615 * coming online. kthread_bind() can't be used because it may put the
1616 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001617 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001618 * [dis]associated in the meantime.
1619 *
Tejun Heo706026c2013-01-24 11:01:34 -08001620 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001621 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001622 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1623 * enters idle state or fetches works without dropping lock, it can
1624 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 *
1626 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001627 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001628 * held.
1629 *
1630 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001631 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001632 * bound), %false if offline.
1633 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001634static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001635__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001636{
Tejun Heoe22bee72010-06-29 10:07:14 +02001637 while (true) {
1638 /*
1639 * The following call may fail, succeed or succeed
1640 * without actually migrating the task to the cpu if
1641 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001642 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001643 */
Tejun Heo24647572013-01-24 11:01:33 -08001644 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001645 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001646
Tejun Heod565ed62013-01-24 11:01:33 -08001647 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001648 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001649 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001650 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001651 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001652 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001653 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001654
Tejun Heo5035b202011-04-29 18:08:37 +02001655 /*
1656 * We've raced with CPU hot[un]plug. Give it a breather
1657 * and retry migration. cond_resched() is required here;
1658 * otherwise, we might deadlock against cpu_stop trying to
1659 * bring down the CPU on non-preemptive kernel.
1660 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001662 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001663 }
1664}
1665
Tejun Heoc34056a2010-06-29 10:07:11 +02001666static struct worker *alloc_worker(void)
1667{
1668 struct worker *worker;
1669
1670 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001671 if (worker) {
1672 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001673 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001674 /* on creation a worker is in !idle && prep state */
1675 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001676 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001677 return worker;
1678}
1679
1680/**
1681 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001682 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001683 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001684 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001685 * can be started by calling start_worker() or destroyed using
1686 * destroy_worker().
1687 *
1688 * CONTEXT:
1689 * Might sleep. Does GFP_KERNEL allocations.
1690 *
1691 * RETURNS:
1692 * Pointer to the newly created worker.
1693 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001694static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001695{
Tejun Heoc34056a2010-06-29 10:07:11 +02001696 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001697 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001698 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001699
Tejun Heocd549682013-03-13 19:47:39 -07001700 lockdep_assert_held(&pool->manager_mutex);
1701
Tejun Heo822d8402013-03-19 13:45:21 -07001702 /*
1703 * ID is needed to determine kthread name. Allocate ID first
1704 * without installing the pointer.
1705 */
1706 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001707 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001708
1709 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1710
Tejun Heod565ed62013-01-24 11:01:33 -08001711 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001712 idr_preload_end();
1713 if (id < 0)
1714 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001715
1716 worker = alloc_worker();
1717 if (!worker)
1718 goto fail;
1719
Tejun Heobd7bdd42012-07-12 14:46:37 -07001720 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001721 worker->id = id;
1722
Tejun Heo29c91e92013-03-12 11:30:03 -07001723 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001724 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1725 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001726 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001727 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1728
Tejun Heof3f90ad2013-04-01 11:23:34 -07001729 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001730 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001731 if (IS_ERR(worker->task))
1732 goto fail;
1733
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001734 /*
1735 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1736 * online CPUs. It'll be re-applied when any of the CPUs come up.
1737 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001738 set_user_nice(worker->task, pool->attrs->nice);
1739 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001740
Tejun Heo14a40ff2013-03-19 13:45:20 -07001741 /* prevent userland from meddling with cpumask of workqueue workers */
1742 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001743
1744 /*
1745 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1746 * remains stable across this function. See the comments above the
1747 * flag definition for details.
1748 */
1749 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001750 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001751
Tejun Heo822d8402013-03-19 13:45:21 -07001752 /* successful, commit the pointer to idr */
1753 spin_lock_irq(&pool->lock);
1754 idr_replace(&pool->worker_idr, worker, worker->id);
1755 spin_unlock_irq(&pool->lock);
1756
Tejun Heoc34056a2010-06-29 10:07:11 +02001757 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001758
Tejun Heoc34056a2010-06-29 10:07:11 +02001759fail:
1760 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001761 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001762 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001763 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001764 }
1765 kfree(worker);
1766 return NULL;
1767}
1768
1769/**
1770 * start_worker - start a newly created worker
1771 * @worker: worker to start
1772 *
Tejun Heo706026c2013-01-24 11:01:34 -08001773 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001774 *
1775 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001776 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001777 */
1778static void start_worker(struct worker *worker)
1779{
Tejun Heocb444762010-07-02 10:03:50 +02001780 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001781 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001782 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001783 wake_up_process(worker->task);
1784}
1785
1786/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001787 * create_and_start_worker - create and start a worker for a pool
1788 * @pool: the target pool
1789 *
Tejun Heocd549682013-03-13 19:47:39 -07001790 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001791 */
1792static int create_and_start_worker(struct worker_pool *pool)
1793{
1794 struct worker *worker;
1795
Tejun Heocd549682013-03-13 19:47:39 -07001796 mutex_lock(&pool->manager_mutex);
1797
Tejun Heoebf44d12013-03-13 19:47:39 -07001798 worker = create_worker(pool);
1799 if (worker) {
1800 spin_lock_irq(&pool->lock);
1801 start_worker(worker);
1802 spin_unlock_irq(&pool->lock);
1803 }
1804
Tejun Heocd549682013-03-13 19:47:39 -07001805 mutex_unlock(&pool->manager_mutex);
1806
Tejun Heoebf44d12013-03-13 19:47:39 -07001807 return worker ? 0 : -ENOMEM;
1808}
1809
1810/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001811 * destroy_worker - destroy a workqueue worker
1812 * @worker: worker to be destroyed
1813 *
Tejun Heo706026c2013-01-24 11:01:34 -08001814 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001815 *
1816 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001817 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001818 */
1819static void destroy_worker(struct worker *worker)
1820{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001821 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001822
Tejun Heocd549682013-03-13 19:47:39 -07001823 lockdep_assert_held(&pool->manager_mutex);
1824 lockdep_assert_held(&pool->lock);
1825
Tejun Heoc34056a2010-06-29 10:07:11 +02001826 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001827 if (WARN_ON(worker->current_work) ||
1828 WARN_ON(!list_empty(&worker->scheduled)))
1829 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001830
Tejun Heoc8e55f32010-06-29 10:07:12 +02001831 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001832 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001833 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001834 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001835
1836 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001837 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001838
Tejun Heo822d8402013-03-19 13:45:21 -07001839 idr_remove(&pool->worker_idr, worker->id);
1840
Tejun Heod565ed62013-01-24 11:01:33 -08001841 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001842
Tejun Heoc34056a2010-06-29 10:07:11 +02001843 kthread_stop(worker->task);
1844 kfree(worker);
1845
Tejun Heod565ed62013-01-24 11:01:33 -08001846 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001847}
1848
Tejun Heo63d95a92012-07-12 14:46:37 -07001849static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001850{
Tejun Heo63d95a92012-07-12 14:46:37 -07001851 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001852
Tejun Heod565ed62013-01-24 11:01:33 -08001853 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001854
Tejun Heo63d95a92012-07-12 14:46:37 -07001855 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001856 struct worker *worker;
1857 unsigned long expires;
1858
1859 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001860 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001861 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1862
1863 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001864 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001865 else {
1866 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001867 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 }
1870 }
1871
Tejun Heod565ed62013-01-24 11:01:33 -08001872 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001873}
1874
Tejun Heo493a1722013-03-12 11:29:59 -07001875static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001876{
Tejun Heo112202d2013-02-13 19:29:12 -08001877 struct pool_workqueue *pwq = get_work_pwq(work);
1878 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001879
Tejun Heo2e109a22013-03-13 19:47:40 -07001880 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001881
Tejun Heo493008a2013-03-12 11:30:03 -07001882 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001883 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001884
1885 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001886 if (list_empty(&pwq->mayday_node)) {
1887 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001889 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001890}
1891
Tejun Heo706026c2013-01-24 11:01:34 -08001892static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001893{
Tejun Heo63d95a92012-07-12 14:46:37 -07001894 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 struct work_struct *work;
1896
Tejun Heo2e109a22013-03-13 19:47:40 -07001897 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001898 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001899
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001901 /*
1902 * We've been trying to create a new worker but
1903 * haven't been successful. We might be hitting an
1904 * allocation deadlock. Send distress signals to
1905 * rescuers.
1906 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001907 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 send_mayday(work);
1909 }
1910
Tejun Heo493a1722013-03-12 11:29:59 -07001911 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001912 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001913
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001915}
1916
1917/**
1918 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001919 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001920 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 * have at least one idle worker on return from this function. If
1923 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 * possible allocation deadlock.
1926 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001927 * On return, need_to_create_worker() is guaranteed to be %false and
1928 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 *
1930 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001931 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 * multiple times. Does GFP_KERNEL allocations. Called only from
1933 * manager.
1934 *
1935 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001936 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 * otherwise.
1938 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001939static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001940__releases(&pool->lock)
1941__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001942{
Tejun Heo63d95a92012-07-12 14:46:37 -07001943 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 return false;
1945restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001946 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001947
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001949 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001950
1951 while (true) {
1952 struct worker *worker;
1953
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001954 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001957 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001959 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1960 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 return true;
1962 }
1963
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 break;
1966
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 __set_current_state(TASK_INTERRUPTIBLE);
1968 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001969
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 break;
1972 }
1973
Tejun Heo63d95a92012-07-12 14:46:37 -07001974 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001975 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 goto restart;
1978 return true;
1979}
1980
1981/**
1982 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001983 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 * IDLE_WORKER_TIMEOUT.
1987 *
1988 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001989 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 * multiple times. Called only from manager.
1991 *
1992 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001993 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 * otherwise.
1995 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001996static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001997{
1998 bool ret = false;
1999
Tejun Heo63d95a92012-07-12 14:46:37 -07002000 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 struct worker *worker;
2002 unsigned long expires;
2003
Tejun Heo63d95a92012-07-12 14:46:37 -07002004 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2006
2007 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002008 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 break;
2010 }
2011
2012 destroy_worker(worker);
2013 ret = true;
2014 }
2015
2016 return ret;
2017}
2018
2019/**
2020 * manage_workers - manage worker pool
2021 * @worker: self
2022 *
Tejun Heo706026c2013-01-24 11:01:34 -08002023 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002025 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 *
2027 * The caller can safely start processing works on false return. On
2028 * true return, it's guaranteed that need_to_create_worker() is false
2029 * and may_start_working() is true.
2030 *
2031 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002032 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 * multiple times. Does GFP_KERNEL allocations.
2034 *
2035 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002036 * spin_lock_irq(pool->lock) which may be released and regrabbed
2037 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 */
2039static bool manage_workers(struct worker *worker)
2040{
Tejun Heo63d95a92012-07-12 14:46:37 -07002041 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002042 bool ret = false;
2043
Tejun Heobc3a1af2013-03-13 19:47:39 -07002044 /*
2045 * Managership is governed by two mutexes - manager_arb and
2046 * manager_mutex. manager_arb handles arbitration of manager role.
2047 * Anyone who successfully grabs manager_arb wins the arbitration
2048 * and becomes the manager. mutex_trylock() on pool->manager_arb
2049 * failure while holding pool->lock reliably indicates that someone
2050 * else is managing the pool and the worker which failed trylock
2051 * can proceed to executing work items. This means that anyone
2052 * grabbing manager_arb is responsible for actually performing
2053 * manager duties. If manager_arb is grabbed and released without
2054 * actual management, the pool may stall indefinitely.
2055 *
2056 * manager_mutex is used for exclusion of actual management
2057 * operations. The holder of manager_mutex can be sure that none
2058 * of management operations, including creation and destruction of
2059 * workers, won't take place until the mutex is released. Because
2060 * manager_mutex doesn't interfere with manager role arbitration,
2061 * it is guaranteed that the pool's management, while may be
2062 * delayed, won't be disturbed by someone else grabbing
2063 * manager_mutex.
2064 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002065 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002066 return ret;
2067
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002068 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002069 * With manager arbitration won, manager_mutex would be free in
2070 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002071 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002072 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002073 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002074 mutex_lock(&pool->manager_mutex);
Joonsoo Kim8f174b12013-05-01 00:07:00 +09002075 spin_lock_irq(&pool->lock);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002076 ret = true;
2077 }
2078
Tejun Heo11ebea52012-07-12 14:46:37 -07002079 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002080
2081 /*
2082 * Destroy and then create so that may_start_working() is true
2083 * on return.
2084 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002085 ret |= maybe_destroy_workers(pool);
2086 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002087
Tejun Heobc3a1af2013-03-13 19:47:39 -07002088 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002089 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002090 return ret;
2091}
2092
Tejun Heoa62428c2010-06-29 10:07:10 +02002093/**
2094 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002095 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002096 * @work: work to process
2097 *
2098 * Process @work. This function contains all the logics necessary to
2099 * process a single work including synchronization against and
2100 * interaction with other workers on the same cpu, queueing and
2101 * flushing. As long as context requirement is met, any worker can
2102 * call this function to process a work.
2103 *
2104 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002105 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002106 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002107static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002108__releases(&pool->lock)
2109__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002110{
Tejun Heo112202d2013-02-13 19:29:12 -08002111 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002112 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002113 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002114 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002115 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002116#ifdef CONFIG_LOCKDEP
2117 /*
2118 * It is permissible to free the struct work_struct from
2119 * inside the function that is called from it, this we need to
2120 * take into account for lockdep too. To avoid bogus "held
2121 * lock freed" warnings as well as problems when looking into
2122 * work->lockdep_map, make a copy and use that here.
2123 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002124 struct lockdep_map lockdep_map;
2125
2126 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002127#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002128 /*
2129 * Ensure we're on the correct CPU. DISASSOCIATED test is
2130 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002131 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002132 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002133 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002134 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002135 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002136
Tejun Heo7e116292010-06-29 10:07:13 +02002137 /*
2138 * A single work shouldn't be executed concurrently by
2139 * multiple workers on a single cpu. Check whether anyone is
2140 * already processing the work. If so, defer the work to the
2141 * currently executing one.
2142 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002143 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002144 if (unlikely(collision)) {
2145 move_linked_works(work, &collision->scheduled, NULL);
2146 return;
2147 }
2148
Tejun Heo8930cab2012-08-03 10:30:45 -07002149 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002150 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002151 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002152 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002153 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002154 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002155 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002156
Tejun Heoa62428c2010-06-29 10:07:10 +02002157 list_del_init(&work->entry);
2158
Tejun Heo649027d2010-06-29 10:07:14 +02002159 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002160 * CPU intensive works don't participate in concurrency
2161 * management. They're the scheduler's responsibility.
2162 */
2163 if (unlikely(cpu_intensive))
2164 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2165
Tejun Heo974271c2012-07-12 14:46:37 -07002166 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002167 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002168 * executed ASAP. Wake up another worker if necessary.
2169 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002170 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2171 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002172
Tejun Heo8930cab2012-08-03 10:30:45 -07002173 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002174 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002175 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002176 * PENDING and queued state changes happen together while IRQ is
2177 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002178 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002179 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002180
Tejun Heod565ed62013-01-24 11:01:33 -08002181 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002182
Tejun Heo112202d2013-02-13 19:29:12 -08002183 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002184 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002185 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002186 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002187 /*
2188 * While we must be careful to not use "work" after this, the trace
2189 * point will only record its address.
2190 */
2191 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002192 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002193 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002194
2195 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002196 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2197 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002198 current->comm, preempt_count(), task_pid_nr(current),
2199 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002200 debug_show_held_locks(current);
2201 dump_stack();
2202 }
2203
Tejun Heod565ed62013-01-24 11:01:33 -08002204 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002205
Tejun Heofb0e7be2010-06-29 10:07:15 +02002206 /* clear cpu intensive status */
2207 if (unlikely(cpu_intensive))
2208 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2209
Tejun Heoa62428c2010-06-29 10:07:10 +02002210 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002211 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002212 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002213 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002214 worker->current_pwq = NULL;
Tejun Heo3d1cb202013-04-30 15:27:22 -07002215 worker->desc_valid = false;
Tejun Heo112202d2013-02-13 19:29:12 -08002216 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002217}
2218
Tejun Heoaffee4b2010-06-29 10:07:12 +02002219/**
2220 * process_scheduled_works - process scheduled works
2221 * @worker: self
2222 *
2223 * Process all scheduled works. Please note that the scheduled list
2224 * may change while processing a work, so this function repeatedly
2225 * fetches a work from the top and executes it.
2226 *
2227 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002228 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002229 * multiple times.
2230 */
2231static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002233 while (!list_empty(&worker->scheduled)) {
2234 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002236 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Tejun Heo4690c4a2010-06-29 10:07:10 +02002240/**
2241 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002242 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002243 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002244 * The worker thread function. All workers belong to a worker_pool -
2245 * either a per-cpu one or dynamic unbound one. These workers process all
2246 * work items regardless of their specific target workqueue. The only
2247 * exception is work items which belong to workqueues with a rescuer which
2248 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002249 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002250static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Tejun Heoc34056a2010-06-29 10:07:11 +02002252 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002253 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Tejun Heoe22bee72010-06-29 10:07:14 +02002255 /* tell the scheduler that this is a workqueue worker */
2256 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002257woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002258 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Tejun Heoa9ab7752013-03-19 13:45:21 -07002260 /* am I supposed to die? */
2261 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002262 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002263 WARN_ON_ONCE(!list_empty(&worker->entry));
2264 worker->task->flags &= ~PF_WQ_WORKER;
2265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
Tejun Heoc8e55f32010-06-29 10:07:12 +02002268 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002269recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002270 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002271 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002272 goto sleep;
2273
2274 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002275 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002276 goto recheck;
2277
Tejun Heoc8e55f32010-06-29 10:07:12 +02002278 /*
2279 * ->scheduled list can only be filled while a worker is
2280 * preparing to process a work or actually processing it.
2281 * Make sure nobody diddled with it while I was sleeping.
2282 */
Tejun Heo6183c002013-03-12 11:29:57 -07002283 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002284
Tejun Heoe22bee72010-06-29 10:07:14 +02002285 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002286 * Finish PREP stage. We're guaranteed to have at least one idle
2287 * worker or that someone else has already assumed the manager
2288 * role. This is where @worker starts participating in concurrency
2289 * management if applicable and concurrency management is restored
2290 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002291 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002292 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002293
2294 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002295 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002296 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002297 struct work_struct, entry);
2298
2299 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2300 /* optimization path, not strictly necessary */
2301 process_one_work(worker, work);
2302 if (unlikely(!list_empty(&worker->scheduled)))
2303 process_scheduled_works(worker);
2304 } else {
2305 move_linked_works(work, &worker->scheduled, NULL);
2306 process_scheduled_works(worker);
2307 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002308 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002309
Tejun Heoe22bee72010-06-29 10:07:14 +02002310 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002311sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002312 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002313 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002314
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002316 * pool->lock is held and there's no work to process and no need to
2317 * manage, sleep. Workers are woken up only while holding
2318 * pool->lock or from local cpu, so setting the current state
2319 * before releasing pool->lock is enough to prevent losing any
2320 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002321 */
2322 worker_enter_idle(worker);
2323 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002324 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002325 schedule();
2326 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Tejun Heoe22bee72010-06-29 10:07:14 +02002329/**
2330 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002331 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002332 *
2333 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002334 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002335 *
Tejun Heo706026c2013-01-24 11:01:34 -08002336 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002337 * worker which uses GFP_KERNEL allocation which has slight chance of
2338 * developing into deadlock if some works currently on the same queue
2339 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2340 * the problem rescuer solves.
2341 *
Tejun Heo706026c2013-01-24 11:01:34 -08002342 * When such condition is possible, the pool summons rescuers of all
2343 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002344 * those works so that forward progress can be guaranteed.
2345 *
2346 * This should happen rarely.
2347 */
Tejun Heo111c2252013-01-17 17:16:24 -08002348static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002349{
Tejun Heo111c2252013-01-17 17:16:24 -08002350 struct worker *rescuer = __rescuer;
2351 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002352 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002353
2354 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002355
2356 /*
2357 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2358 * doesn't participate in concurrency management.
2359 */
2360 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002361repeat:
2362 set_current_state(TASK_INTERRUPTIBLE);
2363
Mike Galbraith412d32e2012-11-28 07:17:18 +01002364 if (kthread_should_stop()) {
2365 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002366 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002367 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002368 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002369
Tejun Heo493a1722013-03-12 11:29:59 -07002370 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002371 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002372
2373 while (!list_empty(&wq->maydays)) {
2374 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2375 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002376 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002377 struct work_struct *work, *n;
2378
2379 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002380 list_del_init(&pwq->mayday_node);
2381
Tejun Heo2e109a22013-03-13 19:47:40 -07002382 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002383
2384 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002385 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002386 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002387
2388 /*
2389 * Slurp in all works issued via this workqueue and
2390 * process'em.
2391 */
Tejun Heo6183c002013-03-12 11:29:57 -07002392 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002393 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002394 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002395 move_linked_works(work, scheduled, &n);
2396
2397 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002398
2399 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002400 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002401 * regular worker; otherwise, we end up with 0 concurrency
2402 * and stalling the execution.
2403 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002404 if (keep_working(pool))
2405 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002406
Lai Jiangshanb3104102013-02-19 12:17:02 -08002407 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002408 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002409 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002410 }
2411
Tejun Heo2e109a22013-03-13 19:47:40 -07002412 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002413
Tejun Heo111c2252013-01-17 17:16:24 -08002414 /* rescuers should never participate in concurrency management */
2415 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002416 schedule();
2417 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002420struct wq_barrier {
2421 struct work_struct work;
2422 struct completion done;
2423};
2424
2425static void wq_barrier_func(struct work_struct *work)
2426{
2427 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2428 complete(&barr->done);
2429}
2430
Tejun Heo4690c4a2010-06-29 10:07:10 +02002431/**
2432 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002433 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002434 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002435 * @target: target work to attach @barr to
2436 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002437 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002438 * @barr is linked to @target such that @barr is completed only after
2439 * @target finishes execution. Please note that the ordering
2440 * guarantee is observed only with respect to @target and on the local
2441 * cpu.
2442 *
2443 * Currently, a queued barrier can't be canceled. This is because
2444 * try_to_grab_pending() can't determine whether the work to be
2445 * grabbed is at the head of the queue and thus can't clear LINKED
2446 * flag of the previous work while there must be a valid next work
2447 * after a work with LINKED flag set.
2448 *
2449 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002450 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002451 *
2452 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002453 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002454 */
Tejun Heo112202d2013-02-13 19:29:12 -08002455static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002456 struct wq_barrier *barr,
2457 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002458{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002459 struct list_head *head;
2460 unsigned int linked = 0;
2461
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002462 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002463 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002464 * as we know for sure that this will not trigger any of the
2465 * checks and call back into the fixup functions where we
2466 * might deadlock.
2467 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002468 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002469 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002470 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002471
Tejun Heoaffee4b2010-06-29 10:07:12 +02002472 /*
2473 * If @target is currently being executed, schedule the
2474 * barrier to the worker; otherwise, put it after @target.
2475 */
2476 if (worker)
2477 head = worker->scheduled.next;
2478 else {
2479 unsigned long *bits = work_data_bits(target);
2480
2481 head = target->entry.next;
2482 /* there can already be other linked works, inherit and set */
2483 linked = *bits & WORK_STRUCT_LINKED;
2484 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2485 }
2486
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002487 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002488 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002489 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002490}
2491
Tejun Heo73f53c42010-06-29 10:07:11 +02002492/**
Tejun Heo112202d2013-02-13 19:29:12 -08002493 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002494 * @wq: workqueue being flushed
2495 * @flush_color: new flush color, < 0 for no-op
2496 * @work_color: new work color, < 0 for no-op
2497 *
Tejun Heo112202d2013-02-13 19:29:12 -08002498 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002499 *
Tejun Heo112202d2013-02-13 19:29:12 -08002500 * If @flush_color is non-negative, flush_color on all pwqs should be
2501 * -1. If no pwq has in-flight commands at the specified color, all
2502 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2503 * has in flight commands, its pwq->flush_color is set to
2504 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 * wakeup logic is armed and %true is returned.
2506 *
2507 * The caller should have initialized @wq->first_flusher prior to
2508 * calling this function with non-negative @flush_color. If
2509 * @flush_color is negative, no flush color update is done and %false
2510 * is returned.
2511 *
Tejun Heo112202d2013-02-13 19:29:12 -08002512 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002513 * work_color which is previous to @work_color and all will be
2514 * advanced to @work_color.
2515 *
2516 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002517 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002518 *
2519 * RETURNS:
2520 * %true if @flush_color >= 0 and there's something to flush. %false
2521 * otherwise.
2522 */
Tejun Heo112202d2013-02-13 19:29:12 -08002523static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002524 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Tejun Heo73f53c42010-06-29 10:07:11 +02002526 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002527 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002528
Tejun Heo73f53c42010-06-29 10:07:11 +02002529 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002530 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002531 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002532 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002533
Tejun Heo49e3cf42013-03-12 11:29:58 -07002534 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002535 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002536
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002537 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002538
2539 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002540 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002541
Tejun Heo112202d2013-02-13 19:29:12 -08002542 if (pwq->nr_in_flight[flush_color]) {
2543 pwq->flush_color = flush_color;
2544 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002545 wait = true;
2546 }
2547 }
2548
2549 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002550 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002551 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002552 }
2553
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002554 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002555 }
2556
Tejun Heo112202d2013-02-13 19:29:12 -08002557 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002558 complete(&wq->first_flusher->done);
2559
2560 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002563/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002565 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002567 * This function sleeps until all work items which were queued on entry
2568 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002570void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Tejun Heo73f53c42010-06-29 10:07:11 +02002572 struct wq_flusher this_flusher = {
2573 .list = LIST_HEAD_INIT(this_flusher.list),
2574 .flush_color = -1,
2575 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2576 };
2577 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002578
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002579 lock_map_acquire(&wq->lockdep_map);
2580 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002581
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002582 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002583
2584 /*
2585 * Start-to-wait phase
2586 */
2587 next_color = work_next_color(wq->work_color);
2588
2589 if (next_color != wq->flush_color) {
2590 /*
2591 * Color space is not full. The current work_color
2592 * becomes our flush_color and work_color is advanced
2593 * by one.
2594 */
Tejun Heo6183c002013-03-12 11:29:57 -07002595 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002596 this_flusher.flush_color = wq->work_color;
2597 wq->work_color = next_color;
2598
2599 if (!wq->first_flusher) {
2600 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002601 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002602
2603 wq->first_flusher = &this_flusher;
2604
Tejun Heo112202d2013-02-13 19:29:12 -08002605 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002606 wq->work_color)) {
2607 /* nothing to flush, done */
2608 wq->flush_color = next_color;
2609 wq->first_flusher = NULL;
2610 goto out_unlock;
2611 }
2612 } else {
2613 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002614 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002615 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002616 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002617 }
2618 } else {
2619 /*
2620 * Oops, color space is full, wait on overflow queue.
2621 * The next flush completion will assign us
2622 * flush_color and transfer to flusher_queue.
2623 */
2624 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2625 }
2626
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002627 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002628
2629 wait_for_completion(&this_flusher.done);
2630
2631 /*
2632 * Wake-up-and-cascade phase
2633 *
2634 * First flushers are responsible for cascading flushes and
2635 * handling overflow. Non-first flushers can simply return.
2636 */
2637 if (wq->first_flusher != &this_flusher)
2638 return;
2639
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002640 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002641
Tejun Heo4ce48b32010-07-02 10:03:51 +02002642 /* we might have raced, check again with mutex held */
2643 if (wq->first_flusher != &this_flusher)
2644 goto out_unlock;
2645
Tejun Heo73f53c42010-06-29 10:07:11 +02002646 wq->first_flusher = NULL;
2647
Tejun Heo6183c002013-03-12 11:29:57 -07002648 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2649 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002650
2651 while (true) {
2652 struct wq_flusher *next, *tmp;
2653
2654 /* complete all the flushers sharing the current flush color */
2655 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2656 if (next->flush_color != wq->flush_color)
2657 break;
2658 list_del_init(&next->list);
2659 complete(&next->done);
2660 }
2661
Tejun Heo6183c002013-03-12 11:29:57 -07002662 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2663 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002664
2665 /* this flush_color is finished, advance by one */
2666 wq->flush_color = work_next_color(wq->flush_color);
2667
2668 /* one color has been freed, handle overflow queue */
2669 if (!list_empty(&wq->flusher_overflow)) {
2670 /*
2671 * Assign the same color to all overflowed
2672 * flushers, advance work_color and append to
2673 * flusher_queue. This is the start-to-wait
2674 * phase for these overflowed flushers.
2675 */
2676 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2677 tmp->flush_color = wq->work_color;
2678
2679 wq->work_color = work_next_color(wq->work_color);
2680
2681 list_splice_tail_init(&wq->flusher_overflow,
2682 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002683 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002684 }
2685
2686 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002687 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002688 break;
2689 }
2690
2691 /*
2692 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002693 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002694 */
Tejun Heo6183c002013-03-12 11:29:57 -07002695 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2696 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002697
2698 list_del_init(&next->list);
2699 wq->first_flusher = next;
2700
Tejun Heo112202d2013-02-13 19:29:12 -08002701 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002702 break;
2703
2704 /*
2705 * Meh... this color is already done, clear first
2706 * flusher and repeat cascading.
2707 */
2708 wq->first_flusher = NULL;
2709 }
2710
2711out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002712 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
Dave Jonesae90dd52006-06-30 01:40:45 -04002714EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002716/**
2717 * drain_workqueue - drain a workqueue
2718 * @wq: workqueue to drain
2719 *
2720 * Wait until the workqueue becomes empty. While draining is in progress,
2721 * only chain queueing is allowed. IOW, only currently pending or running
2722 * work items on @wq can queue further work items on it. @wq is flushed
2723 * repeatedly until it becomes empty. The number of flushing is detemined
2724 * by the depth of chaining and should be relatively short. Whine if it
2725 * takes too long.
2726 */
2727void drain_workqueue(struct workqueue_struct *wq)
2728{
2729 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002730 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002731
2732 /*
2733 * __queue_work() needs to test whether there are drainers, is much
2734 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002735 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002736 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002737 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002738 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002739 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002740 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002741reflush:
2742 flush_workqueue(wq);
2743
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002744 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002745
Tejun Heo49e3cf42013-03-12 11:29:58 -07002746 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002747 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002748
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002749 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002750 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002751 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002752
2753 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002754 continue;
2755
2756 if (++flush_cnt == 10 ||
2757 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002758 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002759 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002760
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002761 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002762 goto reflush;
2763 }
2764
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002765 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002766 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002767 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002768}
2769EXPORT_SYMBOL_GPL(drain_workqueue);
2770
Tejun Heo606a5022012-08-20 14:51:23 -07002771static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002772{
2773 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002774 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002775 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002776
2777 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002778
Tejun Heofa1b54e2013-03-12 11:30:00 -07002779 local_irq_disable();
2780 pool = get_work_pool(work);
2781 if (!pool) {
2782 local_irq_enable();
2783 return false;
2784 }
2785
2786 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002787 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002788 pwq = get_work_pwq(work);
2789 if (pwq) {
2790 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002791 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002792 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002793 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002794 if (!worker)
2795 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002796 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002797 }
Tejun Heobaf59022010-09-16 10:42:16 +02002798
Tejun Heo112202d2013-02-13 19:29:12 -08002799 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002800 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002801
Tejun Heoe1594892011-01-09 23:32:15 +01002802 /*
2803 * If @max_active is 1 or rescuer is in use, flushing another work
2804 * item on the same workqueue may lead to deadlock. Make sure the
2805 * flusher is not running on the same workqueue by verifying write
2806 * access.
2807 */
Tejun Heo493008a2013-03-12 11:30:03 -07002808 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002809 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002810 else
Tejun Heo112202d2013-02-13 19:29:12 -08002811 lock_map_acquire_read(&pwq->wq->lockdep_map);
2812 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002813
Tejun Heobaf59022010-09-16 10:42:16 +02002814 return true;
2815already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002816 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002817 return false;
2818}
2819
Oleg Nesterovdb700892008-07-25 01:47:49 -07002820/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002821 * flush_work - wait for a work to finish executing the last queueing instance
2822 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002823 *
Tejun Heo606a5022012-08-20 14:51:23 -07002824 * Wait until @work has finished execution. @work is guaranteed to be idle
2825 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002826 *
2827 * RETURNS:
2828 * %true if flush_work() waited for the work to finish execution,
2829 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002830 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002831bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002832{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002833 struct wq_barrier barr;
2834
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002835 lock_map_acquire(&work->lockdep_map);
2836 lock_map_release(&work->lockdep_map);
2837
Tejun Heo606a5022012-08-20 14:51:23 -07002838 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002839 wait_for_completion(&barr.done);
2840 destroy_work_on_stack(&barr.work);
2841 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002842 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002843 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002844 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002845}
2846EXPORT_SYMBOL_GPL(flush_work);
2847
Tejun Heo36e227d2012-08-03 10:30:46 -07002848static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002849{
Tejun Heobbb68df2012-08-03 10:30:46 -07002850 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002851 int ret;
2852
2853 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002854 ret = try_to_grab_pending(work, is_dwork, &flags);
2855 /*
2856 * If someone else is canceling, wait for the same event it
2857 * would be waiting for before retrying.
2858 */
2859 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002860 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002861 } while (unlikely(ret < 0));
2862
Tejun Heobbb68df2012-08-03 10:30:46 -07002863 /* tell other tasks trying to grab @work to back off */
2864 mark_work_canceling(work);
2865 local_irq_restore(flags);
2866
Tejun Heo606a5022012-08-20 14:51:23 -07002867 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002868 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002869 return ret;
2870}
2871
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002872/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002873 * cancel_work_sync - cancel a work and wait for it to finish
2874 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002875 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002876 * Cancel @work and wait for its execution to finish. This function
2877 * can be used even if the work re-queues itself or migrates to
2878 * another workqueue. On return from this function, @work is
2879 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002880 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002881 * cancel_work_sync(&delayed_work->work) must not be used for
2882 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002883 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002884 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002885 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002886 *
2887 * RETURNS:
2888 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002889 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002890bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002891{
Tejun Heo36e227d2012-08-03 10:30:46 -07002892 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002893}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002894EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002895
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002896/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002897 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2898 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002899 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002900 * Delayed timer is cancelled and the pending work is queued for
2901 * immediate execution. Like flush_work(), this function only
2902 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002903 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 * RETURNS:
2905 * %true if flush_work() waited for the work to finish execution,
2906 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002907 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002908bool flush_delayed_work(struct delayed_work *dwork)
2909{
Tejun Heo8930cab2012-08-03 10:30:45 -07002910 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002911 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002912 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002913 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002914 return flush_work(&dwork->work);
2915}
2916EXPORT_SYMBOL(flush_delayed_work);
2917
2918/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002919 * cancel_delayed_work - cancel a delayed work
2920 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002921 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002922 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2923 * and canceled; %false if wasn't pending. Note that the work callback
2924 * function may still be running on return, unless it returns %true and the
2925 * work doesn't re-arm itself. Explicitly flush or use
2926 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002927 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002928 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002929 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002930bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002931{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002932 unsigned long flags;
2933 int ret;
2934
2935 do {
2936 ret = try_to_grab_pending(&dwork->work, true, &flags);
2937 } while (unlikely(ret == -EAGAIN));
2938
2939 if (unlikely(ret < 0))
2940 return false;
2941
Tejun Heo7c3eed52013-01-24 11:01:33 -08002942 set_work_pool_and_clear_pending(&dwork->work,
2943 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002944 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002945 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002946}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002948
2949/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002950 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2951 * @dwork: the delayed work cancel
2952 *
2953 * This is cancel_work_sync() for delayed works.
2954 *
2955 * RETURNS:
2956 * %true if @dwork was pending, %false otherwise.
2957 */
2958bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002959{
Tejun Heo36e227d2012-08-03 10:30:46 -07002960 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002961}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002962EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002964/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002965 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002966 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002967 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002968 * schedule_on_each_cpu() executes @func on each online CPU using the
2969 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002970 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002971 *
2972 * RETURNS:
2973 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002974 */
David Howells65f27f32006-11-22 14:55:48 +00002975int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002976{
2977 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002978 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002979
Andrew Mortonb6136772006-06-25 05:47:49 -07002980 works = alloc_percpu(struct work_struct);
2981 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002982 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002983
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002984 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002985
Christoph Lameter15316ba2006-01-08 01:00:43 -08002986 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002987 struct work_struct *work = per_cpu_ptr(works, cpu);
2988
2989 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002990 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002991 }
Tejun Heo93981802009-11-17 14:06:20 -08002992
2993 for_each_online_cpu(cpu)
2994 flush_work(per_cpu_ptr(works, cpu));
2995
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002996 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002997 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002998 return 0;
2999}
3000
Alan Sterneef6a7d2010-02-12 17:39:21 +09003001/**
3002 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3003 *
3004 * Forces execution of the kernel-global workqueue and blocks until its
3005 * completion.
3006 *
3007 * Think twice before calling this function! It's very easy to get into
3008 * trouble if you don't take great care. Either of the following situations
3009 * will lead to deadlock:
3010 *
3011 * One of the work items currently on the workqueue needs to acquire
3012 * a lock held by your code or its caller.
3013 *
3014 * Your code is running in the context of a work routine.
3015 *
3016 * They will be detected by lockdep when they occur, but the first might not
3017 * occur very often. It depends on what work items are on the workqueue and
3018 * what locks they need, which you have no control over.
3019 *
3020 * In most situations flushing the entire workqueue is overkill; you merely
3021 * need to know that a particular work item isn't queued and isn't running.
3022 * In such cases you should use cancel_delayed_work_sync() or
3023 * cancel_work_sync() instead.
3024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025void flush_scheduled_work(void)
3026{
Tejun Heod320c032010-06-29 10:07:14 +02003027 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
Dave Jonesae90dd52006-06-30 01:40:45 -04003029EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003032 * execute_in_process_context - reliably execute the routine with user context
3033 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003034 * @ew: guaranteed storage for the execute work structure (must
3035 * be available when the work executes)
3036 *
3037 * Executes the function immediately if process context is available,
3038 * otherwise schedules the function for delayed execution.
3039 *
3040 * Returns: 0 - function was executed
3041 * 1 - function was scheduled for execution
3042 */
David Howells65f27f32006-11-22 14:55:48 +00003043int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003044{
3045 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003046 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003047 return 0;
3048 }
3049
David Howells65f27f32006-11-22 14:55:48 +00003050 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003051 schedule_work(&ew->work);
3052
3053 return 1;
3054}
3055EXPORT_SYMBOL_GPL(execute_in_process_context);
3056
Tejun Heo226223a2013-03-12 11:30:05 -07003057#ifdef CONFIG_SYSFS
3058/*
3059 * Workqueues with WQ_SYSFS flag set is visible to userland via
3060 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3061 * following attributes.
3062 *
3063 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3064 * max_active RW int : maximum number of in-flight work items
3065 *
3066 * Unbound workqueues have the following extra attributes.
3067 *
3068 * id RO int : the associated pool ID
3069 * nice RW int : nice value of the workers
3070 * cpumask RW mask : bitmask of allowed CPUs for the workers
3071 */
3072struct wq_device {
3073 struct workqueue_struct *wq;
3074 struct device dev;
3075};
3076
3077static struct workqueue_struct *dev_to_wq(struct device *dev)
3078{
3079 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3080
3081 return wq_dev->wq;
3082}
3083
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003084static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr,
3085 char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003086{
3087 struct workqueue_struct *wq = dev_to_wq(dev);
3088
3089 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3090}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003091static DEVICE_ATTR_RO(per_cpu);
Tejun Heo226223a2013-03-12 11:30:05 -07003092
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003093static ssize_t max_active_show(struct device *dev,
3094 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003095{
3096 struct workqueue_struct *wq = dev_to_wq(dev);
3097
3098 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3099}
3100
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003101static ssize_t max_active_store(struct device *dev,
3102 struct device_attribute *attr, const char *buf,
3103 size_t count)
Tejun Heo226223a2013-03-12 11:30:05 -07003104{
3105 struct workqueue_struct *wq = dev_to_wq(dev);
3106 int val;
3107
3108 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3109 return -EINVAL;
3110
3111 workqueue_set_max_active(wq, val);
3112 return count;
3113}
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003114static DEVICE_ATTR_RW(max_active);
Tejun Heo226223a2013-03-12 11:30:05 -07003115
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003116static struct attribute *wq_sysfs_attrs[] = {
3117 &dev_attr_per_cpu.attr,
3118 &dev_attr_max_active.attr,
3119 NULL,
Tejun Heo226223a2013-03-12 11:30:05 -07003120};
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003121ATTRIBUTE_GROUPS(wq_sysfs);
Tejun Heo226223a2013-03-12 11:30:05 -07003122
Tejun Heod55262c2013-04-01 11:23:38 -07003123static ssize_t wq_pool_ids_show(struct device *dev,
3124 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003125{
3126 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003127 const char *delim = "";
3128 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003129
3130 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003131 for_each_node(node) {
3132 written += scnprintf(buf + written, PAGE_SIZE - written,
3133 "%s%d:%d", delim, node,
3134 unbound_pwq_by_node(wq, node)->pool->id);
3135 delim = " ";
3136 }
3137 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003138 rcu_read_unlock_sched();
3139
3140 return written;
3141}
3142
3143static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3144 char *buf)
3145{
3146 struct workqueue_struct *wq = dev_to_wq(dev);
3147 int written;
3148
Tejun Heo6029a912013-04-01 11:23:34 -07003149 mutex_lock(&wq->mutex);
3150 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3151 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003152
3153 return written;
3154}
3155
3156/* prepare workqueue_attrs for sysfs store operations */
3157static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3158{
3159 struct workqueue_attrs *attrs;
3160
3161 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3162 if (!attrs)
3163 return NULL;
3164
Tejun Heo6029a912013-04-01 11:23:34 -07003165 mutex_lock(&wq->mutex);
3166 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3167 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003168 return attrs;
3169}
3170
3171static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3172 const char *buf, size_t count)
3173{
3174 struct workqueue_struct *wq = dev_to_wq(dev);
3175 struct workqueue_attrs *attrs;
3176 int ret;
3177
3178 attrs = wq_sysfs_prep_attrs(wq);
3179 if (!attrs)
3180 return -ENOMEM;
3181
3182 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3183 attrs->nice >= -20 && attrs->nice <= 19)
3184 ret = apply_workqueue_attrs(wq, attrs);
3185 else
3186 ret = -EINVAL;
3187
3188 free_workqueue_attrs(attrs);
3189 return ret ?: count;
3190}
3191
3192static ssize_t wq_cpumask_show(struct device *dev,
3193 struct device_attribute *attr, char *buf)
3194{
3195 struct workqueue_struct *wq = dev_to_wq(dev);
3196 int written;
3197
Tejun Heo6029a912013-04-01 11:23:34 -07003198 mutex_lock(&wq->mutex);
3199 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3200 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003201
3202 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3203 return written;
3204}
3205
3206static ssize_t wq_cpumask_store(struct device *dev,
3207 struct device_attribute *attr,
3208 const char *buf, size_t count)
3209{
3210 struct workqueue_struct *wq = dev_to_wq(dev);
3211 struct workqueue_attrs *attrs;
3212 int ret;
3213
3214 attrs = wq_sysfs_prep_attrs(wq);
3215 if (!attrs)
3216 return -ENOMEM;
3217
3218 ret = cpumask_parse(buf, attrs->cpumask);
3219 if (!ret)
3220 ret = apply_workqueue_attrs(wq, attrs);
3221
3222 free_workqueue_attrs(attrs);
3223 return ret ?: count;
3224}
3225
Tejun Heod55262c2013-04-01 11:23:38 -07003226static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3227 char *buf)
3228{
3229 struct workqueue_struct *wq = dev_to_wq(dev);
3230 int written;
3231
3232 mutex_lock(&wq->mutex);
3233 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3234 !wq->unbound_attrs->no_numa);
3235 mutex_unlock(&wq->mutex);
3236
3237 return written;
3238}
3239
3240static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3241 const char *buf, size_t count)
3242{
3243 struct workqueue_struct *wq = dev_to_wq(dev);
3244 struct workqueue_attrs *attrs;
3245 int v, ret;
3246
3247 attrs = wq_sysfs_prep_attrs(wq);
3248 if (!attrs)
3249 return -ENOMEM;
3250
3251 ret = -EINVAL;
3252 if (sscanf(buf, "%d", &v) == 1) {
3253 attrs->no_numa = !v;
3254 ret = apply_workqueue_attrs(wq, attrs);
3255 }
3256
3257 free_workqueue_attrs(attrs);
3258 return ret ?: count;
3259}
3260
Tejun Heo226223a2013-03-12 11:30:05 -07003261static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003262 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003263 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3264 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003265 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003266 __ATTR_NULL,
3267};
3268
3269static struct bus_type wq_subsys = {
3270 .name = "workqueue",
Greg Kroah-Hartman1a6661d2013-08-23 14:24:41 -07003271 .dev_groups = wq_sysfs_groups,
Tejun Heo226223a2013-03-12 11:30:05 -07003272};
3273
3274static int __init wq_sysfs_init(void)
3275{
3276 return subsys_virtual_register(&wq_subsys, NULL);
3277}
3278core_initcall(wq_sysfs_init);
3279
3280static void wq_device_release(struct device *dev)
3281{
3282 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3283
3284 kfree(wq_dev);
3285}
3286
3287/**
3288 * workqueue_sysfs_register - make a workqueue visible in sysfs
3289 * @wq: the workqueue to register
3290 *
3291 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3292 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3293 * which is the preferred method.
3294 *
3295 * Workqueue user should use this function directly iff it wants to apply
3296 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3297 * apply_workqueue_attrs() may race against userland updating the
3298 * attributes.
3299 *
3300 * Returns 0 on success, -errno on failure.
3301 */
3302int workqueue_sysfs_register(struct workqueue_struct *wq)
3303{
3304 struct wq_device *wq_dev;
3305 int ret;
3306
3307 /*
3308 * Adjusting max_active or creating new pwqs by applyting
3309 * attributes breaks ordering guarantee. Disallow exposing ordered
3310 * workqueues.
3311 */
3312 if (WARN_ON(wq->flags & __WQ_ORDERED))
3313 return -EINVAL;
3314
3315 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3316 if (!wq_dev)
3317 return -ENOMEM;
3318
3319 wq_dev->wq = wq;
3320 wq_dev->dev.bus = &wq_subsys;
3321 wq_dev->dev.init_name = wq->name;
3322 wq_dev->dev.release = wq_device_release;
3323
3324 /*
3325 * unbound_attrs are created separately. Suppress uevent until
3326 * everything is ready.
3327 */
3328 dev_set_uevent_suppress(&wq_dev->dev, true);
3329
3330 ret = device_register(&wq_dev->dev);
3331 if (ret) {
3332 kfree(wq_dev);
3333 wq->wq_dev = NULL;
3334 return ret;
3335 }
3336
3337 if (wq->flags & WQ_UNBOUND) {
3338 struct device_attribute *attr;
3339
3340 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3341 ret = device_create_file(&wq_dev->dev, attr);
3342 if (ret) {
3343 device_unregister(&wq_dev->dev);
3344 wq->wq_dev = NULL;
3345 return ret;
3346 }
3347 }
3348 }
3349
3350 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3351 return 0;
3352}
3353
3354/**
3355 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3356 * @wq: the workqueue to unregister
3357 *
3358 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3359 */
3360static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3361{
3362 struct wq_device *wq_dev = wq->wq_dev;
3363
3364 if (!wq->wq_dev)
3365 return;
3366
3367 wq->wq_dev = NULL;
3368 device_unregister(&wq_dev->dev);
3369}
3370#else /* CONFIG_SYSFS */
3371static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3372#endif /* CONFIG_SYSFS */
3373
Tejun Heo7a4e3442013-03-12 11:30:00 -07003374/**
3375 * free_workqueue_attrs - free a workqueue_attrs
3376 * @attrs: workqueue_attrs to free
3377 *
3378 * Undo alloc_workqueue_attrs().
3379 */
3380void free_workqueue_attrs(struct workqueue_attrs *attrs)
3381{
3382 if (attrs) {
3383 free_cpumask_var(attrs->cpumask);
3384 kfree(attrs);
3385 }
3386}
3387
3388/**
3389 * alloc_workqueue_attrs - allocate a workqueue_attrs
3390 * @gfp_mask: allocation mask to use
3391 *
3392 * Allocate a new workqueue_attrs, initialize with default settings and
3393 * return it. Returns NULL on failure.
3394 */
3395struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3396{
3397 struct workqueue_attrs *attrs;
3398
3399 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3400 if (!attrs)
3401 goto fail;
3402 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3403 goto fail;
3404
Tejun Heo13e2e552013-04-01 11:23:31 -07003405 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003406 return attrs;
3407fail:
3408 free_workqueue_attrs(attrs);
3409 return NULL;
3410}
3411
Tejun Heo29c91e92013-03-12 11:30:03 -07003412static void copy_workqueue_attrs(struct workqueue_attrs *to,
3413 const struct workqueue_attrs *from)
3414{
3415 to->nice = from->nice;
3416 cpumask_copy(to->cpumask, from->cpumask);
3417}
3418
Tejun Heo29c91e92013-03-12 11:30:03 -07003419/* hash value of the content of @attr */
3420static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3421{
3422 u32 hash = 0;
3423
3424 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003425 hash = jhash(cpumask_bits(attrs->cpumask),
3426 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003427 return hash;
3428}
3429
3430/* content equality test */
3431static bool wqattrs_equal(const struct workqueue_attrs *a,
3432 const struct workqueue_attrs *b)
3433{
3434 if (a->nice != b->nice)
3435 return false;
3436 if (!cpumask_equal(a->cpumask, b->cpumask))
3437 return false;
3438 return true;
3439}
3440
Tejun Heo7a4e3442013-03-12 11:30:00 -07003441/**
3442 * init_worker_pool - initialize a newly zalloc'd worker_pool
3443 * @pool: worker_pool to initialize
3444 *
3445 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003446 * Returns 0 on success, -errno on failure. Even on failure, all fields
3447 * inside @pool proper are initialized and put_unbound_pool() can be called
3448 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003449 */
3450static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003451{
3452 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003453 pool->id = -1;
3454 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003455 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003456 pool->flags |= POOL_DISASSOCIATED;
3457 INIT_LIST_HEAD(&pool->worklist);
3458 INIT_LIST_HEAD(&pool->idle_list);
3459 hash_init(pool->busy_hash);
3460
3461 init_timer_deferrable(&pool->idle_timer);
3462 pool->idle_timer.function = idle_worker_timeout;
3463 pool->idle_timer.data = (unsigned long)pool;
3464
3465 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3466 (unsigned long)pool);
3467
3468 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003469 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003470 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003471
Tejun Heo29c91e92013-03-12 11:30:03 -07003472 INIT_HLIST_NODE(&pool->hash_node);
3473 pool->refcnt = 1;
3474
3475 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003476 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3477 if (!pool->attrs)
3478 return -ENOMEM;
3479 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003480}
3481
Tejun Heo29c91e92013-03-12 11:30:03 -07003482static void rcu_free_pool(struct rcu_head *rcu)
3483{
3484 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3485
Tejun Heo822d8402013-03-19 13:45:21 -07003486 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003487 free_workqueue_attrs(pool->attrs);
3488 kfree(pool);
3489}
3490
3491/**
3492 * put_unbound_pool - put a worker_pool
3493 * @pool: worker_pool to put
3494 *
3495 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003496 * safe manner. get_unbound_pool() calls this function on its failure path
3497 * and this function should be able to release pools which went through,
3498 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003499 *
3500 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003501 */
3502static void put_unbound_pool(struct worker_pool *pool)
3503{
3504 struct worker *worker;
3505
Tejun Heoa892cac2013-04-01 11:23:32 -07003506 lockdep_assert_held(&wq_pool_mutex);
3507
3508 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003509 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003510
3511 /* sanity checks */
3512 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003513 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003514 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003515
3516 /* release id and unhash */
3517 if (pool->id >= 0)
3518 idr_remove(&worker_pool_idr, pool->id);
3519 hash_del(&pool->hash_node);
3520
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003521 /*
3522 * Become the manager and destroy all workers. Grabbing
3523 * manager_arb prevents @pool's workers from blocking on
3524 * manager_mutex.
3525 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003526 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003527 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003528 spin_lock_irq(&pool->lock);
3529
3530 while ((worker = first_worker(pool)))
3531 destroy_worker(worker);
3532 WARN_ON(pool->nr_workers || pool->nr_idle);
3533
3534 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003535 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003536 mutex_unlock(&pool->manager_arb);
3537
3538 /* shut down the timers */
3539 del_timer_sync(&pool->idle_timer);
3540 del_timer_sync(&pool->mayday_timer);
3541
3542 /* sched-RCU protected to allow dereferences from get_work_pool() */
3543 call_rcu_sched(&pool->rcu, rcu_free_pool);
3544}
3545
3546/**
3547 * get_unbound_pool - get a worker_pool with the specified attributes
3548 * @attrs: the attributes of the worker_pool to get
3549 *
3550 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3551 * reference count and return it. If there already is a matching
3552 * worker_pool, it will be used; otherwise, this function attempts to
3553 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003554 *
3555 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003556 */
3557static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3558{
Tejun Heo29c91e92013-03-12 11:30:03 -07003559 u32 hash = wqattrs_hash(attrs);
3560 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003561 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003562
Tejun Heoa892cac2013-04-01 11:23:32 -07003563 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003564
3565 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003566 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3567 if (wqattrs_equal(pool->attrs, attrs)) {
3568 pool->refcnt++;
3569 goto out_unlock;
3570 }
3571 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003572
3573 /* nope, create a new one */
3574 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3575 if (!pool || init_worker_pool(pool) < 0)
3576 goto fail;
3577
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003578 if (workqueue_freezing)
3579 pool->flags |= POOL_FREEZING;
3580
Tejun Heo8864b4e2013-03-12 11:30:04 -07003581 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003582 copy_workqueue_attrs(pool->attrs, attrs);
3583
Tejun Heof3f90ad2013-04-01 11:23:34 -07003584 /* if cpumask is contained inside a NUMA node, we belong to that node */
3585 if (wq_numa_enabled) {
3586 for_each_node(node) {
3587 if (cpumask_subset(pool->attrs->cpumask,
3588 wq_numa_possible_cpumask[node])) {
3589 pool->node = node;
3590 break;
3591 }
3592 }
3593 }
3594
Tejun Heo29c91e92013-03-12 11:30:03 -07003595 if (worker_pool_assign_id(pool) < 0)
3596 goto fail;
3597
3598 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003599 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003600 goto fail;
3601
Tejun Heo29c91e92013-03-12 11:30:03 -07003602 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003603 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3604out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003605 return pool;
3606fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003607 if (pool)
3608 put_unbound_pool(pool);
3609 return NULL;
3610}
3611
Tejun Heo8864b4e2013-03-12 11:30:04 -07003612static void rcu_free_pwq(struct rcu_head *rcu)
3613{
3614 kmem_cache_free(pwq_cache,
3615 container_of(rcu, struct pool_workqueue, rcu));
3616}
3617
3618/*
3619 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3620 * and needs to be destroyed.
3621 */
3622static void pwq_unbound_release_workfn(struct work_struct *work)
3623{
3624 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3625 unbound_release_work);
3626 struct workqueue_struct *wq = pwq->wq;
3627 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003628 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003629
3630 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3631 return;
3632
Tejun Heo75ccf592013-03-12 11:30:04 -07003633 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003634 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003635 * necessary on release but do it anyway. It's easier to verify
3636 * and consistent with the linking path.
3637 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003638 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003639 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003640 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003641 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003642
Tejun Heoa892cac2013-04-01 11:23:32 -07003643 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003644 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003645 mutex_unlock(&wq_pool_mutex);
3646
Tejun Heo8864b4e2013-03-12 11:30:04 -07003647 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3648
3649 /*
3650 * If we're the last pwq going away, @wq is already dead and no one
3651 * is gonna access it anymore. Free it.
3652 */
Tejun Heo6029a912013-04-01 11:23:34 -07003653 if (is_last) {
3654 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003655 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003656 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003657}
3658
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003659/**
Tejun Heo699ce092013-03-13 16:51:35 -07003660 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003661 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003662 *
Tejun Heo699ce092013-03-13 16:51:35 -07003663 * If @pwq isn't freezing, set @pwq->max_active to the associated
3664 * workqueue's saved_max_active and activate delayed work items
3665 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003666 */
Tejun Heo699ce092013-03-13 16:51:35 -07003667static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003668{
Tejun Heo699ce092013-03-13 16:51:35 -07003669 struct workqueue_struct *wq = pwq->wq;
3670 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003671
Tejun Heo699ce092013-03-13 16:51:35 -07003672 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003673 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003674
3675 /* fast exit for non-freezable wqs */
3676 if (!freezable && pwq->max_active == wq->saved_max_active)
3677 return;
3678
Lai Jiangshana357fc02013-03-25 16:57:19 -07003679 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003680
3681 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3682 pwq->max_active = wq->saved_max_active;
3683
3684 while (!list_empty(&pwq->delayed_works) &&
3685 pwq->nr_active < pwq->max_active)
3686 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003687
3688 /*
3689 * Need to kick a worker after thawed or an unbound wq's
3690 * max_active is bumped. It's a slow path. Do it always.
3691 */
3692 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003693 } else {
3694 pwq->max_active = 0;
3695 }
3696
Lai Jiangshana357fc02013-03-25 16:57:19 -07003697 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003698}
3699
Tejun Heoe50aba92013-04-01 11:23:35 -07003700/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003701static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3702 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003703{
3704 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3705
Tejun Heoe50aba92013-04-01 11:23:35 -07003706 memset(pwq, 0, sizeof(*pwq));
3707
Tejun Heod2c1d402013-03-12 11:30:04 -07003708 pwq->pool = pool;
3709 pwq->wq = wq;
3710 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003711 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003712 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003713 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003714 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003715 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003716}
Tejun Heod2c1d402013-03-12 11:30:04 -07003717
Tejun Heof147f292013-04-01 11:23:35 -07003718/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003719static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003720{
3721 struct workqueue_struct *wq = pwq->wq;
3722
3723 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003724
Tejun Heo1befcf32013-04-01 11:23:35 -07003725 /* may be called multiple times, ignore if already linked */
3726 if (!list_empty(&pwq->pwqs_node))
3727 return;
3728
Tejun Heo983ca252013-03-13 16:51:35 -07003729 /*
3730 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003731 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003732 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003733 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003734
3735 /* sync max_active to the current setting */
3736 pwq_adjust_max_active(pwq);
3737
3738 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003739 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003740}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003741
Tejun Heof147f292013-04-01 11:23:35 -07003742/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3743static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3744 const struct workqueue_attrs *attrs)
3745{
3746 struct worker_pool *pool;
3747 struct pool_workqueue *pwq;
3748
3749 lockdep_assert_held(&wq_pool_mutex);
3750
3751 pool = get_unbound_pool(attrs);
3752 if (!pool)
3753 return NULL;
3754
Tejun Heoe50aba92013-04-01 11:23:35 -07003755 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003756 if (!pwq) {
3757 put_unbound_pool(pool);
3758 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003759 }
Tejun Heo6029a912013-04-01 11:23:34 -07003760
Tejun Heof147f292013-04-01 11:23:35 -07003761 init_pwq(pwq, wq, pool);
3762 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003763}
3764
Tejun Heo4c16bd32013-04-01 11:23:36 -07003765/* undo alloc_unbound_pwq(), used only in the error path */
3766static void free_unbound_pwq(struct pool_workqueue *pwq)
3767{
3768 lockdep_assert_held(&wq_pool_mutex);
3769
3770 if (pwq) {
3771 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003772 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003773 }
3774}
3775
3776/**
3777 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3778 * @attrs: the wq_attrs of interest
3779 * @node: the target NUMA node
3780 * @cpu_going_down: if >= 0, the CPU to consider as offline
3781 * @cpumask: outarg, the resulting cpumask
3782 *
3783 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3784 * @cpu_going_down is >= 0, that cpu is considered offline during
3785 * calculation. The result is stored in @cpumask. This function returns
3786 * %true if the resulting @cpumask is different from @attrs->cpumask,
3787 * %false if equal.
3788 *
3789 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3790 * enabled and @node has online CPUs requested by @attrs, the returned
3791 * cpumask is the intersection of the possible CPUs of @node and
3792 * @attrs->cpumask.
3793 *
3794 * The caller is responsible for ensuring that the cpumask of @node stays
3795 * stable.
3796 */
3797static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3798 int cpu_going_down, cpumask_t *cpumask)
3799{
Tejun Heod55262c2013-04-01 11:23:38 -07003800 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003801 goto use_dfl;
3802
3803 /* does @node have any online CPUs @attrs wants? */
3804 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3805 if (cpu_going_down >= 0)
3806 cpumask_clear_cpu(cpu_going_down, cpumask);
3807
3808 if (cpumask_empty(cpumask))
3809 goto use_dfl;
3810
3811 /* yeap, return possible CPUs in @node that @attrs wants */
3812 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3813 return !cpumask_equal(cpumask, attrs->cpumask);
3814
3815use_dfl:
3816 cpumask_copy(cpumask, attrs->cpumask);
3817 return false;
3818}
3819
Tejun Heo1befcf32013-04-01 11:23:35 -07003820/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3821static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3822 int node,
3823 struct pool_workqueue *pwq)
3824{
3825 struct pool_workqueue *old_pwq;
3826
3827 lockdep_assert_held(&wq->mutex);
3828
3829 /* link_pwq() can handle duplicate calls */
3830 link_pwq(pwq);
3831
3832 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3833 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3834 return old_pwq;
3835}
3836
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003837/**
3838 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3839 * @wq: the target workqueue
3840 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3841 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003842 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3843 * machines, this function maps a separate pwq to each NUMA node with
3844 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3845 * NUMA node it was issued on. Older pwqs are released as in-flight work
3846 * items finish. Note that a work item which repeatedly requeues itself
3847 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003848 *
3849 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3850 * failure.
3851 */
3852int apply_workqueue_attrs(struct workqueue_struct *wq,
3853 const struct workqueue_attrs *attrs)
3854{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003855 struct workqueue_attrs *new_attrs, *tmp_attrs;
3856 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003857 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003858
Tejun Heo8719dce2013-03-12 11:30:04 -07003859 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003860 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3861 return -EINVAL;
3862
Tejun Heo8719dce2013-03-12 11:30:04 -07003863 /* creating multiple pwqs breaks ordering guarantee */
3864 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3865 return -EINVAL;
3866
Tejun Heo4c16bd32013-04-01 11:23:36 -07003867 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003868 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003869 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3870 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003871 goto enomem;
3872
Tejun Heo4c16bd32013-04-01 11:23:36 -07003873 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003874 copy_workqueue_attrs(new_attrs, attrs);
3875 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3876
Tejun Heo4c16bd32013-04-01 11:23:36 -07003877 /*
3878 * We may create multiple pwqs with differing cpumasks. Make a
3879 * copy of @new_attrs which will be modified and used to obtain
3880 * pools.
3881 */
3882 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003883
Tejun Heo4c16bd32013-04-01 11:23:36 -07003884 /*
3885 * CPUs should stay stable across pwq creations and installations.
3886 * Pin CPUs, determine the target cpumask for each node and create
3887 * pwqs accordingly.
3888 */
3889 get_online_cpus();
3890
3891 mutex_lock(&wq_pool_mutex);
3892
3893 /*
3894 * If something goes wrong during CPU up/down, we'll fall back to
3895 * the default pwq covering whole @attrs->cpumask. Always create
3896 * it even if we don't use it immediately.
3897 */
3898 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3899 if (!dfl_pwq)
3900 goto enomem_pwq;
3901
3902 for_each_node(node) {
3903 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3904 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3905 if (!pwq_tbl[node])
3906 goto enomem_pwq;
3907 } else {
3908 dfl_pwq->refcnt++;
3909 pwq_tbl[node] = dfl_pwq;
3910 }
3911 }
3912
3913 mutex_unlock(&wq_pool_mutex);
3914
3915 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003916 mutex_lock(&wq->mutex);
3917
Tejun Heof147f292013-04-01 11:23:35 -07003918 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003919
3920 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003921 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003922 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3923
3924 /* @dfl_pwq might not have been used, ensure it's linked */
3925 link_pwq(dfl_pwq);
3926 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003927
3928 mutex_unlock(&wq->mutex);
3929
Tejun Heo4c16bd32013-04-01 11:23:36 -07003930 /* put the old pwqs */
3931 for_each_node(node)
3932 put_pwq_unlocked(pwq_tbl[node]);
3933 put_pwq_unlocked(dfl_pwq);
3934
3935 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003936 ret = 0;
3937 /* fall through */
3938out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003939 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003940 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003941 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003942 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003943
Tejun Heo4c16bd32013-04-01 11:23:36 -07003944enomem_pwq:
3945 free_unbound_pwq(dfl_pwq);
3946 for_each_node(node)
3947 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3948 free_unbound_pwq(pwq_tbl[node]);
3949 mutex_unlock(&wq_pool_mutex);
3950 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003951enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003952 ret = -ENOMEM;
3953 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003954}
3955
Tejun Heo4c16bd32013-04-01 11:23:36 -07003956/**
3957 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3958 * @wq: the target workqueue
3959 * @cpu: the CPU coming up or going down
3960 * @online: whether @cpu is coming up or going down
3961 *
3962 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3963 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3964 * @wq accordingly.
3965 *
3966 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3967 * falls back to @wq->dfl_pwq which may not be optimal but is always
3968 * correct.
3969 *
3970 * Note that when the last allowed CPU of a NUMA node goes offline for a
3971 * workqueue with a cpumask spanning multiple nodes, the workers which were
3972 * already executing the work items for the workqueue will lose their CPU
3973 * affinity and may execute on any CPU. This is similar to how per-cpu
3974 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3975 * affinity, it's the user's responsibility to flush the work item from
3976 * CPU_DOWN_PREPARE.
3977 */
3978static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3979 bool online)
3980{
3981 int node = cpu_to_node(cpu);
3982 int cpu_off = online ? -1 : cpu;
3983 struct pool_workqueue *old_pwq = NULL, *pwq;
3984 struct workqueue_attrs *target_attrs;
3985 cpumask_t *cpumask;
3986
3987 lockdep_assert_held(&wq_pool_mutex);
3988
3989 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3990 return;
3991
3992 /*
3993 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3994 * Let's use a preallocated one. The following buf is protected by
3995 * CPU hotplug exclusion.
3996 */
3997 target_attrs = wq_update_unbound_numa_attrs_buf;
3998 cpumask = target_attrs->cpumask;
3999
4000 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07004001 if (wq->unbound_attrs->no_numa)
4002 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004003
4004 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
4005 pwq = unbound_pwq_by_node(wq, node);
4006
4007 /*
4008 * Let's determine what needs to be done. If the target cpumask is
4009 * different from wq's, we need to compare it to @pwq's and create
4010 * a new one if they don't match. If the target cpumask equals
4011 * wq's, the default pwq should be used. If @pwq is already the
4012 * default one, nothing to do; otherwise, install the default one.
4013 */
4014 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
4015 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4016 goto out_unlock;
4017 } else {
4018 if (pwq == wq->dfl_pwq)
4019 goto out_unlock;
4020 else
4021 goto use_dfl_pwq;
4022 }
4023
4024 mutex_unlock(&wq->mutex);
4025
4026 /* create a new pwq */
4027 pwq = alloc_unbound_pwq(wq, target_attrs);
4028 if (!pwq) {
4029 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4030 wq->name);
4031 goto out_unlock;
4032 }
4033
4034 /*
4035 * Install the new pwq. As this function is called only from CPU
4036 * hotplug callbacks and applying a new attrs is wrapped with
4037 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4038 * inbetween.
4039 */
4040 mutex_lock(&wq->mutex);
4041 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4042 goto out_unlock;
4043
4044use_dfl_pwq:
4045 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4046 get_pwq(wq->dfl_pwq);
4047 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4048 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4049out_unlock:
4050 mutex_unlock(&wq->mutex);
4051 put_pwq_unlocked(old_pwq);
4052}
4053
Tejun Heo30cdf242013-03-12 11:29:57 -07004054static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004056 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07004057 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004058
Tejun Heo30cdf242013-03-12 11:29:57 -07004059 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004060 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4061 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004062 return -ENOMEM;
4063
4064 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004065 struct pool_workqueue *pwq =
4066 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004067 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004068 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004069
Tejun Heof147f292013-04-01 11:23:35 -07004070 init_pwq(pwq, wq, &cpu_pools[highpri]);
4071
4072 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004073 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004074 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004075 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004076 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07004077 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004078 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004079 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004080}
4081
Tejun Heof3421792010-07-02 10:03:51 +02004082static int wq_clamp_max_active(int max_active, unsigned int flags,
4083 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004084{
Tejun Heof3421792010-07-02 10:03:51 +02004085 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4086
4087 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004088 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4089 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004090
Tejun Heof3421792010-07-02 10:03:51 +02004091 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004092}
4093
Tejun Heob196be82012-01-10 15:11:35 -08004094struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004095 unsigned int flags,
4096 int max_active,
4097 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004098 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004099{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004100 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004101 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004102 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004103 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004104
Viresh Kumarcee22a12013-04-08 16:45:40 +05304105 /* see the comment above the definition of WQ_POWER_EFFICIENT */
4106 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
4107 flags |= WQ_UNBOUND;
4108
Tejun Heoecf68812013-04-01 11:23:34 -07004109 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004110 if (flags & WQ_UNBOUND)
4111 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4112
4113 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004114 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004115 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004116
Tejun Heo6029a912013-04-01 11:23:34 -07004117 if (flags & WQ_UNBOUND) {
4118 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4119 if (!wq->unbound_attrs)
4120 goto err_free_wq;
4121 }
4122
Tejun Heoecf68812013-04-01 11:23:34 -07004123 va_start(args, lock_name);
4124 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004125 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004126
Tejun Heod320c032010-06-29 10:07:14 +02004127 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004128 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004129
Tejun Heob196be82012-01-10 15:11:35 -08004130 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004131 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004132 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004133 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004134 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004135 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004136 INIT_LIST_HEAD(&wq->flusher_queue);
4137 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004138 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004139
Johannes Bergeb13ba82008-01-16 09:51:58 +01004140 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004141 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004142
Tejun Heo30cdf242013-03-12 11:29:57 -07004143 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004144 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004145
Tejun Heo493008a2013-03-12 11:30:03 -07004146 /*
4147 * Workqueues which may be used during memory reclaim should
4148 * have a rescuer to guarantee forward progress.
4149 */
4150 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004151 struct worker *rescuer;
4152
Tejun Heod2c1d402013-03-12 11:30:04 -07004153 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004154 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004155 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004156
Tejun Heo111c2252013-01-17 17:16:24 -08004157 rescuer->rescue_wq = wq;
4158 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004159 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004160 if (IS_ERR(rescuer->task)) {
4161 kfree(rescuer);
4162 goto err_destroy;
4163 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004164
Tejun Heod2c1d402013-03-12 11:30:04 -07004165 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004166 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004167 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004168 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004169
Tejun Heo226223a2013-03-12 11:30:05 -07004170 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4171 goto err_destroy;
4172
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004173 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004174 * wq_pool_mutex protects global freeze state and workqueues list.
4175 * Grab it, adjust max_active and add the new @wq to workqueues
4176 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004177 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004178 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004179
Lai Jiangshana357fc02013-03-25 16:57:19 -07004180 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004181 for_each_pwq(pwq, wq)
4182 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004183 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004184
Tejun Heo15376632010-06-29 10:07:11 +02004185 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004186
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004187 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004188
Oleg Nesterov3af244332007-05-09 02:34:09 -07004189 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004190
4191err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004192 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004193 kfree(wq);
4194 return NULL;
4195err_destroy:
4196 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004197 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004198}
Tejun Heod320c032010-06-29 10:07:14 +02004199EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004200
4201/**
4202 * destroy_workqueue - safely terminate a workqueue
4203 * @wq: target workqueue
4204 *
4205 * Safely destroy a workqueue. All work currently pending will be done first.
4206 */
4207void destroy_workqueue(struct workqueue_struct *wq)
4208{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004209 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004210 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004211
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004212 /* drain it before proceeding with destruction */
4213 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004214
Tejun Heo6183c002013-03-12 11:29:57 -07004215 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004216 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004217 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004218 int i;
4219
Tejun Heo76af4d92013-03-12 11:30:00 -07004220 for (i = 0; i < WORK_NR_COLORS; i++) {
4221 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004222 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004223 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004224 }
4225 }
4226
Lai Jiangshan5c529592013-04-04 10:05:38 +08004227 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004228 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004229 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004230 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004231 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004232 }
Tejun Heo6183c002013-03-12 11:29:57 -07004233 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004234 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004235
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004236 /*
4237 * wq list is used to freeze wq, remove from list after
4238 * flushing is complete in case freeze races us.
4239 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004240 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004241 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004242 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004243
Tejun Heo226223a2013-03-12 11:30:05 -07004244 workqueue_sysfs_unregister(wq);
4245
Tejun Heo493008a2013-03-12 11:30:03 -07004246 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004247 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004248 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004249 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004250 }
4251
Tejun Heo8864b4e2013-03-12 11:30:04 -07004252 if (!(wq->flags & WQ_UNBOUND)) {
4253 /*
4254 * The base ref is never dropped on per-cpu pwqs. Directly
4255 * free the pwqs and wq.
4256 */
4257 free_percpu(wq->cpu_pwqs);
4258 kfree(wq);
4259 } else {
4260 /*
4261 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004262 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4263 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004264 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004265 for_each_node(node) {
4266 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4267 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4268 put_pwq_unlocked(pwq);
4269 }
4270
4271 /*
4272 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4273 * put. Don't access it afterwards.
4274 */
4275 pwq = wq->dfl_pwq;
4276 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004277 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004278 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004279}
4280EXPORT_SYMBOL_GPL(destroy_workqueue);
4281
Tejun Heodcd989c2010-06-29 10:07:14 +02004282/**
4283 * workqueue_set_max_active - adjust max_active of a workqueue
4284 * @wq: target workqueue
4285 * @max_active: new max_active value.
4286 *
4287 * Set max_active of @wq to @max_active.
4288 *
4289 * CONTEXT:
4290 * Don't call from IRQ context.
4291 */
4292void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4293{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004294 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004295
Tejun Heo8719dce2013-03-12 11:30:04 -07004296 /* disallow meddling with max_active for ordered workqueues */
4297 if (WARN_ON(wq->flags & __WQ_ORDERED))
4298 return;
4299
Tejun Heof3421792010-07-02 10:03:51 +02004300 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004301
Lai Jiangshana357fc02013-03-25 16:57:19 -07004302 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004303
4304 wq->saved_max_active = max_active;
4305
Tejun Heo699ce092013-03-13 16:51:35 -07004306 for_each_pwq(pwq, wq)
4307 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004308
Lai Jiangshana357fc02013-03-25 16:57:19 -07004309 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004310}
4311EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4312
4313/**
Tejun Heoe62676162013-03-12 17:41:37 -07004314 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4315 *
4316 * Determine whether %current is a workqueue rescuer. Can be used from
4317 * work functions to determine whether it's being run off the rescuer task.
4318 */
4319bool current_is_workqueue_rescuer(void)
4320{
4321 struct worker *worker = current_wq_worker();
4322
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004323 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07004324}
4325
4326/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004327 * workqueue_congested - test whether a workqueue is congested
4328 * @cpu: CPU in question
4329 * @wq: target workqueue
4330 *
4331 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4332 * no synchronization around this function and the test result is
4333 * unreliable and only useful as advisory hints or for debugging.
4334 *
Tejun Heod3251852013-05-10 11:10:17 -07004335 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4336 * Note that both per-cpu and unbound workqueues may be associated with
4337 * multiple pool_workqueues which have separate congested states. A
4338 * workqueue being congested on one CPU doesn't mean the workqueue is also
4339 * contested on other CPUs / NUMA nodes.
4340 *
Tejun Heodcd989c2010-06-29 10:07:14 +02004341 * RETURNS:
4342 * %true if congested, %false otherwise.
4343 */
Tejun Heod84ff052013-03-12 11:29:59 -07004344bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004345{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004346 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004347 bool ret;
4348
Lai Jiangshan88109452013-03-20 03:28:10 +08004349 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004350
Tejun Heod3251852013-05-10 11:10:17 -07004351 if (cpu == WORK_CPU_UNBOUND)
4352 cpu = smp_processor_id();
4353
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004354 if (!(wq->flags & WQ_UNBOUND))
4355 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4356 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004357 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004358
Tejun Heo76af4d92013-03-12 11:30:00 -07004359 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004360 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004361
4362 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004363}
4364EXPORT_SYMBOL_GPL(workqueue_congested);
4365
4366/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004367 * work_busy - test whether a work is currently pending or running
4368 * @work: the work to be tested
4369 *
4370 * Test whether @work is currently pending or running. There is no
4371 * synchronization around this function and the test result is
4372 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004373 *
4374 * RETURNS:
4375 * OR'd bitmask of WORK_BUSY_* bits.
4376 */
4377unsigned int work_busy(struct work_struct *work)
4378{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004379 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004380 unsigned long flags;
4381 unsigned int ret = 0;
4382
Tejun Heodcd989c2010-06-29 10:07:14 +02004383 if (work_pending(work))
4384 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004385
Tejun Heofa1b54e2013-03-12 11:30:00 -07004386 local_irq_save(flags);
4387 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004388 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004389 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004390 if (find_worker_executing_work(pool, work))
4391 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004392 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004393 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004394 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004395
4396 return ret;
4397}
4398EXPORT_SYMBOL_GPL(work_busy);
4399
Tejun Heo3d1cb202013-04-30 15:27:22 -07004400/**
4401 * set_worker_desc - set description for the current work item
4402 * @fmt: printf-style format string
4403 * @...: arguments for the format string
4404 *
4405 * This function can be called by a running work function to describe what
4406 * the work item is about. If the worker task gets dumped, this
4407 * information will be printed out together to help debugging. The
4408 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4409 */
4410void set_worker_desc(const char *fmt, ...)
4411{
4412 struct worker *worker = current_wq_worker();
4413 va_list args;
4414
4415 if (worker) {
4416 va_start(args, fmt);
4417 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4418 va_end(args);
4419 worker->desc_valid = true;
4420 }
4421}
4422
4423/**
4424 * print_worker_info - print out worker information and description
4425 * @log_lvl: the log level to use when printing
4426 * @task: target task
4427 *
4428 * If @task is a worker and currently executing a work item, print out the
4429 * name of the workqueue being serviced and worker description set with
4430 * set_worker_desc() by the currently executing work item.
4431 *
4432 * This function can be safely called on any task as long as the
4433 * task_struct itself is accessible. While safe, this function isn't
4434 * synchronized and may print out mixups or garbages of limited length.
4435 */
4436void print_worker_info(const char *log_lvl, struct task_struct *task)
4437{
4438 work_func_t *fn = NULL;
4439 char name[WQ_NAME_LEN] = { };
4440 char desc[WORKER_DESC_LEN] = { };
4441 struct pool_workqueue *pwq = NULL;
4442 struct workqueue_struct *wq = NULL;
4443 bool desc_valid = false;
4444 struct worker *worker;
4445
4446 if (!(task->flags & PF_WQ_WORKER))
4447 return;
4448
4449 /*
4450 * This function is called without any synchronization and @task
4451 * could be in any state. Be careful with dereferences.
4452 */
4453 worker = probe_kthread_data(task);
4454
4455 /*
4456 * Carefully copy the associated workqueue's workfn and name. Keep
4457 * the original last '\0' in case the original contains garbage.
4458 */
4459 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4460 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4461 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4462 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4463
4464 /* copy worker description */
4465 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4466 if (desc_valid)
4467 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4468
4469 if (fn || name[0] || desc[0]) {
4470 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4471 if (desc[0])
4472 pr_cont(" (%s)", desc);
4473 pr_cont("\n");
4474 }
4475}
4476
Tejun Heodb7bccf2010-06-29 10:07:12 +02004477/*
4478 * CPU hotplug.
4479 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004480 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004481 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004482 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004483 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004484 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004485 * blocked draining impractical.
4486 *
Tejun Heo24647572013-01-24 11:01:33 -08004487 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004488 * running as an unbound one and allowing it to be reattached later if the
4489 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004490 */
4491
Tejun Heo706026c2013-01-24 11:01:34 -08004492static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004493{
Tejun Heo38db41d2013-01-24 11:01:34 -08004494 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004495 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004496 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004497 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004498
Tejun Heof02ae732013-03-12 11:30:03 -07004499 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004500 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004501
Tejun Heobc3a1af2013-03-13 19:47:39 -07004502 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004503 spin_lock_irq(&pool->lock);
4504
4505 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004506 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004507 * unbound and set DISASSOCIATED. Before this, all workers
4508 * except for the ones which are still executing works from
4509 * before the last CPU down must be on the cpu. After
4510 * this, they may become diasporas.
4511 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004512 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004513 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004514
Tejun Heo24647572013-01-24 11:01:33 -08004515 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004516
Tejun Heo94cf58b2013-01-24 11:01:33 -08004517 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004518 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004519
Lai Jiangshaneb283422013-03-08 15:18:28 -08004520 /*
4521 * Call schedule() so that we cross rq->lock and thus can
4522 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4523 * This is necessary as scheduler callbacks may be invoked
4524 * from other cpus.
4525 */
4526 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004527
Lai Jiangshaneb283422013-03-08 15:18:28 -08004528 /*
4529 * Sched callbacks are disabled now. Zap nr_running.
4530 * After this, nr_running stays zero and need_more_worker()
4531 * and keep_working() are always true as long as the
4532 * worklist is not empty. This pool now behaves as an
4533 * unbound (in terms of concurrency management) pool which
4534 * are served by workers tied to the pool.
4535 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004536 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004537
4538 /*
4539 * With concurrency management just turned off, a busy
4540 * worker blocking could lead to lengthy stalls. Kick off
4541 * unbound chain execution of currently pending work items.
4542 */
4543 spin_lock_irq(&pool->lock);
4544 wake_up_worker(pool);
4545 spin_unlock_irq(&pool->lock);
4546 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004547}
4548
Tejun Heobd7c0892013-03-19 13:45:21 -07004549/**
4550 * rebind_workers - rebind all workers of a pool to the associated CPU
4551 * @pool: pool of interest
4552 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004553 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004554 */
4555static void rebind_workers(struct worker_pool *pool)
4556{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004557 struct worker *worker;
4558 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004559
4560 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004561
Tejun Heoa9ab7752013-03-19 13:45:21 -07004562 /*
4563 * Restore CPU affinity of all workers. As all idle workers should
4564 * be on the run-queue of the associated CPU before any local
4565 * wake-ups for concurrency management happen, restore CPU affinty
4566 * of all workers first and then clear UNBOUND. As we're called
4567 * from CPU_ONLINE, the following shouldn't fail.
4568 */
4569 for_each_pool_worker(worker, wi, pool)
4570 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4571 pool->attrs->cpumask) < 0);
4572
4573 spin_lock_irq(&pool->lock);
4574
4575 for_each_pool_worker(worker, wi, pool) {
4576 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004577
4578 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004579 * A bound idle worker should actually be on the runqueue
4580 * of the associated CPU for local wake-ups targeting it to
4581 * work. Kick all idle workers so that they migrate to the
4582 * associated CPU. Doing this in the same loop as
4583 * replacing UNBOUND with REBOUND is safe as no worker will
4584 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004585 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004586 if (worker_flags & WORKER_IDLE)
4587 wake_up_process(worker->task);
4588
4589 /*
4590 * We want to clear UNBOUND but can't directly call
4591 * worker_clr_flags() or adjust nr_running. Atomically
4592 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4593 * @worker will clear REBOUND using worker_clr_flags() when
4594 * it initiates the next execution cycle thus restoring
4595 * concurrency management. Note that when or whether
4596 * @worker clears REBOUND doesn't affect correctness.
4597 *
4598 * ACCESS_ONCE() is necessary because @worker->flags may be
4599 * tested without holding any lock in
4600 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4601 * fail incorrectly leading to premature concurrency
4602 * management operations.
4603 */
4604 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4605 worker_flags |= WORKER_REBOUND;
4606 worker_flags &= ~WORKER_UNBOUND;
4607 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004608 }
4609
Tejun Heoa9ab7752013-03-19 13:45:21 -07004610 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004611}
4612
Tejun Heo7dbc7252013-03-19 13:45:21 -07004613/**
4614 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4615 * @pool: unbound pool of interest
4616 * @cpu: the CPU which is coming up
4617 *
4618 * An unbound pool may end up with a cpumask which doesn't have any online
4619 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4620 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4621 * online CPU before, cpus_allowed of all its workers should be restored.
4622 */
4623static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4624{
4625 static cpumask_t cpumask;
4626 struct worker *worker;
4627 int wi;
4628
4629 lockdep_assert_held(&pool->manager_mutex);
4630
4631 /* is @cpu allowed for @pool? */
4632 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4633 return;
4634
4635 /* is @cpu the only online CPU? */
4636 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4637 if (cpumask_weight(&cpumask) != 1)
4638 return;
4639
4640 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4641 for_each_pool_worker(worker, wi, pool)
4642 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4643 pool->attrs->cpumask) < 0);
4644}
4645
Tejun Heo8db25e72012-07-17 12:39:28 -07004646/*
4647 * Workqueues should be brought up before normal priority CPU notifiers.
4648 * This will be registered high priority CPU notifier.
4649 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004650static int workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004651 unsigned long action,
4652 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004653{
Tejun Heod84ff052013-03-12 11:29:59 -07004654 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004655 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004656 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004657 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Tejun Heo8db25e72012-07-17 12:39:28 -07004659 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004661 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004662 if (pool->nr_workers)
4663 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004664 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004665 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004667 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004668
Tejun Heo65758202012-07-17 12:39:26 -07004669 case CPU_DOWN_FAILED:
4670 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004671 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004672
4673 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004674 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004675
Tejun Heo7dbc7252013-03-19 13:45:21 -07004676 if (pool->cpu == cpu) {
4677 spin_lock_irq(&pool->lock);
4678 pool->flags &= ~POOL_DISASSOCIATED;
4679 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004680
Tejun Heo7dbc7252013-03-19 13:45:21 -07004681 rebind_workers(pool);
4682 } else if (pool->cpu < 0) {
4683 restore_unbound_workers_cpumask(pool, cpu);
4684 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004685
Tejun Heobc3a1af2013-03-13 19:47:39 -07004686 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004687 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004688
Tejun Heo4c16bd32013-04-01 11:23:36 -07004689 /* update NUMA affinity of unbound workqueues */
4690 list_for_each_entry(wq, &workqueues, list)
4691 wq_update_unbound_numa(wq, cpu, true);
4692
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004693 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004694 break;
Tejun Heo65758202012-07-17 12:39:26 -07004695 }
4696 return NOTIFY_OK;
4697}
4698
4699/*
4700 * Workqueues should be brought down after normal priority CPU notifiers.
4701 * This will be registered as low priority CPU notifier.
4702 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004703static int workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004704 unsigned long action,
4705 void *hcpu)
4706{
Tejun Heod84ff052013-03-12 11:29:59 -07004707 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004708 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004709 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004710
Tejun Heo65758202012-07-17 12:39:26 -07004711 switch (action & ~CPU_TASKS_FROZEN) {
4712 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004713 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004714 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004715 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004716
4717 /* update NUMA affinity of unbound workqueues */
4718 mutex_lock(&wq_pool_mutex);
4719 list_for_each_entry(wq, &workqueues, list)
4720 wq_update_unbound_numa(wq, cpu, false);
4721 mutex_unlock(&wq_pool_mutex);
4722
4723 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004724 flush_work(&unbind_work);
4725 break;
Tejun Heo65758202012-07-17 12:39:26 -07004726 }
4727 return NOTIFY_OK;
4728}
4729
Rusty Russell2d3854a2008-11-05 13:39:10 +11004730#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004731
Rusty Russell2d3854a2008-11-05 13:39:10 +11004732struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004733 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004734 long (*fn)(void *);
4735 void *arg;
4736 long ret;
4737};
4738
Tejun Heoed48ece2012-09-18 12:48:43 -07004739static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004740{
Tejun Heoed48ece2012-09-18 12:48:43 -07004741 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4742
Rusty Russell2d3854a2008-11-05 13:39:10 +11004743 wfc->ret = wfc->fn(wfc->arg);
4744}
4745
4746/**
4747 * work_on_cpu - run a function in user context on a particular cpu
4748 * @cpu: the cpu to run on
4749 * @fn: the function to run
4750 * @arg: the function arg
4751 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004752 * This will return the value @fn returns.
4753 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06004754 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004755 */
Tejun Heod84ff052013-03-12 11:29:59 -07004756long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004757{
Tejun Heoed48ece2012-09-18 12:48:43 -07004758 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004759
Tejun Heoed48ece2012-09-18 12:48:43 -07004760 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4761 schedule_work_on(cpu, &wfc.work);
4762 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004763 return wfc.ret;
4764}
4765EXPORT_SYMBOL_GPL(work_on_cpu);
4766#endif /* CONFIG_SMP */
4767
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004768#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304769
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004770/**
4771 * freeze_workqueues_begin - begin freezing workqueues
4772 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004773 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004774 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004775 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004776 *
4777 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004778 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004779 */
4780void freeze_workqueues_begin(void)
4781{
Tejun Heo17116962013-03-12 11:29:58 -07004782 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004783 struct workqueue_struct *wq;
4784 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004785 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004786
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004787 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004788
Tejun Heo6183c002013-03-12 11:29:57 -07004789 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004790 workqueue_freezing = true;
4791
Tejun Heo24b8a842013-03-12 11:29:58 -07004792 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004793 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004794 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004795 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4796 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004797 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004798 }
4799
Tejun Heo24b8a842013-03-12 11:29:58 -07004800 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004801 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004802 for_each_pwq(pwq, wq)
4803 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004804 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004805 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004806
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004807 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004809
4810/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004811 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004812 *
4813 * Check whether freezing is complete. This function must be called
4814 * between freeze_workqueues_begin() and thaw_workqueues().
4815 *
4816 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004817 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004818 *
4819 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004820 * %true if some freezable workqueues are still busy. %false if freezing
4821 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004822 */
4823bool freeze_workqueues_busy(void)
4824{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004825 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004826 struct workqueue_struct *wq;
4827 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004828
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004829 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004830
Tejun Heo6183c002013-03-12 11:29:57 -07004831 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004832
Tejun Heo24b8a842013-03-12 11:29:58 -07004833 list_for_each_entry(wq, &workqueues, list) {
4834 if (!(wq->flags & WQ_FREEZABLE))
4835 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004836 /*
4837 * nr_active is monotonically decreasing. It's safe
4838 * to peek without lock.
4839 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004840 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004841 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004842 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004843 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004844 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004845 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004846 goto out_unlock;
4847 }
4848 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004849 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004850 }
4851out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004852 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004853 return busy;
4854}
4855
4856/**
4857 * thaw_workqueues - thaw workqueues
4858 *
4859 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004860 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004861 *
4862 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004863 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004864 */
4865void thaw_workqueues(void)
4866{
Tejun Heo24b8a842013-03-12 11:29:58 -07004867 struct workqueue_struct *wq;
4868 struct pool_workqueue *pwq;
4869 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004870 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004871
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004872 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004873
4874 if (!workqueue_freezing)
4875 goto out_unlock;
4876
Tejun Heo24b8a842013-03-12 11:29:58 -07004877 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004878 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004879 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004880 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4881 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004882 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004883 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004884
Tejun Heo24b8a842013-03-12 11:29:58 -07004885 /* restore max_active and repopulate worklist */
4886 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004887 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004888 for_each_pwq(pwq, wq)
4889 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004890 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004891 }
4892
4893 workqueue_freezing = false;
4894out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004895 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004896}
4897#endif /* CONFIG_FREEZER */
4898
Tejun Heobce90382013-04-01 11:23:32 -07004899static void __init wq_numa_init(void)
4900{
4901 cpumask_var_t *tbl;
4902 int node, cpu;
4903
4904 /* determine NUMA pwq table len - highest node id + 1 */
4905 for_each_node(node)
4906 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4907
4908 if (num_possible_nodes() <= 1)
4909 return;
4910
Tejun Heod55262c2013-04-01 11:23:38 -07004911 if (wq_disable_numa) {
4912 pr_info("workqueue: NUMA affinity support disabled\n");
4913 return;
4914 }
4915
Tejun Heo4c16bd32013-04-01 11:23:36 -07004916 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4917 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4918
Tejun Heobce90382013-04-01 11:23:32 -07004919 /*
4920 * We want masks of possible CPUs of each node which isn't readily
4921 * available. Build one from cpu_to_node() which should have been
4922 * fully initialized by now.
4923 */
4924 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4925 BUG_ON(!tbl);
4926
4927 for_each_node(node)
Tejun Heo1be0c252013-05-15 14:24:24 -07004928 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
4929 node_online(node) ? node : NUMA_NO_NODE));
Tejun Heobce90382013-04-01 11:23:32 -07004930
4931 for_each_possible_cpu(cpu) {
4932 node = cpu_to_node(cpu);
4933 if (WARN_ON(node == NUMA_NO_NODE)) {
4934 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4935 /* happens iff arch is bonkers, let's just proceed */
4936 return;
4937 }
4938 cpumask_set_cpu(cpu, tbl[node]);
4939 }
4940
4941 wq_numa_possible_cpumask = tbl;
4942 wq_numa_enabled = true;
4943}
4944
Suresh Siddha6ee05782010-07-30 14:57:37 -07004945static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004947 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4948 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004949
Tejun Heo7c3eed52013-01-24 11:01:33 -08004950 /* make sure we have enough bits for OFFQ pool ID */
4951 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004952 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004953
Tejun Heoe904e6c2013-03-12 11:29:57 -07004954 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4955
4956 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4957
Tejun Heo65758202012-07-17 12:39:26 -07004958 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004959 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004960
Tejun Heobce90382013-04-01 11:23:32 -07004961 wq_numa_init();
4962
Tejun Heo706026c2013-01-24 11:01:34 -08004963 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004964 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004965 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004966
Tejun Heo7a4e3442013-03-12 11:30:00 -07004967 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004968 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004969 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004970 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004971 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004972 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004973 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004974
Tejun Heo9daf9e62013-01-24 11:01:33 -08004975 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004976 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004977 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004978 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004979 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004980 }
4981
Tejun Heoe22bee72010-06-29 10:07:14 +02004982 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004983 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004984 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004985
Tejun Heof02ae732013-03-12 11:30:03 -07004986 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004987 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004988 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004989 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004990 }
4991
Tejun Heo29c91e92013-03-12 11:30:03 -07004992 /* create default unbound wq attrs */
4993 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4994 struct workqueue_attrs *attrs;
4995
4996 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004997 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004998 unbound_std_wq_attrs[i] = attrs;
4999 }
5000
Tejun Heod320c032010-06-29 10:07:14 +02005001 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005002 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02005003 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02005004 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
5005 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01005006 system_freezable_wq = alloc_workqueue("events_freezable",
5007 WQ_FREEZABLE, 0);
Viresh Kumar06681062013-04-24 17:12:54 +05305008 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
5009 WQ_POWER_EFFICIENT, 0);
5010 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
5011 WQ_FREEZABLE | WQ_POWER_EFFICIENT,
5012 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09005013 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Viresh Kumar06681062013-04-24 17:12:54 +05305014 !system_unbound_wq || !system_freezable_wq ||
5015 !system_power_efficient_wq ||
5016 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07005017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018}
Suresh Siddha6ee05782010-07-30 14:57:37 -07005019early_initcall(init_workqueues);