blob: 57cd77de4a4fdd8ead040bffab82f2e5614ecbb8 [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 Heoe22bee72010-06-29 10:07:14 +020049
Tejun Heoea138442013-01-18 14:05:55 -080050#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Tejun Heoc8e55f32010-06-29 10:07:12 +020052enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 /*
Tejun Heo24647572013-01-24 11:01:33 -080054 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 *
Tejun Heo24647572013-01-24 11:01:33 -080056 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070057 * While associated (!DISASSOCIATED), all workers are bound to the
58 * CPU and none has %WORKER_UNBOUND set and concurrency management
59 * is in effect.
60 *
61 * While DISASSOCIATED, the cpu may be offline and all workers have
62 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080063 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070065 * Note that DISASSOCIATED should be flipped only while holding
66 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080067 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070068 */
Tejun Heo11ebea52012-07-12 14:46:37 -070069 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080070 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080071 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020072
Tejun Heoc8e55f32010-06-29 10:07:12 +020073 /* worker flags */
74 WORKER_STARTED = 1 << 0, /* started */
75 WORKER_DIE = 1 << 1, /* die die die */
76 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020077 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020078 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020079 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070080 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020081
Tejun Heoa9ab7752013-03-19 13:45:21 -070082 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
83 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe34cdddb2013-01-24 11:01:33 -080085 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070086
Tejun Heo29c91e92013-03-12 11:30:03 -070087 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020088 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020089
Tejun Heoe22bee72010-06-29 10:07:14 +020090 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
91 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
92
Tejun Heo3233cdb2011-02-16 18:10:19 +010093 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
94 /* call for help after 10ms
95 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020096 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
97 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020098
99 /*
100 * Rescue workers are used only on emergencies and shared by
101 * all cpus. Give -20.
102 */
103 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700104 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoecf68812013-04-01 11:23:34 -0700105
106 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200107};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 * Structure fields follow one of the following exclusion rules.
111 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200112 * I: Modifiable by initialization/destruction paths and read-only for
113 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200115 * P: Preemption protected. Disabling preemption is enough and should
116 * only be modified and accessed from the local cpu.
117 *
Tejun Heod565ed62013-01-24 11:01:33 -0800118 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200119 *
Tejun Heod565ed62013-01-24 11:01:33 -0800120 * X: During normal operation, modification requires pool->lock and should
121 * be done only from local cpu. Either disabling preemption on local
122 * cpu or grabbing pool->lock is enough for read access. If
123 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200124 *
Tejun Heo822d8402013-03-19 13:45:21 -0700125 * MG: pool->manager_mutex and pool->lock protected. Writes require both
126 * locks. Reads can happen under either lock.
127 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700128 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700129 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700130 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700131 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700132 * WQ: wq->mutex protected.
133 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700134 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700135 *
136 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200137 */
138
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800139/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200140
Tejun Heobd7bdd42012-07-12 14:46:37 -0700141struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800142 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700143 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700144 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800145 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700146 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700147
148 struct list_head worklist; /* L: list of pending works */
149 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700150
151 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700152 int nr_idle; /* L: currently idle ones */
153
154 struct list_head idle_list; /* X: list of idle workers */
155 struct timer_list idle_timer; /* L: worker idle timeout */
156 struct timer_list mayday_timer; /* L: SOS timer for workers */
157
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700158 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800159 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
160 /* L: hash of busy workers */
161
Tejun Heobc3a1af2013-03-13 19:47:39 -0700162 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700163 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700164 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700165 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800166
Tejun Heo7a4e3442013-03-12 11:30:00 -0700167 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700168 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
169 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700170
Tejun Heoe19e3972013-01-24 11:39:44 -0800171 /*
172 * The current concurrency level. As it's likely to be accessed
173 * from other CPUs during try_to_wake_up(), put it in a separate
174 * cacheline.
175 */
176 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700177
178 /*
179 * Destruction of pool is sched-RCU protected to allow dereferences
180 * from get_work_pool().
181 */
182 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200183} ____cacheline_aligned_in_smp;
184
185/*
Tejun Heo112202d2013-02-13 19:29:12 -0800186 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
187 * of work_struct->data are used for flags and the remaining high bits
188 * point to the pwq; thus, pwqs need to be aligned at two's power of the
189 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Tejun Heo112202d2013-02-13 19:29:12 -0800191struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700192 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200193 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int work_color; /* L: current color */
195 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700196 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200197 int nr_in_flight[WORK_NR_COLORS];
198 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200199 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200200 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200201 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700202 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700203 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700204
205 /*
206 * Release of unbound pwq is punted to system_wq. See put_pwq()
207 * and pwq_unbound_release_workfn() for details. pool_workqueue
208 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700209 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700210 */
211 struct work_struct unbound_release_work;
212 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700213} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200216 * Structure used to wait for workqueue flush.
217 */
218struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700219 struct list_head list; /* WQ: list of flushers */
220 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200221 struct completion done; /* flush completion */
222};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo226223a2013-03-12 11:30:05 -0700224struct wq_device;
225
Tejun Heo73f53c42010-06-29 10:07:11 +0200226/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700227 * The externally visible workqueue. It relays the issued work items to
228 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
230struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700231 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700232 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200233
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700234 struct mutex mutex; /* protects this wq */
235 int work_color; /* WQ: current work color */
236 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800237 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700238 struct wq_flusher *first_flusher; /* WQ: first flusher */
239 struct list_head flusher_queue; /* WQ: flush waiters */
240 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200241
Tejun Heo2e109a22013-03-13 19:47:40 -0700242 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200243 struct worker *rescuer; /* I: rescue worker */
244
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700245 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700246 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700247
Tejun Heo6029a912013-04-01 11:23:34 -0700248 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
Tejun Heo4c16bd32013-04-01 11:23:36 -0700249 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
Tejun Heo6029a912013-04-01 11:23:34 -0700250
Tejun Heo226223a2013-03-12 11:30:05 -0700251#ifdef CONFIG_SYSFS
252 struct wq_device *wq_dev; /* I: for sysfs interface */
253#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700254#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200255 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700256#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700257 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700258
259 /* hot fields used during command issue, aligned to cacheline */
260 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
261 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700262 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
Tejun Heoe904e6c2013-03-12 11:29:57 -0700265static struct kmem_cache *pwq_cache;
266
Tejun Heobce90382013-04-01 11:23:32 -0700267static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
268static cpumask_var_t *wq_numa_possible_cpumask;
269 /* possible CPUs of each node */
270
271static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
272
Tejun Heo4c16bd32013-04-01 11:23:36 -0700273/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
274static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
275
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700276static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700277static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700278
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700279static LIST_HEAD(workqueues); /* PL: list of all workqueues */
280static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700281
282/* the per-cpu worker pools */
283static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
284 cpu_worker_pools);
285
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700286static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700287
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700288/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700289static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
290
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700291/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700292static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
293
Tejun Heod320c032010-06-29 10:07:14 +0200294struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200295EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300296struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900297EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300298struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200299EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300300struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200301EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300302struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100303EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200304
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700305static int worker_thread(void *__worker);
306static void copy_workqueue_attrs(struct workqueue_attrs *to,
307 const struct workqueue_attrs *from);
308
Tejun Heo97bd2342010-10-05 10:41:14 +0200309#define CREATE_TRACE_POINTS
310#include <trace/events/workqueue.h>
311
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700312#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700313 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700314 lockdep_is_held(&wq_pool_mutex), \
315 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700316
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700317#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700318 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700319 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700320 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700321
Tejun Heo822d8402013-03-19 13:45:21 -0700322#ifdef CONFIG_LOCKDEP
323#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800324 WARN_ONCE(debug_locks && \
325 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700326 !lockdep_is_held(&(pool)->lock), \
327 "pool->manager_mutex or ->lock should be held")
328#else
329#define assert_manager_or_pool_lock(pool) do { } while (0)
330#endif
331
Tejun Heof02ae732013-03-12 11:30:03 -0700332#define for_each_cpu_worker_pool(pool, cpu) \
333 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
334 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700335 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700336
Tejun Heo49e3cf42013-03-12 11:29:58 -0700337/**
Tejun Heo17116962013-03-12 11:29:58 -0700338 * for_each_pool - iterate through all worker_pools in the system
339 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700340 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700341 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700342 * This must be called either with wq_pool_mutex held or sched RCU read
343 * locked. If the pool needs to be used beyond the locking in effect, the
344 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700345 *
346 * The if/else clause exists only for the lockdep assertion and can be
347 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700348 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700349#define for_each_pool(pool, pi) \
350 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700351 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700352 else
Tejun Heo17116962013-03-12 11:29:58 -0700353
354/**
Tejun Heo822d8402013-03-19 13:45:21 -0700355 * for_each_pool_worker - iterate through all workers of a worker_pool
356 * @worker: iteration cursor
357 * @wi: integer used for iteration
358 * @pool: worker_pool to iterate workers of
359 *
360 * This must be called with either @pool->manager_mutex or ->lock held.
361 *
362 * The if/else clause exists only for the lockdep assertion and can be
363 * ignored.
364 */
365#define for_each_pool_worker(worker, wi, pool) \
366 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
367 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
368 else
369
370/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700371 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
372 * @pwq: iteration cursor
373 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700374 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700375 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700376 * If the pwq needs to be used beyond the locking in effect, the caller is
377 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700378 *
379 * The if/else clause exists only for the lockdep assertion and can be
380 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700381 */
382#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700383 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700384 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700385 else
Tejun Heof3421792010-07-02 10:03:51 +0200386
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900387#ifdef CONFIG_DEBUG_OBJECTS_WORK
388
389static struct debug_obj_descr work_debug_descr;
390
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100391static void *work_debug_hint(void *addr)
392{
393 return ((struct work_struct *) addr)->func;
394}
395
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900396/*
397 * fixup_init is called when:
398 * - an active object is initialized
399 */
400static int work_fixup_init(void *addr, enum debug_obj_state state)
401{
402 struct work_struct *work = addr;
403
404 switch (state) {
405 case ODEBUG_STATE_ACTIVE:
406 cancel_work_sync(work);
407 debug_object_init(work, &work_debug_descr);
408 return 1;
409 default:
410 return 0;
411 }
412}
413
414/*
415 * fixup_activate is called when:
416 * - an active object is activated
417 * - an unknown object is activated (might be a statically initialized object)
418 */
419static int work_fixup_activate(void *addr, enum debug_obj_state state)
420{
421 struct work_struct *work = addr;
422
423 switch (state) {
424
425 case ODEBUG_STATE_NOTAVAILABLE:
426 /*
427 * This is not really a fixup. The work struct was
428 * statically initialized. We just make sure that it
429 * is tracked in the object tracker.
430 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200431 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900432 debug_object_init(work, &work_debug_descr);
433 debug_object_activate(work, &work_debug_descr);
434 return 0;
435 }
436 WARN_ON_ONCE(1);
437 return 0;
438
439 case ODEBUG_STATE_ACTIVE:
440 WARN_ON(1);
441
442 default:
443 return 0;
444 }
445}
446
447/*
448 * fixup_free is called when:
449 * - an active object is freed
450 */
451static int work_fixup_free(void *addr, enum debug_obj_state state)
452{
453 struct work_struct *work = addr;
454
455 switch (state) {
456 case ODEBUG_STATE_ACTIVE:
457 cancel_work_sync(work);
458 debug_object_free(work, &work_debug_descr);
459 return 1;
460 default:
461 return 0;
462 }
463}
464
465static struct debug_obj_descr work_debug_descr = {
466 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100467 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900468 .fixup_init = work_fixup_init,
469 .fixup_activate = work_fixup_activate,
470 .fixup_free = work_fixup_free,
471};
472
473static inline void debug_work_activate(struct work_struct *work)
474{
475 debug_object_activate(work, &work_debug_descr);
476}
477
478static inline void debug_work_deactivate(struct work_struct *work)
479{
480 debug_object_deactivate(work, &work_debug_descr);
481}
482
483void __init_work(struct work_struct *work, int onstack)
484{
485 if (onstack)
486 debug_object_init_on_stack(work, &work_debug_descr);
487 else
488 debug_object_init(work, &work_debug_descr);
489}
490EXPORT_SYMBOL_GPL(__init_work);
491
492void destroy_work_on_stack(struct work_struct *work)
493{
494 debug_object_free(work, &work_debug_descr);
495}
496EXPORT_SYMBOL_GPL(destroy_work_on_stack);
497
498#else
499static inline void debug_work_activate(struct work_struct *work) { }
500static inline void debug_work_deactivate(struct work_struct *work) { }
501#endif
502
Tejun Heo9daf9e62013-01-24 11:01:33 -0800503/* allocate ID and assign it to @pool */
504static int worker_pool_assign_id(struct worker_pool *pool)
505{
506 int ret;
507
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700508 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700509
Tejun Heofa1b54e2013-03-12 11:30:00 -0700510 do {
511 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
512 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700513 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700514 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800515
516 return ret;
517}
518
Tejun Heo76af4d92013-03-12 11:30:00 -0700519/**
520 * first_pwq - return the first pool_workqueue of the specified workqueue
521 * @wq: the target workqueue
522 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700523 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700524 * If the pwq needs to be used beyond the locking in effect, the caller is
525 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700526 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700527static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700528{
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700529 assert_rcu_or_wq_mutex(wq);
Tejun Heo76af4d92013-03-12 11:30:00 -0700530 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
531 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700532}
533
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700534/**
535 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
536 * @wq: the target workqueue
537 * @node: the node ID
538 *
539 * This must be called either with pwq_lock held or sched RCU read locked.
540 * If the pwq needs to be used beyond the locking in effect, the caller is
541 * responsible for guaranteeing that the pwq stays online.
542 */
543static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
544 int node)
545{
546 assert_rcu_or_wq_mutex(wq);
547 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
548}
549
Tejun Heo73f53c42010-06-29 10:07:11 +0200550static unsigned int work_color_to_flags(int color)
551{
552 return color << WORK_STRUCT_COLOR_SHIFT;
553}
554
555static int get_work_color(struct work_struct *work)
556{
557 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
558 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
559}
560
561static int work_next_color(int color)
562{
563 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700564}
565
David Howells4594bf12006-12-07 11:33:26 +0000566/*
Tejun Heo112202d2013-02-13 19:29:12 -0800567 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
568 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800569 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570 *
Tejun Heo112202d2013-02-13 19:29:12 -0800571 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
572 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700573 * work->data. These functions should only be called while the work is
574 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200575 *
Tejun Heo112202d2013-02-13 19:29:12 -0800576 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800577 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800578 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800579 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700580 *
581 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
582 * canceled. While being canceled, a work item may have its PENDING set
583 * but stay off timer and worklist for arbitrarily long and nobody should
584 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000585 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586static inline void set_work_data(struct work_struct *work, unsigned long data,
587 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000588{
Tejun Heo6183c002013-03-12 11:29:57 -0700589 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000591}
David Howells365970a2006-11-22 14:54:49 +0000592
Tejun Heo112202d2013-02-13 19:29:12 -0800593static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200595{
Tejun Heo112202d2013-02-13 19:29:12 -0800596 set_work_data(work, (unsigned long)pwq,
597 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200598}
599
Lai Jiangshan4468a002013-02-06 18:04:53 -0800600static void set_work_pool_and_keep_pending(struct work_struct *work,
601 int pool_id)
602{
603 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
604 WORK_STRUCT_PENDING);
605}
606
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607static void set_work_pool_and_clear_pending(struct work_struct *work,
608 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000609{
Tejun Heo23657bb2012-08-13 17:08:19 -0700610 /*
611 * The following wmb is paired with the implied mb in
612 * test_and_set_bit(PENDING) and ensures all updates to @work made
613 * here are visible to and precede any updates by the next PENDING
614 * owner.
615 */
616 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800617 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200618}
619
620static void clear_work_data(struct work_struct *work)
621{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800622 smp_wmb(); /* see set_work_pool_and_clear_pending() */
623 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200624}
625
Tejun Heo112202d2013-02-13 19:29:12 -0800626static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200627{
Tejun Heoe1201532010-07-22 14:14:25 +0200628 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200629
Tejun Heo112202d2013-02-13 19:29:12 -0800630 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200631 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
632 else
633 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200634}
635
Tejun Heo7c3eed52013-01-24 11:01:33 -0800636/**
637 * get_work_pool - return the worker_pool a given work was associated with
638 * @work: the work item of interest
639 *
640 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700641 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700642 * Pools are created and destroyed under wq_pool_mutex, and allows read
643 * access under sched-RCU read lock. As such, this function should be
644 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700645 *
646 * All fields of the returned pool are accessible as long as the above
647 * mentioned locking is in effect. If the returned pool needs to be used
648 * beyond the critical section, the caller is responsible for ensuring the
649 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800650 */
651static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200652{
Tejun Heoe1201532010-07-22 14:14:25 +0200653 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800654 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200655
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700656 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700657
Tejun Heo112202d2013-02-13 19:29:12 -0800658 if (data & WORK_STRUCT_PWQ)
659 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800660 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200661
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
663 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200664 return NULL;
665
Tejun Heofa1b54e2013-03-12 11:30:00 -0700666 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800667}
668
669/**
670 * get_work_pool_id - return the worker pool ID a given work is associated with
671 * @work: the work item of interest
672 *
673 * Return the worker_pool ID @work was last associated with.
674 * %WORK_OFFQ_POOL_NONE if none.
675 */
676static int get_work_pool_id(struct work_struct *work)
677{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800678 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800679
Tejun Heo112202d2013-02-13 19:29:12 -0800680 if (data & WORK_STRUCT_PWQ)
681 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800682 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
683
684 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800685}
686
Tejun Heobbb68df2012-08-03 10:30:46 -0700687static void mark_work_canceling(struct work_struct *work)
688{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800689 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700690
Tejun Heo7c3eed52013-01-24 11:01:33 -0800691 pool_id <<= WORK_OFFQ_POOL_SHIFT;
692 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700693}
694
695static bool work_is_canceling(struct work_struct *work)
696{
697 unsigned long data = atomic_long_read(&work->data);
698
Tejun Heo112202d2013-02-13 19:29:12 -0800699 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700700}
701
David Howells365970a2006-11-22 14:54:49 +0000702/*
Tejun Heo32704762012-07-13 22:16:45 -0700703 * Policy functions. These define the policies on how the global worker
704 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800705 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000706 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200707
Tejun Heo63d95a92012-07-12 14:46:37 -0700708static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000709{
Tejun Heoe19e3972013-01-24 11:39:44 -0800710 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000711}
712
Tejun Heoe22bee72010-06-29 10:07:14 +0200713/*
714 * Need to wake up a worker? Called from anything but currently
715 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700716 *
717 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800718 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700719 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200720 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700721static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000722{
Tejun Heo63d95a92012-07-12 14:46:37 -0700723 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000724}
725
Tejun Heoe22bee72010-06-29 10:07:14 +0200726/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700727static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200728{
Tejun Heo63d95a92012-07-12 14:46:37 -0700729 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200730}
731
732/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700733static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200734{
Tejun Heoe19e3972013-01-24 11:39:44 -0800735 return !list_empty(&pool->worklist) &&
736 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200737}
738
739/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700740static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200741{
Tejun Heo63d95a92012-07-12 14:46:37 -0700742 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200743}
744
745/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700746static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200747{
Tejun Heo63d95a92012-07-12 14:46:37 -0700748 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700749 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200750}
751
752/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700753static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200754{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700755 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700756 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
757 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200758
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700759 /*
760 * nr_idle and idle_list may disagree if idle rebinding is in
761 * progress. Never return %true if idle_list is empty.
762 */
763 if (list_empty(&pool->idle_list))
764 return false;
765
Tejun Heoe22bee72010-06-29 10:07:14 +0200766 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
767}
768
769/*
770 * Wake up functions.
771 */
772
Tejun Heo7e116292010-06-29 10:07:13 +0200773/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700774static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200775{
Tejun Heo63d95a92012-07-12 14:46:37 -0700776 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200777 return NULL;
778
Tejun Heo63d95a92012-07-12 14:46:37 -0700779 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200780}
781
782/**
783 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700784 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200785 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700786 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200787 *
788 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800789 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200790 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700791static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200792{
Tejun Heo63d95a92012-07-12 14:46:37 -0700793 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200794
795 if (likely(worker))
796 wake_up_process(worker->task);
797}
798
Tejun Heo4690c4a2010-06-29 10:07:10 +0200799/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200800 * wq_worker_waking_up - a worker is waking up
801 * @task: task waking up
802 * @cpu: CPU @task is waking up to
803 *
804 * This function is called during try_to_wake_up() when a worker is
805 * being awoken.
806 *
807 * CONTEXT:
808 * spin_lock_irq(rq->lock)
809 */
Tejun Heod84ff052013-03-12 11:29:59 -0700810void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200811{
812 struct worker *worker = kthread_data(task);
813
Joonsoo Kim36576002012-10-26 23:03:49 +0900814 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800815 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800816 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900817 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200818}
819
820/**
821 * wq_worker_sleeping - a worker is going to sleep
822 * @task: task going to sleep
823 * @cpu: CPU in question, must be the current CPU number
824 *
825 * This function is called during schedule() when a busy worker is
826 * going to sleep. Worker on the same cpu can be woken up by
827 * returning pointer to its task.
828 *
829 * CONTEXT:
830 * spin_lock_irq(rq->lock)
831 *
832 * RETURNS:
833 * Worker task on @cpu to wake up, %NULL if none.
834 */
Tejun Heod84ff052013-03-12 11:29:59 -0700835struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200836{
837 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800838 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200839
Tejun Heo111c2252013-01-17 17:16:24 -0800840 /*
841 * Rescuers, which may not have all the fields set up like normal
842 * workers, also reach here, let's not access anything before
843 * checking NOT_RUNNING.
844 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500845 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200846 return NULL;
847
Tejun Heo111c2252013-01-17 17:16:24 -0800848 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800849
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700851 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
852 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200853
854 /*
855 * The counterpart of the following dec_and_test, implied mb,
856 * worklist not empty test sequence is in insert_work().
857 * Please read comment there.
858 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700859 * NOT_RUNNING is clear. This means that we're bound to and
860 * running on the local cpu w/ rq lock held and preemption
861 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800862 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700863 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200864 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800865 if (atomic_dec_and_test(&pool->nr_running) &&
866 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700867 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200868 return to_wakeup ? to_wakeup->task : NULL;
869}
870
871/**
872 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200873 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200874 * @flags: flags to set
875 * @wakeup: wakeup an idle worker if necessary
876 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200877 * Set @flags in @worker->flags and adjust nr_running accordingly. If
878 * nr_running becomes zero and @wakeup is %true, an idle worker is
879 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200880 *
Tejun Heocb444762010-07-02 10:03:50 +0200881 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800882 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200883 */
884static inline void worker_set_flags(struct worker *worker, unsigned int flags,
885 bool wakeup)
886{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700887 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200888
Tejun Heocb444762010-07-02 10:03:50 +0200889 WARN_ON_ONCE(worker->task != current);
890
Tejun Heoe22bee72010-06-29 10:07:14 +0200891 /*
892 * If transitioning into NOT_RUNNING, adjust nr_running and
893 * wake up an idle worker as necessary if requested by
894 * @wakeup.
895 */
896 if ((flags & WORKER_NOT_RUNNING) &&
897 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200898 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800899 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700900 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700901 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200902 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800903 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200904 }
905
Tejun Heod302f012010-06-29 10:07:13 +0200906 worker->flags |= flags;
907}
908
909/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200910 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200911 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200912 * @flags: flags to clear
913 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200914 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200915 *
Tejun Heocb444762010-07-02 10:03:50 +0200916 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800917 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200918 */
919static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
920{
Tejun Heo63d95a92012-07-12 14:46:37 -0700921 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200922 unsigned int oflags = worker->flags;
923
Tejun Heocb444762010-07-02 10:03:50 +0200924 WARN_ON_ONCE(worker->task != current);
925
Tejun Heod302f012010-06-29 10:07:13 +0200926 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200927
Tejun Heo42c025f2011-01-11 15:58:49 +0100928 /*
929 * If transitioning out of NOT_RUNNING, increment nr_running. Note
930 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
931 * of multiple flags, not a single flag.
932 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200933 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
934 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800935 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200936}
937
938/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200939 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800940 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200941 * @work: work to find worker for
942 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800943 * Find a worker which is executing @work on @pool by searching
944 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800945 * to match, its current execution should match the address of @work and
946 * its work function. This is to avoid unwanted dependency between
947 * unrelated work executions through a work item being recycled while still
948 * being executed.
949 *
950 * This is a bit tricky. A work item may be freed once its execution
951 * starts and nothing prevents the freed area from being recycled for
952 * another work item. If the same work item address ends up being reused
953 * before the original execution finishes, workqueue will identify the
954 * recycled work item as currently executing and make it wait until the
955 * current execution finishes, introducing an unwanted dependency.
956 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700957 * This function checks the work item address and work function to avoid
958 * false positives. Note that this isn't complete as one may construct a
959 * work function which can introduce dependency onto itself through a
960 * recycled work item. Well, if somebody wants to shoot oneself in the
961 * foot that badly, there's only so much we can do, and if such deadlock
962 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200963 *
964 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800965 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200966 *
967 * RETURNS:
968 * Pointer to worker which is executing @work if found, NULL
969 * otherwise.
970 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800971static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200972 struct work_struct *work)
973{
Sasha Levin42f85702012-12-17 10:01:23 -0500974 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500975
Sasha Levinb67bfe02013-02-27 17:06:00 -0800976 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800977 (unsigned long)work)
978 if (worker->current_work == work &&
979 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500980 return worker;
981
982 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200983}
984
985/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700986 * move_linked_works - move linked works to a list
987 * @work: start of series of works to be scheduled
988 * @head: target list to append @work to
989 * @nextp: out paramter for nested worklist walking
990 *
991 * Schedule linked works starting from @work to @head. Work series to
992 * be scheduled starts at @work and includes any consecutive work with
993 * WORK_STRUCT_LINKED set in its predecessor.
994 *
995 * If @nextp is not NULL, it's updated to point to the next work of
996 * the last scheduled work. This allows move_linked_works() to be
997 * nested inside outer list_for_each_entry_safe().
998 *
999 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001000 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001001 */
1002static void move_linked_works(struct work_struct *work, struct list_head *head,
1003 struct work_struct **nextp)
1004{
1005 struct work_struct *n;
1006
1007 /*
1008 * Linked worklist will always end before the end of the list,
1009 * use NULL for list head.
1010 */
1011 list_for_each_entry_safe_from(work, n, NULL, entry) {
1012 list_move_tail(&work->entry, head);
1013 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1014 break;
1015 }
1016
1017 /*
1018 * If we're already inside safe list traversal and have moved
1019 * multiple works to the scheduled queue, the next position
1020 * needs to be updated.
1021 */
1022 if (nextp)
1023 *nextp = n;
1024}
1025
Tejun Heo8864b4e2013-03-12 11:30:04 -07001026/**
1027 * get_pwq - get an extra reference on the specified pool_workqueue
1028 * @pwq: pool_workqueue to get
1029 *
1030 * Obtain an extra reference on @pwq. The caller should guarantee that
1031 * @pwq has positive refcnt and be holding the matching pool->lock.
1032 */
1033static void get_pwq(struct pool_workqueue *pwq)
1034{
1035 lockdep_assert_held(&pwq->pool->lock);
1036 WARN_ON_ONCE(pwq->refcnt <= 0);
1037 pwq->refcnt++;
1038}
1039
1040/**
1041 * put_pwq - put a pool_workqueue reference
1042 * @pwq: pool_workqueue to put
1043 *
1044 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1045 * destruction. The caller should be holding the matching pool->lock.
1046 */
1047static void put_pwq(struct pool_workqueue *pwq)
1048{
1049 lockdep_assert_held(&pwq->pool->lock);
1050 if (likely(--pwq->refcnt))
1051 return;
1052 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1053 return;
1054 /*
1055 * @pwq can't be released under pool->lock, bounce to
1056 * pwq_unbound_release_workfn(). This never recurses on the same
1057 * pool->lock as this path is taken only for unbound workqueues and
1058 * the release work item is scheduled on a per-cpu workqueue. To
1059 * avoid lockdep warning, unbound pool->locks are given lockdep
1060 * subclass of 1 in get_unbound_pool().
1061 */
1062 schedule_work(&pwq->unbound_release_work);
1063}
1064
Tejun Heodce90d42013-04-01 11:23:35 -07001065/**
1066 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1067 * @pwq: pool_workqueue to put (can be %NULL)
1068 *
1069 * put_pwq() with locking. This function also allows %NULL @pwq.
1070 */
1071static void put_pwq_unlocked(struct pool_workqueue *pwq)
1072{
1073 if (pwq) {
1074 /*
1075 * As both pwqs and pools are sched-RCU protected, the
1076 * following lock operations are safe.
1077 */
1078 spin_lock_irq(&pwq->pool->lock);
1079 put_pwq(pwq);
1080 spin_unlock_irq(&pwq->pool->lock);
1081 }
1082}
1083
Tejun Heo112202d2013-02-13 19:29:12 -08001084static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001085{
Tejun Heo112202d2013-02-13 19:29:12 -08001086 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001087
1088 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001089 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001090 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001091 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001092}
1093
Tejun Heo112202d2013-02-13 19:29:12 -08001094static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001095{
Tejun Heo112202d2013-02-13 19:29:12 -08001096 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001097 struct work_struct, entry);
1098
Tejun Heo112202d2013-02-13 19:29:12 -08001099 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001100}
1101
Tejun Heobf4ede02012-08-03 10:30:46 -07001102/**
Tejun Heo112202d2013-02-13 19:29:12 -08001103 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1104 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001105 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001106 *
1107 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001108 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001109 *
1110 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001111 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001112 */
Tejun Heo112202d2013-02-13 19:29:12 -08001113static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001114{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001115 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001116 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001117 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001118
Tejun Heo112202d2013-02-13 19:29:12 -08001119 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001120
Tejun Heo112202d2013-02-13 19:29:12 -08001121 pwq->nr_active--;
1122 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001123 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001124 if (pwq->nr_active < pwq->max_active)
1125 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001126 }
1127
1128 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001129 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001130 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001131
1132 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001133 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001134 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001135
Tejun Heo112202d2013-02-13 19:29:12 -08001136 /* this pwq is done, clear flush_color */
1137 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001138
1139 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001140 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001141 * will handle the rest.
1142 */
Tejun Heo112202d2013-02-13 19:29:12 -08001143 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1144 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001145out_put:
1146 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001147}
1148
Tejun Heo36e227d2012-08-03 10:30:46 -07001149/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001150 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001151 * @work: work item to steal
1152 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001153 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001154 *
1155 * Try to grab PENDING bit of @work. This function can handle @work in any
1156 * stable state - idle, on timer or on worklist. Return values are
1157 *
1158 * 1 if @work was pending and we successfully stole PENDING
1159 * 0 if @work was idle and we claimed PENDING
1160 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001161 * -ENOENT if someone else is canceling @work, this state may persist
1162 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001163 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001164 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001165 * interrupted while holding PENDING and @work off queue, irq must be
1166 * disabled on entry. This, combined with delayed_work->timer being
1167 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001168 *
1169 * On successful return, >= 0, irq is disabled and the caller is
1170 * responsible for releasing it using local_irq_restore(*@flags).
1171 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001172 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001173 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001174static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1175 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001176{
Tejun Heod565ed62013-01-24 11:01:33 -08001177 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001178 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001179
Tejun Heobbb68df2012-08-03 10:30:46 -07001180 local_irq_save(*flags);
1181
Tejun Heo36e227d2012-08-03 10:30:46 -07001182 /* try to steal the timer if it exists */
1183 if (is_dwork) {
1184 struct delayed_work *dwork = to_delayed_work(work);
1185
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001186 /*
1187 * dwork->timer is irqsafe. If del_timer() fails, it's
1188 * guaranteed that the timer is not queued anywhere and not
1189 * running on the local CPU.
1190 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001191 if (likely(del_timer(&dwork->timer)))
1192 return 1;
1193 }
1194
1195 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001196 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1197 return 0;
1198
1199 /*
1200 * The queueing is in progress, or it is already queued. Try to
1201 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1202 */
Tejun Heod565ed62013-01-24 11:01:33 -08001203 pool = get_work_pool(work);
1204 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001205 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001206
Tejun Heod565ed62013-01-24 11:01:33 -08001207 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001208 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001209 * work->data is guaranteed to point to pwq only while the work
1210 * item is queued on pwq->wq, and both updating work->data to point
1211 * to pwq on queueing and to pool on dequeueing are done under
1212 * pwq->pool->lock. This in turn guarantees that, if work->data
1213 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001214 * item is currently queued on that pool.
1215 */
Tejun Heo112202d2013-02-13 19:29:12 -08001216 pwq = get_work_pwq(work);
1217 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001218 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001219
Tejun Heo16062832013-02-06 18:04:53 -08001220 /*
1221 * A delayed work item cannot be grabbed directly because
1222 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001223 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001224 * management later on and cause stall. Make sure the work
1225 * item is activated before grabbing.
1226 */
1227 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001228 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001229
Tejun Heo16062832013-02-06 18:04:53 -08001230 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001231 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001232
Tejun Heo112202d2013-02-13 19:29:12 -08001233 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001234 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001235
Tejun Heo16062832013-02-06 18:04:53 -08001236 spin_unlock(&pool->lock);
1237 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001238 }
Tejun Heod565ed62013-01-24 11:01:33 -08001239 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001240fail:
1241 local_irq_restore(*flags);
1242 if (work_is_canceling(work))
1243 return -ENOENT;
1244 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001245 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001246}
1247
1248/**
Tejun Heo706026c2013-01-24 11:01:34 -08001249 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001250 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001251 * @work: work to insert
1252 * @head: insertion point
1253 * @extra_flags: extra WORK_STRUCT_* flags to set
1254 *
Tejun Heo112202d2013-02-13 19:29:12 -08001255 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001256 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001257 *
1258 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001259 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001260 */
Tejun Heo112202d2013-02-13 19:29:12 -08001261static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1262 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001263{
Tejun Heo112202d2013-02-13 19:29:12 -08001264 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001265
Tejun Heo4690c4a2010-06-29 10:07:10 +02001266 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001267 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001268 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001269 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001270
1271 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001272 * Ensure either wq_worker_sleeping() sees the above
1273 * list_add_tail() or we see zero nr_running to avoid workers lying
1274 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001275 */
1276 smp_mb();
1277
Tejun Heo63d95a92012-07-12 14:46:37 -07001278 if (__need_more_worker(pool))
1279 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001280}
1281
Tejun Heoc8efcc22010-12-20 19:32:04 +01001282/*
1283 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001284 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001285 */
1286static bool is_chained_work(struct workqueue_struct *wq)
1287{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001288 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001289
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001290 worker = current_wq_worker();
1291 /*
1292 * Return %true iff I'm a worker execuing a work item on @wq. If
1293 * I'm @worker, it's safe to dereference it without locking.
1294 */
Tejun Heo112202d2013-02-13 19:29:12 -08001295 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001296}
1297
Tejun Heod84ff052013-03-12 11:29:59 -07001298static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct work_struct *work)
1300{
Tejun Heo112202d2013-02-13 19:29:12 -08001301 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001302 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001303 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001304 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001305 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001306
1307 /*
1308 * While a work item is PENDING && off queue, a task trying to
1309 * steal the PENDING will busy-loop waiting for it to either get
1310 * queued or lose PENDING. Grabbing PENDING and queueing should
1311 * happen with IRQ disabled.
1312 */
1313 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001315 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001316
Tejun Heoc8efcc22010-12-20 19:32:04 +01001317 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001318 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001319 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001320 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001321retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001322 if (req_cpu == WORK_CPU_UNBOUND)
1323 cpu = raw_smp_processor_id();
1324
Tejun Heoc9178082013-03-12 11:30:04 -07001325 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001326 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001327 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001328 else
1329 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001330
Tejun Heoc9178082013-03-12 11:30:04 -07001331 /*
1332 * If @work was previously on a different pool, it might still be
1333 * running there, in which case the work needs to be queued on that
1334 * pool to guarantee non-reentrancy.
1335 */
1336 last_pool = get_work_pool(work);
1337 if (last_pool && last_pool != pwq->pool) {
1338 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001339
Tejun Heoc9178082013-03-12 11:30:04 -07001340 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001341
Tejun Heoc9178082013-03-12 11:30:04 -07001342 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001343
Tejun Heoc9178082013-03-12 11:30:04 -07001344 if (worker && worker->current_pwq->wq == wq) {
1345 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001346 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001347 /* meh... not running there, queue here */
1348 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001349 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001350 }
Tejun Heof3421792010-07-02 10:03:51 +02001351 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001352 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001353 }
1354
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001355 /*
1356 * pwq is determined and locked. For unbound pools, we could have
1357 * raced with pwq release and it could already be dead. If its
1358 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001359 * without another pwq replacing it in the numa_pwq_tbl or while
1360 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001361 * make forward-progress.
1362 */
1363 if (unlikely(!pwq->refcnt)) {
1364 if (wq->flags & WQ_UNBOUND) {
1365 spin_unlock(&pwq->pool->lock);
1366 cpu_relax();
1367 goto retry;
1368 }
1369 /* oops */
1370 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1371 wq->name, cpu);
1372 }
1373
Tejun Heo112202d2013-02-13 19:29:12 -08001374 /* pwq determined, queue */
1375 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001376
Dan Carpenterf5b25522012-04-13 22:06:58 +03001377 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001378 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001379 return;
1380 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001381
Tejun Heo112202d2013-02-13 19:29:12 -08001382 pwq->nr_in_flight[pwq->work_color]++;
1383 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001384
Tejun Heo112202d2013-02-13 19:29:12 -08001385 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001386 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001387 pwq->nr_active++;
1388 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001389 } else {
1390 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001391 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001392 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001393
Tejun Heo112202d2013-02-13 19:29:12 -08001394 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001395
Tejun Heo112202d2013-02-13 19:29:12 -08001396 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001399/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001400 * queue_work_on - queue work on specific cpu
1401 * @cpu: CPU number to execute work on
1402 * @wq: workqueue to use
1403 * @work: work to queue
1404 *
Tejun Heod4283e92012-08-03 10:30:44 -07001405 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001406 *
1407 * We queue the work to a specific CPU, the caller must ensure it
1408 * can't go away.
1409 */
Tejun Heod4283e92012-08-03 10:30:44 -07001410bool queue_work_on(int cpu, struct workqueue_struct *wq,
1411 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001412{
Tejun Heod4283e92012-08-03 10:30:44 -07001413 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001414 unsigned long flags;
1415
1416 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001417
Tejun Heo22df02b2010-06-29 10:07:10 +02001418 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001419 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001420 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001421 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001422
1423 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001424 return ret;
1425}
1426EXPORT_SYMBOL_GPL(queue_work_on);
1427
Tejun Heod8e794d2012-08-03 10:30:45 -07001428void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
David Howells52bad642006-11-22 14:54:01 +00001430 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001432 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001433 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001435EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001437static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1438 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001440 struct timer_list *timer = &dwork->timer;
1441 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001443 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1444 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001445 WARN_ON_ONCE(timer_pending(timer));
1446 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001447
Tejun Heo8852aac2012-12-01 16:23:42 -08001448 /*
1449 * If @delay is 0, queue @dwork->work immediately. This is for
1450 * both optimization and correctness. The earliest @timer can
1451 * expire is on the closest next tick and delayed_work users depend
1452 * on that there's no such delay when @delay is 0.
1453 */
1454 if (!delay) {
1455 __queue_work(cpu, wq, &dwork->work);
1456 return;
1457 }
1458
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001459 timer_stats_timer_set_start_info(&dwork->timer);
1460
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001461 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001462 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001463 timer->expires = jiffies + delay;
1464
1465 if (unlikely(cpu != WORK_CPU_UNBOUND))
1466 add_timer_on(timer, cpu);
1467 else
1468 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001471/**
1472 * queue_delayed_work_on - queue work on specific CPU after delay
1473 * @cpu: CPU number to execute work on
1474 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001475 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001476 * @delay: number of jiffies to wait before queueing
1477 *
Tejun Heo715f1302012-08-03 10:30:46 -07001478 * Returns %false if @work was already on a queue, %true otherwise. If
1479 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1480 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001481 */
Tejun Heod4283e92012-08-03 10:30:44 -07001482bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1483 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001484{
David Howells52bad642006-11-22 14:54:01 +00001485 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001486 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001487 unsigned long flags;
1488
1489 /* read the comment in __queue_work() */
1490 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001491
Tejun Heo22df02b2010-06-29 10:07:10 +02001492 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001493 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001494 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001495 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001496
1497 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001498 return ret;
1499}
Dave Jonesae90dd52006-06-30 01:40:45 -04001500EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tejun Heoc8e55f32010-06-29 10:07:12 +02001502/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001503 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1504 * @cpu: CPU number to execute work on
1505 * @wq: workqueue to use
1506 * @dwork: work to queue
1507 * @delay: number of jiffies to wait before queueing
1508 *
1509 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1510 * modify @dwork's timer so that it expires after @delay. If @delay is
1511 * zero, @work is guaranteed to be scheduled immediately regardless of its
1512 * current state.
1513 *
1514 * Returns %false if @dwork was idle and queued, %true if @dwork was
1515 * pending and its timer was modified.
1516 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001517 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001518 * See try_to_grab_pending() for details.
1519 */
1520bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1521 struct delayed_work *dwork, unsigned long delay)
1522{
1523 unsigned long flags;
1524 int ret;
1525
1526 do {
1527 ret = try_to_grab_pending(&dwork->work, true, &flags);
1528 } while (unlikely(ret == -EAGAIN));
1529
1530 if (likely(ret >= 0)) {
1531 __queue_delayed_work(cpu, wq, dwork, delay);
1532 local_irq_restore(flags);
1533 }
1534
1535 /* -ENOENT from try_to_grab_pending() becomes %true */
1536 return ret;
1537}
1538EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1539
1540/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001541 * worker_enter_idle - enter idle state
1542 * @worker: worker which is entering idle state
1543 *
1544 * @worker is entering idle state. Update stats and idle timer if
1545 * necessary.
1546 *
1547 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001548 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001549 */
1550static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001552 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Tejun Heo6183c002013-03-12 11:29:57 -07001554 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1555 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1556 (worker->hentry.next || worker->hentry.pprev)))
1557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Tejun Heocb444762010-07-02 10:03:50 +02001559 /* can't use worker_set_flags(), also called from start_worker() */
1560 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001561 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001563
Tejun Heoc8e55f32010-06-29 10:07:12 +02001564 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001565 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001566
Tejun Heo628c78e2012-07-17 12:39:27 -07001567 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1568 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001569
Tejun Heo544ecf32012-05-14 15:04:50 -07001570 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001571 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001572 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001573 * nr_running, the warning may trigger spuriously. Check iff
1574 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001575 */
Tejun Heo24647572013-01-24 11:01:33 -08001576 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001577 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001578 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Tejun Heoc8e55f32010-06-29 10:07:12 +02001581/**
1582 * worker_leave_idle - leave idle state
1583 * @worker: worker which is leaving idle state
1584 *
1585 * @worker is leaving idle state. Update stats.
1586 *
1587 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001588 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001589 */
1590static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001592 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Tejun Heo6183c002013-03-12 11:29:57 -07001594 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1595 return;
Tejun Heod302f012010-06-29 10:07:13 +02001596 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001597 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001598 list_del_init(&worker->entry);
1599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Tejun Heoe22bee72010-06-29 10:07:14 +02001601/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001602 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1603 * @pool: target worker_pool
1604 *
1605 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 *
1607 * Works which are scheduled while the cpu is online must at least be
1608 * scheduled to a worker which is bound to the cpu so that if they are
1609 * flushed from cpu callbacks while cpu is going down, they are
1610 * guaranteed to execute on the cpu.
1611 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001612 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001613 * themselves to the target cpu and may race with cpu going down or
1614 * coming online. kthread_bind() can't be used because it may put the
1615 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001616 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 * [dis]associated in the meantime.
1618 *
Tejun Heo706026c2013-01-24 11:01:34 -08001619 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001620 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001621 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1622 * enters idle state or fetches works without dropping lock, it can
1623 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 *
1625 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001626 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 * held.
1628 *
1629 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001630 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001631 * bound), %false if offline.
1632 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001633static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001634__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001635{
Tejun Heoe22bee72010-06-29 10:07:14 +02001636 while (true) {
1637 /*
1638 * The following call may fail, succeed or succeed
1639 * without actually migrating the task to the cpu if
1640 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001641 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 */
Tejun Heo24647572013-01-24 11:01:33 -08001643 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001644 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001645
Tejun Heod565ed62013-01-24 11:01:33 -08001646 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001647 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001649 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001650 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001651 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001652 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001653
Tejun Heo5035b202011-04-29 18:08:37 +02001654 /*
1655 * We've raced with CPU hot[un]plug. Give it a breather
1656 * and retry migration. cond_resched() is required here;
1657 * otherwise, we might deadlock against cpu_stop trying to
1658 * bring down the CPU on non-preemptive kernel.
1659 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001660 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001661 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001662 }
1663}
1664
Tejun Heoc34056a2010-06-29 10:07:11 +02001665static struct worker *alloc_worker(void)
1666{
1667 struct worker *worker;
1668
1669 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001670 if (worker) {
1671 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001672 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001673 /* on creation a worker is in !idle && prep state */
1674 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001675 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001676 return worker;
1677}
1678
1679/**
1680 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001681 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001682 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001683 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001684 * can be started by calling start_worker() or destroyed using
1685 * destroy_worker().
1686 *
1687 * CONTEXT:
1688 * Might sleep. Does GFP_KERNEL allocations.
1689 *
1690 * RETURNS:
1691 * Pointer to the newly created worker.
1692 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001693static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001694{
Tejun Heoc34056a2010-06-29 10:07:11 +02001695 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001696 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001697 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001698
Tejun Heocd549682013-03-13 19:47:39 -07001699 lockdep_assert_held(&pool->manager_mutex);
1700
Tejun Heo822d8402013-03-19 13:45:21 -07001701 /*
1702 * ID is needed to determine kthread name. Allocate ID first
1703 * without installing the pointer.
1704 */
1705 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001706 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001707
1708 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1709
Tejun Heod565ed62013-01-24 11:01:33 -08001710 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001711 idr_preload_end();
1712 if (id < 0)
1713 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001714
1715 worker = alloc_worker();
1716 if (!worker)
1717 goto fail;
1718
Tejun Heobd7bdd42012-07-12 14:46:37 -07001719 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001720 worker->id = id;
1721
Tejun Heo29c91e92013-03-12 11:30:03 -07001722 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001723 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1724 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001725 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001726 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1727
Tejun Heof3f90ad2013-04-01 11:23:34 -07001728 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001729 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001730 if (IS_ERR(worker->task))
1731 goto fail;
1732
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001733 /*
1734 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1735 * online CPUs. It'll be re-applied when any of the CPUs come up.
1736 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001737 set_user_nice(worker->task, pool->attrs->nice);
1738 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001739
Tejun Heo14a40ff2013-03-19 13:45:20 -07001740 /* prevent userland from meddling with cpumask of workqueue workers */
1741 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001742
1743 /*
1744 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1745 * remains stable across this function. See the comments above the
1746 * flag definition for details.
1747 */
1748 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001749 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001750
Tejun Heo822d8402013-03-19 13:45:21 -07001751 /* successful, commit the pointer to idr */
1752 spin_lock_irq(&pool->lock);
1753 idr_replace(&pool->worker_idr, worker, worker->id);
1754 spin_unlock_irq(&pool->lock);
1755
Tejun Heoc34056a2010-06-29 10:07:11 +02001756 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001757
Tejun Heoc34056a2010-06-29 10:07:11 +02001758fail:
1759 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001760 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001761 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001762 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 }
1764 kfree(worker);
1765 return NULL;
1766}
1767
1768/**
1769 * start_worker - start a newly created worker
1770 * @worker: worker to start
1771 *
Tejun Heo706026c2013-01-24 11:01:34 -08001772 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 *
1774 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001775 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001776 */
1777static void start_worker(struct worker *worker)
1778{
Tejun Heocb444762010-07-02 10:03:50 +02001779 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001780 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001781 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001782 wake_up_process(worker->task);
1783}
1784
1785/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001786 * create_and_start_worker - create and start a worker for a pool
1787 * @pool: the target pool
1788 *
Tejun Heocd549682013-03-13 19:47:39 -07001789 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001790 */
1791static int create_and_start_worker(struct worker_pool *pool)
1792{
1793 struct worker *worker;
1794
Tejun Heocd549682013-03-13 19:47:39 -07001795 mutex_lock(&pool->manager_mutex);
1796
Tejun Heoebf44d12013-03-13 19:47:39 -07001797 worker = create_worker(pool);
1798 if (worker) {
1799 spin_lock_irq(&pool->lock);
1800 start_worker(worker);
1801 spin_unlock_irq(&pool->lock);
1802 }
1803
Tejun Heocd549682013-03-13 19:47:39 -07001804 mutex_unlock(&pool->manager_mutex);
1805
Tejun Heoebf44d12013-03-13 19:47:39 -07001806 return worker ? 0 : -ENOMEM;
1807}
1808
1809/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001810 * destroy_worker - destroy a workqueue worker
1811 * @worker: worker to be destroyed
1812 *
Tejun Heo706026c2013-01-24 11:01:34 -08001813 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001814 *
1815 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001816 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001817 */
1818static void destroy_worker(struct worker *worker)
1819{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001820 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001821
Tejun Heocd549682013-03-13 19:47:39 -07001822 lockdep_assert_held(&pool->manager_mutex);
1823 lockdep_assert_held(&pool->lock);
1824
Tejun Heoc34056a2010-06-29 10:07:11 +02001825 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001826 if (WARN_ON(worker->current_work) ||
1827 WARN_ON(!list_empty(&worker->scheduled)))
1828 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001829
Tejun Heoc8e55f32010-06-29 10:07:12 +02001830 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001831 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001833 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001834
1835 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001836 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001837
Tejun Heo822d8402013-03-19 13:45:21 -07001838 idr_remove(&pool->worker_idr, worker->id);
1839
Tejun Heod565ed62013-01-24 11:01:33 -08001840 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001841
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 kthread_stop(worker->task);
1843 kfree(worker);
1844
Tejun Heod565ed62013-01-24 11:01:33 -08001845 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001846}
1847
Tejun Heo63d95a92012-07-12 14:46:37 -07001848static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001849{
Tejun Heo63d95a92012-07-12 14:46:37 -07001850 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001851
Tejun Heod565ed62013-01-24 11:01:33 -08001852 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001853
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 struct worker *worker;
1856 unsigned long expires;
1857
1858 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001859 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001860 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1861
1862 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 else {
1865 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001866 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001867 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001868 }
1869 }
1870
Tejun Heod565ed62013-01-24 11:01:33 -08001871 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001872}
1873
Tejun Heo493a1722013-03-12 11:29:59 -07001874static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001875{
Tejun Heo112202d2013-02-13 19:29:12 -08001876 struct pool_workqueue *pwq = get_work_pwq(work);
1877 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001878
Tejun Heo2e109a22013-03-13 19:47:40 -07001879 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001880
Tejun Heo493008a2013-03-12 11:30:03 -07001881 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001882 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001883
1884 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001885 if (list_empty(&pwq->mayday_node)) {
1886 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001887 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001888 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001889}
1890
Tejun Heo706026c2013-01-24 11:01:34 -08001891static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001892{
Tejun Heo63d95a92012-07-12 14:46:37 -07001893 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001894 struct work_struct *work;
1895
Tejun Heo2e109a22013-03-13 19:47:40 -07001896 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001897 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001898
Tejun Heo63d95a92012-07-12 14:46:37 -07001899 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001900 /*
1901 * We've been trying to create a new worker but
1902 * haven't been successful. We might be hitting an
1903 * allocation deadlock. Send distress signals to
1904 * rescuers.
1905 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001906 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001907 send_mayday(work);
1908 }
1909
Tejun Heo493a1722013-03-12 11:29:59 -07001910 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001911 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001912
Tejun Heo63d95a92012-07-12 14:46:37 -07001913 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001914}
1915
1916/**
1917 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 * have at least one idle worker on return from this function. If
1922 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 * possible allocation deadlock.
1925 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001926 * On return, need_to_create_worker() is guaranteed to be %false and
1927 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 *
1929 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001930 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 * multiple times. Does GFP_KERNEL allocations. Called only from
1932 * manager.
1933 *
1934 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001935 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 * otherwise.
1937 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001938static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001939__releases(&pool->lock)
1940__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001941{
Tejun Heo63d95a92012-07-12 14:46:37 -07001942 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001943 return false;
1944restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001945 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001946
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001949
1950 while (true) {
1951 struct worker *worker;
1952
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001953 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001955 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001956 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001958 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1959 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 return true;
1961 }
1962
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 break;
1965
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 __set_current_state(TASK_INTERRUPTIBLE);
1967 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001968
Tejun Heo63d95a92012-07-12 14:46:37 -07001969 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 break;
1971 }
1972
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001974 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001975 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 goto restart;
1977 return true;
1978}
1979
1980/**
1981 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001982 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001984 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 * IDLE_WORKER_TIMEOUT.
1986 *
1987 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001988 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 * multiple times. Called only from manager.
1990 *
1991 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001992 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 * otherwise.
1994 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001995static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001996{
1997 bool ret = false;
1998
Tejun Heo63d95a92012-07-12 14:46:37 -07001999 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 struct worker *worker;
2001 unsigned long expires;
2002
Tejun Heo63d95a92012-07-12 14:46:37 -07002003 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2005
2006 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002007 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 break;
2009 }
2010
2011 destroy_worker(worker);
2012 ret = true;
2013 }
2014
2015 return ret;
2016}
2017
2018/**
2019 * manage_workers - manage worker pool
2020 * @worker: self
2021 *
Tejun Heo706026c2013-01-24 11:01:34 -08002022 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002024 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 *
2026 * The caller can safely start processing works on false return. On
2027 * true return, it's guaranteed that need_to_create_worker() is false
2028 * and may_start_working() is true.
2029 *
2030 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002031 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 * multiple times. Does GFP_KERNEL allocations.
2033 *
2034 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002035 * spin_lock_irq(pool->lock) which may be released and regrabbed
2036 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 */
2038static bool manage_workers(struct worker *worker)
2039{
Tejun Heo63d95a92012-07-12 14:46:37 -07002040 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 bool ret = false;
2042
Tejun Heobc3a1af2013-03-13 19:47:39 -07002043 /*
2044 * Managership is governed by two mutexes - manager_arb and
2045 * manager_mutex. manager_arb handles arbitration of manager role.
2046 * Anyone who successfully grabs manager_arb wins the arbitration
2047 * and becomes the manager. mutex_trylock() on pool->manager_arb
2048 * failure while holding pool->lock reliably indicates that someone
2049 * else is managing the pool and the worker which failed trylock
2050 * can proceed to executing work items. This means that anyone
2051 * grabbing manager_arb is responsible for actually performing
2052 * manager duties. If manager_arb is grabbed and released without
2053 * actual management, the pool may stall indefinitely.
2054 *
2055 * manager_mutex is used for exclusion of actual management
2056 * operations. The holder of manager_mutex can be sure that none
2057 * of management operations, including creation and destruction of
2058 * workers, won't take place until the mutex is released. Because
2059 * manager_mutex doesn't interfere with manager role arbitration,
2060 * it is guaranteed that the pool's management, while may be
2061 * delayed, won't be disturbed by someone else grabbing
2062 * manager_mutex.
2063 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002064 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002065 return ret;
2066
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002067 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002068 * With manager arbitration won, manager_mutex would be free in
2069 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002070 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002071 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002072 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002073 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002074 ret = true;
2075 }
2076
Tejun Heo11ebea52012-07-12 14:46:37 -07002077 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002078
2079 /*
2080 * Destroy and then create so that may_start_working() is true
2081 * on return.
2082 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002083 ret |= maybe_destroy_workers(pool);
2084 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002085
Tejun Heobc3a1af2013-03-13 19:47:39 -07002086 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002087 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002088 return ret;
2089}
2090
Tejun Heoa62428c2010-06-29 10:07:10 +02002091/**
2092 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002093 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002094 * @work: work to process
2095 *
2096 * Process @work. This function contains all the logics necessary to
2097 * process a single work including synchronization against and
2098 * interaction with other workers on the same cpu, queueing and
2099 * flushing. As long as context requirement is met, any worker can
2100 * call this function to process a work.
2101 *
2102 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002103 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002104 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002105static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002106__releases(&pool->lock)
2107__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002108{
Tejun Heo112202d2013-02-13 19:29:12 -08002109 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002110 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002111 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002112 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002113 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002114#ifdef CONFIG_LOCKDEP
2115 /*
2116 * It is permissible to free the struct work_struct from
2117 * inside the function that is called from it, this we need to
2118 * take into account for lockdep too. To avoid bogus "held
2119 * lock freed" warnings as well as problems when looking into
2120 * work->lockdep_map, make a copy and use that here.
2121 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002122 struct lockdep_map lockdep_map;
2123
2124 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002125#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002126 /*
2127 * Ensure we're on the correct CPU. DISASSOCIATED test is
2128 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002129 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002130 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002131 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002132 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002133 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002134
Tejun Heo7e116292010-06-29 10:07:13 +02002135 /*
2136 * A single work shouldn't be executed concurrently by
2137 * multiple workers on a single cpu. Check whether anyone is
2138 * already processing the work. If so, defer the work to the
2139 * currently executing one.
2140 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002141 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002142 if (unlikely(collision)) {
2143 move_linked_works(work, &collision->scheduled, NULL);
2144 return;
2145 }
2146
Tejun Heo8930cab2012-08-03 10:30:45 -07002147 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002148 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002149 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002150 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002151 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002152 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002153 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002154
Tejun Heoa62428c2010-06-29 10:07:10 +02002155 list_del_init(&work->entry);
2156
Tejun Heo649027d2010-06-29 10:07:14 +02002157 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002158 * CPU intensive works don't participate in concurrency
2159 * management. They're the scheduler's responsibility.
2160 */
2161 if (unlikely(cpu_intensive))
2162 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2163
Tejun Heo974271c2012-07-12 14:46:37 -07002164 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002165 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002166 * executed ASAP. Wake up another worker if necessary.
2167 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002168 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2169 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002170
Tejun Heo8930cab2012-08-03 10:30:45 -07002171 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002172 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002173 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002174 * PENDING and queued state changes happen together while IRQ is
2175 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002176 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002177 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002178
Tejun Heod565ed62013-01-24 11:01:33 -08002179 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002180
Tejun Heo112202d2013-02-13 19:29:12 -08002181 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002182 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002183 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002184 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002185 /*
2186 * While we must be careful to not use "work" after this, the trace
2187 * point will only record its address.
2188 */
2189 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002190 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002191 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002192
2193 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002194 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2195 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002196 current->comm, preempt_count(), task_pid_nr(current),
2197 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002198 debug_show_held_locks(current);
2199 dump_stack();
2200 }
2201
Tejun Heod565ed62013-01-24 11:01:33 -08002202 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002203
Tejun Heofb0e7be2010-06-29 10:07:15 +02002204 /* clear cpu intensive status */
2205 if (unlikely(cpu_intensive))
2206 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2207
Tejun Heoa62428c2010-06-29 10:07:10 +02002208 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002209 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002210 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002211 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002212 worker->current_pwq = NULL;
2213 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002214}
2215
Tejun Heoaffee4b2010-06-29 10:07:12 +02002216/**
2217 * process_scheduled_works - process scheduled works
2218 * @worker: self
2219 *
2220 * Process all scheduled works. Please note that the scheduled list
2221 * may change while processing a work, so this function repeatedly
2222 * fetches a work from the top and executes it.
2223 *
2224 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002225 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002226 * multiple times.
2227 */
2228static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002230 while (!list_empty(&worker->scheduled)) {
2231 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002233 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Tejun Heo4690c4a2010-06-29 10:07:10 +02002237/**
2238 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002239 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002240 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002241 * The worker thread function. All workers belong to a worker_pool -
2242 * either a per-cpu one or dynamic unbound one. These workers process all
2243 * work items regardless of their specific target workqueue. The only
2244 * exception is work items which belong to workqueues with a rescuer which
2245 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002246 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002247static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Tejun Heoc34056a2010-06-29 10:07:11 +02002249 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002250 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Tejun Heoe22bee72010-06-29 10:07:14 +02002252 /* tell the scheduler that this is a workqueue worker */
2253 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002254woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002255 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Tejun Heoa9ab7752013-03-19 13:45:21 -07002257 /* am I supposed to die? */
2258 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002259 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002260 WARN_ON_ONCE(!list_empty(&worker->entry));
2261 worker->task->flags &= ~PF_WQ_WORKER;
2262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
Tejun Heoc8e55f32010-06-29 10:07:12 +02002265 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002266recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002267 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002268 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002269 goto sleep;
2270
2271 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002272 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002273 goto recheck;
2274
Tejun Heoc8e55f32010-06-29 10:07:12 +02002275 /*
2276 * ->scheduled list can only be filled while a worker is
2277 * preparing to process a work or actually processing it.
2278 * Make sure nobody diddled with it while I was sleeping.
2279 */
Tejun Heo6183c002013-03-12 11:29:57 -07002280 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002281
Tejun Heoe22bee72010-06-29 10:07:14 +02002282 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002283 * Finish PREP stage. We're guaranteed to have at least one idle
2284 * worker or that someone else has already assumed the manager
2285 * role. This is where @worker starts participating in concurrency
2286 * management if applicable and concurrency management is restored
2287 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002288 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002289 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002290
2291 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002292 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002293 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002294 struct work_struct, entry);
2295
2296 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2297 /* optimization path, not strictly necessary */
2298 process_one_work(worker, work);
2299 if (unlikely(!list_empty(&worker->scheduled)))
2300 process_scheduled_works(worker);
2301 } else {
2302 move_linked_works(work, &worker->scheduled, NULL);
2303 process_scheduled_works(worker);
2304 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002305 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002306
Tejun Heoe22bee72010-06-29 10:07:14 +02002307 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002308sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002309 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002310 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002311
Tejun Heoc8e55f32010-06-29 10:07:12 +02002312 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002313 * pool->lock is held and there's no work to process and no need to
2314 * manage, sleep. Workers are woken up only while holding
2315 * pool->lock or from local cpu, so setting the current state
2316 * before releasing pool->lock is enough to prevent losing any
2317 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002318 */
2319 worker_enter_idle(worker);
2320 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002321 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002322 schedule();
2323 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Tejun Heoe22bee72010-06-29 10:07:14 +02002326/**
2327 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002328 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002329 *
2330 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002331 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002332 *
Tejun Heo706026c2013-01-24 11:01:34 -08002333 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002334 * worker which uses GFP_KERNEL allocation which has slight chance of
2335 * developing into deadlock if some works currently on the same queue
2336 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2337 * the problem rescuer solves.
2338 *
Tejun Heo706026c2013-01-24 11:01:34 -08002339 * When such condition is possible, the pool summons rescuers of all
2340 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 * those works so that forward progress can be guaranteed.
2342 *
2343 * This should happen rarely.
2344 */
Tejun Heo111c2252013-01-17 17:16:24 -08002345static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002346{
Tejun Heo111c2252013-01-17 17:16:24 -08002347 struct worker *rescuer = __rescuer;
2348 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002349 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002350
2351 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002352
2353 /*
2354 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2355 * doesn't participate in concurrency management.
2356 */
2357 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002358repeat:
2359 set_current_state(TASK_INTERRUPTIBLE);
2360
Mike Galbraith412d32e2012-11-28 07:17:18 +01002361 if (kthread_should_stop()) {
2362 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002363 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002364 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002365 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002366
Tejun Heo493a1722013-03-12 11:29:59 -07002367 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002368 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002369
2370 while (!list_empty(&wq->maydays)) {
2371 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2372 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002373 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002374 struct work_struct *work, *n;
2375
2376 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002377 list_del_init(&pwq->mayday_node);
2378
Tejun Heo2e109a22013-03-13 19:47:40 -07002379 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002380
2381 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002382 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002383 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002384
2385 /*
2386 * Slurp in all works issued via this workqueue and
2387 * process'em.
2388 */
Tejun Heo6183c002013-03-12 11:29:57 -07002389 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002390 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002391 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002392 move_linked_works(work, scheduled, &n);
2393
2394 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002395
2396 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002397 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002398 * regular worker; otherwise, we end up with 0 concurrency
2399 * and stalling the execution.
2400 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002401 if (keep_working(pool))
2402 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002403
Lai Jiangshanb3104102013-02-19 12:17:02 -08002404 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002405 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002406 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002407 }
2408
Tejun Heo2e109a22013-03-13 19:47:40 -07002409 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002410
Tejun Heo111c2252013-01-17 17:16:24 -08002411 /* rescuers should never participate in concurrency management */
2412 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002413 schedule();
2414 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002417struct wq_barrier {
2418 struct work_struct work;
2419 struct completion done;
2420};
2421
2422static void wq_barrier_func(struct work_struct *work)
2423{
2424 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2425 complete(&barr->done);
2426}
2427
Tejun Heo4690c4a2010-06-29 10:07:10 +02002428/**
2429 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002430 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002431 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002432 * @target: target work to attach @barr to
2433 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002434 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002435 * @barr is linked to @target such that @barr is completed only after
2436 * @target finishes execution. Please note that the ordering
2437 * guarantee is observed only with respect to @target and on the local
2438 * cpu.
2439 *
2440 * Currently, a queued barrier can't be canceled. This is because
2441 * try_to_grab_pending() can't determine whether the work to be
2442 * grabbed is at the head of the queue and thus can't clear LINKED
2443 * flag of the previous work while there must be a valid next work
2444 * after a work with LINKED flag set.
2445 *
2446 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002447 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002448 *
2449 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002450 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002451 */
Tejun Heo112202d2013-02-13 19:29:12 -08002452static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002453 struct wq_barrier *barr,
2454 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002455{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002456 struct list_head *head;
2457 unsigned int linked = 0;
2458
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002459 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002460 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002461 * as we know for sure that this will not trigger any of the
2462 * checks and call back into the fixup functions where we
2463 * might deadlock.
2464 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002465 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002466 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002467 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002468
Tejun Heoaffee4b2010-06-29 10:07:12 +02002469 /*
2470 * If @target is currently being executed, schedule the
2471 * barrier to the worker; otherwise, put it after @target.
2472 */
2473 if (worker)
2474 head = worker->scheduled.next;
2475 else {
2476 unsigned long *bits = work_data_bits(target);
2477
2478 head = target->entry.next;
2479 /* there can already be other linked works, inherit and set */
2480 linked = *bits & WORK_STRUCT_LINKED;
2481 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2482 }
2483
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002484 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002485 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002486 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002487}
2488
Tejun Heo73f53c42010-06-29 10:07:11 +02002489/**
Tejun Heo112202d2013-02-13 19:29:12 -08002490 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002491 * @wq: workqueue being flushed
2492 * @flush_color: new flush color, < 0 for no-op
2493 * @work_color: new work color, < 0 for no-op
2494 *
Tejun Heo112202d2013-02-13 19:29:12 -08002495 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002496 *
Tejun Heo112202d2013-02-13 19:29:12 -08002497 * If @flush_color is non-negative, flush_color on all pwqs should be
2498 * -1. If no pwq has in-flight commands at the specified color, all
2499 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2500 * has in flight commands, its pwq->flush_color is set to
2501 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002502 * wakeup logic is armed and %true is returned.
2503 *
2504 * The caller should have initialized @wq->first_flusher prior to
2505 * calling this function with non-negative @flush_color. If
2506 * @flush_color is negative, no flush color update is done and %false
2507 * is returned.
2508 *
Tejun Heo112202d2013-02-13 19:29:12 -08002509 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 * work_color which is previous to @work_color and all will be
2511 * advanced to @work_color.
2512 *
2513 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002514 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002515 *
2516 * RETURNS:
2517 * %true if @flush_color >= 0 and there's something to flush. %false
2518 * otherwise.
2519 */
Tejun Heo112202d2013-02-13 19:29:12 -08002520static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002521 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Tejun Heo73f53c42010-06-29 10:07:11 +02002523 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002524 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002525
Tejun Heo73f53c42010-06-29 10:07:11 +02002526 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002527 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002528 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002529 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002530
Tejun Heo49e3cf42013-03-12 11:29:58 -07002531 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002532 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002533
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002534 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002535
2536 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002537 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002538
Tejun Heo112202d2013-02-13 19:29:12 -08002539 if (pwq->nr_in_flight[flush_color]) {
2540 pwq->flush_color = flush_color;
2541 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002542 wait = true;
2543 }
2544 }
2545
2546 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002547 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002548 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002549 }
2550
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002551 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002552 }
2553
Tejun Heo112202d2013-02-13 19:29:12 -08002554 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002555 complete(&wq->first_flusher->done);
2556
2557 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002560/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002562 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002564 * This function sleeps until all work items which were queued on entry
2565 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002567void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Tejun Heo73f53c42010-06-29 10:07:11 +02002569 struct wq_flusher this_flusher = {
2570 .list = LIST_HEAD_INIT(this_flusher.list),
2571 .flush_color = -1,
2572 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2573 };
2574 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002575
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002576 lock_map_acquire(&wq->lockdep_map);
2577 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002579 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002580
2581 /*
2582 * Start-to-wait phase
2583 */
2584 next_color = work_next_color(wq->work_color);
2585
2586 if (next_color != wq->flush_color) {
2587 /*
2588 * Color space is not full. The current work_color
2589 * becomes our flush_color and work_color is advanced
2590 * by one.
2591 */
Tejun Heo6183c002013-03-12 11:29:57 -07002592 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002593 this_flusher.flush_color = wq->work_color;
2594 wq->work_color = next_color;
2595
2596 if (!wq->first_flusher) {
2597 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002598 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002599
2600 wq->first_flusher = &this_flusher;
2601
Tejun Heo112202d2013-02-13 19:29:12 -08002602 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002603 wq->work_color)) {
2604 /* nothing to flush, done */
2605 wq->flush_color = next_color;
2606 wq->first_flusher = NULL;
2607 goto out_unlock;
2608 }
2609 } else {
2610 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002611 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002612 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002613 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002614 }
2615 } else {
2616 /*
2617 * Oops, color space is full, wait on overflow queue.
2618 * The next flush completion will assign us
2619 * flush_color and transfer to flusher_queue.
2620 */
2621 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2622 }
2623
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002624 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002625
2626 wait_for_completion(&this_flusher.done);
2627
2628 /*
2629 * Wake-up-and-cascade phase
2630 *
2631 * First flushers are responsible for cascading flushes and
2632 * handling overflow. Non-first flushers can simply return.
2633 */
2634 if (wq->first_flusher != &this_flusher)
2635 return;
2636
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002637 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002638
Tejun Heo4ce48b32010-07-02 10:03:51 +02002639 /* we might have raced, check again with mutex held */
2640 if (wq->first_flusher != &this_flusher)
2641 goto out_unlock;
2642
Tejun Heo73f53c42010-06-29 10:07:11 +02002643 wq->first_flusher = NULL;
2644
Tejun Heo6183c002013-03-12 11:29:57 -07002645 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2646 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002647
2648 while (true) {
2649 struct wq_flusher *next, *tmp;
2650
2651 /* complete all the flushers sharing the current flush color */
2652 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2653 if (next->flush_color != wq->flush_color)
2654 break;
2655 list_del_init(&next->list);
2656 complete(&next->done);
2657 }
2658
Tejun Heo6183c002013-03-12 11:29:57 -07002659 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2660 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002661
2662 /* this flush_color is finished, advance by one */
2663 wq->flush_color = work_next_color(wq->flush_color);
2664
2665 /* one color has been freed, handle overflow queue */
2666 if (!list_empty(&wq->flusher_overflow)) {
2667 /*
2668 * Assign the same color to all overflowed
2669 * flushers, advance work_color and append to
2670 * flusher_queue. This is the start-to-wait
2671 * phase for these overflowed flushers.
2672 */
2673 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2674 tmp->flush_color = wq->work_color;
2675
2676 wq->work_color = work_next_color(wq->work_color);
2677
2678 list_splice_tail_init(&wq->flusher_overflow,
2679 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002680 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002681 }
2682
2683 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002684 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002685 break;
2686 }
2687
2688 /*
2689 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002690 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002691 */
Tejun Heo6183c002013-03-12 11:29:57 -07002692 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2693 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002694
2695 list_del_init(&next->list);
2696 wq->first_flusher = next;
2697
Tejun Heo112202d2013-02-13 19:29:12 -08002698 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002699 break;
2700
2701 /*
2702 * Meh... this color is already done, clear first
2703 * flusher and repeat cascading.
2704 */
2705 wq->first_flusher = NULL;
2706 }
2707
2708out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002709 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
Dave Jonesae90dd52006-06-30 01:40:45 -04002711EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002713/**
2714 * drain_workqueue - drain a workqueue
2715 * @wq: workqueue to drain
2716 *
2717 * Wait until the workqueue becomes empty. While draining is in progress,
2718 * only chain queueing is allowed. IOW, only currently pending or running
2719 * work items on @wq can queue further work items on it. @wq is flushed
2720 * repeatedly until it becomes empty. The number of flushing is detemined
2721 * by the depth of chaining and should be relatively short. Whine if it
2722 * takes too long.
2723 */
2724void drain_workqueue(struct workqueue_struct *wq)
2725{
2726 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002727 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002728
2729 /*
2730 * __queue_work() needs to test whether there are drainers, is much
2731 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002732 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002733 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002734 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002735 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002736 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002737 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002738reflush:
2739 flush_workqueue(wq);
2740
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002741 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002742
Tejun Heo49e3cf42013-03-12 11:29:58 -07002743 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002744 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002745
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002746 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002747 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002748 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002749
2750 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002751 continue;
2752
2753 if (++flush_cnt == 10 ||
2754 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002755 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002756 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002757
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002758 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002759 goto reflush;
2760 }
2761
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002762 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002763 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002764 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002765}
2766EXPORT_SYMBOL_GPL(drain_workqueue);
2767
Tejun Heo606a5022012-08-20 14:51:23 -07002768static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002769{
2770 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002771 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002772 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002773
2774 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002775
Tejun Heofa1b54e2013-03-12 11:30:00 -07002776 local_irq_disable();
2777 pool = get_work_pool(work);
2778 if (!pool) {
2779 local_irq_enable();
2780 return false;
2781 }
2782
2783 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002784 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002785 pwq = get_work_pwq(work);
2786 if (pwq) {
2787 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002788 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002789 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002790 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002791 if (!worker)
2792 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002793 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002794 }
Tejun Heobaf59022010-09-16 10:42:16 +02002795
Tejun Heo112202d2013-02-13 19:29:12 -08002796 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002797 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002798
Tejun Heoe1594892011-01-09 23:32:15 +01002799 /*
2800 * If @max_active is 1 or rescuer is in use, flushing another work
2801 * item on the same workqueue may lead to deadlock. Make sure the
2802 * flusher is not running on the same workqueue by verifying write
2803 * access.
2804 */
Tejun Heo493008a2013-03-12 11:30:03 -07002805 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002806 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002807 else
Tejun Heo112202d2013-02-13 19:29:12 -08002808 lock_map_acquire_read(&pwq->wq->lockdep_map);
2809 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002810
Tejun Heobaf59022010-09-16 10:42:16 +02002811 return true;
2812already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002813 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002814 return false;
2815}
2816
Oleg Nesterovdb700892008-07-25 01:47:49 -07002817/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002818 * flush_work - wait for a work to finish executing the last queueing instance
2819 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002820 *
Tejun Heo606a5022012-08-20 14:51:23 -07002821 * Wait until @work has finished execution. @work is guaranteed to be idle
2822 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002823 *
2824 * RETURNS:
2825 * %true if flush_work() waited for the work to finish execution,
2826 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002827 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002828bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002829{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002830 struct wq_barrier barr;
2831
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002832 lock_map_acquire(&work->lockdep_map);
2833 lock_map_release(&work->lockdep_map);
2834
Tejun Heo606a5022012-08-20 14:51:23 -07002835 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002836 wait_for_completion(&barr.done);
2837 destroy_work_on_stack(&barr.work);
2838 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002839 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002840 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002841 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002842}
2843EXPORT_SYMBOL_GPL(flush_work);
2844
Tejun Heo36e227d2012-08-03 10:30:46 -07002845static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002846{
Tejun Heobbb68df2012-08-03 10:30:46 -07002847 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002848 int ret;
2849
2850 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002851 ret = try_to_grab_pending(work, is_dwork, &flags);
2852 /*
2853 * If someone else is canceling, wait for the same event it
2854 * would be waiting for before retrying.
2855 */
2856 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002857 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002858 } while (unlikely(ret < 0));
2859
Tejun Heobbb68df2012-08-03 10:30:46 -07002860 /* tell other tasks trying to grab @work to back off */
2861 mark_work_canceling(work);
2862 local_irq_restore(flags);
2863
Tejun Heo606a5022012-08-20 14:51:23 -07002864 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002865 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002866 return ret;
2867}
2868
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002869/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002870 * cancel_work_sync - cancel a work and wait for it to finish
2871 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002872 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002873 * Cancel @work and wait for its execution to finish. This function
2874 * can be used even if the work re-queues itself or migrates to
2875 * another workqueue. On return from this function, @work is
2876 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002877 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002878 * cancel_work_sync(&delayed_work->work) must not be used for
2879 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002880 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002881 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002882 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002883 *
2884 * RETURNS:
2885 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002886 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002887bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002888{
Tejun Heo36e227d2012-08-03 10:30:46 -07002889 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002890}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002891EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002892
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002893/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002894 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2895 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002896 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002897 * Delayed timer is cancelled and the pending work is queued for
2898 * immediate execution. Like flush_work(), this function only
2899 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002900 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * RETURNS:
2902 * %true if flush_work() waited for the work to finish execution,
2903 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002904 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002905bool flush_delayed_work(struct delayed_work *dwork)
2906{
Tejun Heo8930cab2012-08-03 10:30:45 -07002907 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002908 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002909 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002910 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002911 return flush_work(&dwork->work);
2912}
2913EXPORT_SYMBOL(flush_delayed_work);
2914
2915/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002916 * cancel_delayed_work - cancel a delayed work
2917 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002918 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002919 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2920 * and canceled; %false if wasn't pending. Note that the work callback
2921 * function may still be running on return, unless it returns %true and the
2922 * work doesn't re-arm itself. Explicitly flush or use
2923 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002924 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002925 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002926 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002927bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002928{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002929 unsigned long flags;
2930 int ret;
2931
2932 do {
2933 ret = try_to_grab_pending(&dwork->work, true, &flags);
2934 } while (unlikely(ret == -EAGAIN));
2935
2936 if (unlikely(ret < 0))
2937 return false;
2938
Tejun Heo7c3eed52013-01-24 11:01:33 -08002939 set_work_pool_and_clear_pending(&dwork->work,
2940 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002941 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002942 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002943}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002944EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002945
2946/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002947 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2948 * @dwork: the delayed work cancel
2949 *
2950 * This is cancel_work_sync() for delayed works.
2951 *
2952 * RETURNS:
2953 * %true if @dwork was pending, %false otherwise.
2954 */
2955bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002956{
Tejun Heo36e227d2012-08-03 10:30:46 -07002957 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002958}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002959EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002961/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002962 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002963 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002964 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002965 * schedule_on_each_cpu() executes @func on each online CPU using the
2966 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002967 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002968 *
2969 * RETURNS:
2970 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002971 */
David Howells65f27f32006-11-22 14:55:48 +00002972int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002973{
2974 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002975 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002976
Andrew Mortonb6136772006-06-25 05:47:49 -07002977 works = alloc_percpu(struct work_struct);
2978 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002979 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002980
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002981 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002982
Christoph Lameter15316ba2006-01-08 01:00:43 -08002983 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002984 struct work_struct *work = per_cpu_ptr(works, cpu);
2985
2986 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002988 }
Tejun Heo93981802009-11-17 14:06:20 -08002989
2990 for_each_online_cpu(cpu)
2991 flush_work(per_cpu_ptr(works, cpu));
2992
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002993 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002994 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002995 return 0;
2996}
2997
Alan Sterneef6a7d2010-02-12 17:39:21 +09002998/**
2999 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3000 *
3001 * Forces execution of the kernel-global workqueue and blocks until its
3002 * completion.
3003 *
3004 * Think twice before calling this function! It's very easy to get into
3005 * trouble if you don't take great care. Either of the following situations
3006 * will lead to deadlock:
3007 *
3008 * One of the work items currently on the workqueue needs to acquire
3009 * a lock held by your code or its caller.
3010 *
3011 * Your code is running in the context of a work routine.
3012 *
3013 * They will be detected by lockdep when they occur, but the first might not
3014 * occur very often. It depends on what work items are on the workqueue and
3015 * what locks they need, which you have no control over.
3016 *
3017 * In most situations flushing the entire workqueue is overkill; you merely
3018 * need to know that a particular work item isn't queued and isn't running.
3019 * In such cases you should use cancel_delayed_work_sync() or
3020 * cancel_work_sync() instead.
3021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022void flush_scheduled_work(void)
3023{
Tejun Heod320c032010-06-29 10:07:14 +02003024 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
Dave Jonesae90dd52006-06-30 01:40:45 -04003026EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003029 * execute_in_process_context - reliably execute the routine with user context
3030 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003031 * @ew: guaranteed storage for the execute work structure (must
3032 * be available when the work executes)
3033 *
3034 * Executes the function immediately if process context is available,
3035 * otherwise schedules the function for delayed execution.
3036 *
3037 * Returns: 0 - function was executed
3038 * 1 - function was scheduled for execution
3039 */
David Howells65f27f32006-11-22 14:55:48 +00003040int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003041{
3042 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003043 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003044 return 0;
3045 }
3046
David Howells65f27f32006-11-22 14:55:48 +00003047 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003048 schedule_work(&ew->work);
3049
3050 return 1;
3051}
3052EXPORT_SYMBOL_GPL(execute_in_process_context);
3053
Tejun Heo226223a2013-03-12 11:30:05 -07003054#ifdef CONFIG_SYSFS
3055/*
3056 * Workqueues with WQ_SYSFS flag set is visible to userland via
3057 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3058 * following attributes.
3059 *
3060 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3061 * max_active RW int : maximum number of in-flight work items
3062 *
3063 * Unbound workqueues have the following extra attributes.
3064 *
3065 * id RO int : the associated pool ID
3066 * nice RW int : nice value of the workers
3067 * cpumask RW mask : bitmask of allowed CPUs for the workers
3068 */
3069struct wq_device {
3070 struct workqueue_struct *wq;
3071 struct device dev;
3072};
3073
3074static struct workqueue_struct *dev_to_wq(struct device *dev)
3075{
3076 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3077
3078 return wq_dev->wq;
3079}
3080
3081static ssize_t wq_per_cpu_show(struct device *dev,
3082 struct device_attribute *attr, char *buf)
3083{
3084 struct workqueue_struct *wq = dev_to_wq(dev);
3085
3086 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3087}
3088
3089static ssize_t wq_max_active_show(struct device *dev,
3090 struct device_attribute *attr, char *buf)
3091{
3092 struct workqueue_struct *wq = dev_to_wq(dev);
3093
3094 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3095}
3096
3097static ssize_t wq_max_active_store(struct device *dev,
3098 struct device_attribute *attr,
3099 const char *buf, size_t count)
3100{
3101 struct workqueue_struct *wq = dev_to_wq(dev);
3102 int val;
3103
3104 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3105 return -EINVAL;
3106
3107 workqueue_set_max_active(wq, val);
3108 return count;
3109}
3110
3111static struct device_attribute wq_sysfs_attrs[] = {
3112 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3113 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3114 __ATTR_NULL,
3115};
3116
3117static ssize_t wq_pool_id_show(struct device *dev,
3118 struct device_attribute *attr, char *buf)
3119{
3120 struct workqueue_struct *wq = dev_to_wq(dev);
3121 struct worker_pool *pool;
3122 int written;
3123
3124 rcu_read_lock_sched();
3125 pool = first_pwq(wq)->pool;
3126 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3127 rcu_read_unlock_sched();
3128
3129 return written;
3130}
3131
3132static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3133 char *buf)
3134{
3135 struct workqueue_struct *wq = dev_to_wq(dev);
3136 int written;
3137
Tejun Heo6029a912013-04-01 11:23:34 -07003138 mutex_lock(&wq->mutex);
3139 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3140 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003141
3142 return written;
3143}
3144
3145/* prepare workqueue_attrs for sysfs store operations */
3146static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3147{
3148 struct workqueue_attrs *attrs;
3149
3150 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3151 if (!attrs)
3152 return NULL;
3153
Tejun Heo6029a912013-04-01 11:23:34 -07003154 mutex_lock(&wq->mutex);
3155 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3156 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003157 return attrs;
3158}
3159
3160static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3161 const char *buf, size_t count)
3162{
3163 struct workqueue_struct *wq = dev_to_wq(dev);
3164 struct workqueue_attrs *attrs;
3165 int ret;
3166
3167 attrs = wq_sysfs_prep_attrs(wq);
3168 if (!attrs)
3169 return -ENOMEM;
3170
3171 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3172 attrs->nice >= -20 && attrs->nice <= 19)
3173 ret = apply_workqueue_attrs(wq, attrs);
3174 else
3175 ret = -EINVAL;
3176
3177 free_workqueue_attrs(attrs);
3178 return ret ?: count;
3179}
3180
3181static ssize_t wq_cpumask_show(struct device *dev,
3182 struct device_attribute *attr, char *buf)
3183{
3184 struct workqueue_struct *wq = dev_to_wq(dev);
3185 int written;
3186
Tejun Heo6029a912013-04-01 11:23:34 -07003187 mutex_lock(&wq->mutex);
3188 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3189 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003190
3191 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3192 return written;
3193}
3194
3195static ssize_t wq_cpumask_store(struct device *dev,
3196 struct device_attribute *attr,
3197 const char *buf, size_t count)
3198{
3199 struct workqueue_struct *wq = dev_to_wq(dev);
3200 struct workqueue_attrs *attrs;
3201 int ret;
3202
3203 attrs = wq_sysfs_prep_attrs(wq);
3204 if (!attrs)
3205 return -ENOMEM;
3206
3207 ret = cpumask_parse(buf, attrs->cpumask);
3208 if (!ret)
3209 ret = apply_workqueue_attrs(wq, attrs);
3210
3211 free_workqueue_attrs(attrs);
3212 return ret ?: count;
3213}
3214
3215static struct device_attribute wq_sysfs_unbound_attrs[] = {
3216 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3217 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3218 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3219 __ATTR_NULL,
3220};
3221
3222static struct bus_type wq_subsys = {
3223 .name = "workqueue",
3224 .dev_attrs = wq_sysfs_attrs,
3225};
3226
3227static int __init wq_sysfs_init(void)
3228{
3229 return subsys_virtual_register(&wq_subsys, NULL);
3230}
3231core_initcall(wq_sysfs_init);
3232
3233static void wq_device_release(struct device *dev)
3234{
3235 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3236
3237 kfree(wq_dev);
3238}
3239
3240/**
3241 * workqueue_sysfs_register - make a workqueue visible in sysfs
3242 * @wq: the workqueue to register
3243 *
3244 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3245 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3246 * which is the preferred method.
3247 *
3248 * Workqueue user should use this function directly iff it wants to apply
3249 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3250 * apply_workqueue_attrs() may race against userland updating the
3251 * attributes.
3252 *
3253 * Returns 0 on success, -errno on failure.
3254 */
3255int workqueue_sysfs_register(struct workqueue_struct *wq)
3256{
3257 struct wq_device *wq_dev;
3258 int ret;
3259
3260 /*
3261 * Adjusting max_active or creating new pwqs by applyting
3262 * attributes breaks ordering guarantee. Disallow exposing ordered
3263 * workqueues.
3264 */
3265 if (WARN_ON(wq->flags & __WQ_ORDERED))
3266 return -EINVAL;
3267
3268 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3269 if (!wq_dev)
3270 return -ENOMEM;
3271
3272 wq_dev->wq = wq;
3273 wq_dev->dev.bus = &wq_subsys;
3274 wq_dev->dev.init_name = wq->name;
3275 wq_dev->dev.release = wq_device_release;
3276
3277 /*
3278 * unbound_attrs are created separately. Suppress uevent until
3279 * everything is ready.
3280 */
3281 dev_set_uevent_suppress(&wq_dev->dev, true);
3282
3283 ret = device_register(&wq_dev->dev);
3284 if (ret) {
3285 kfree(wq_dev);
3286 wq->wq_dev = NULL;
3287 return ret;
3288 }
3289
3290 if (wq->flags & WQ_UNBOUND) {
3291 struct device_attribute *attr;
3292
3293 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3294 ret = device_create_file(&wq_dev->dev, attr);
3295 if (ret) {
3296 device_unregister(&wq_dev->dev);
3297 wq->wq_dev = NULL;
3298 return ret;
3299 }
3300 }
3301 }
3302
3303 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3304 return 0;
3305}
3306
3307/**
3308 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3309 * @wq: the workqueue to unregister
3310 *
3311 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3312 */
3313static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3314{
3315 struct wq_device *wq_dev = wq->wq_dev;
3316
3317 if (!wq->wq_dev)
3318 return;
3319
3320 wq->wq_dev = NULL;
3321 device_unregister(&wq_dev->dev);
3322}
3323#else /* CONFIG_SYSFS */
3324static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3325#endif /* CONFIG_SYSFS */
3326
Tejun Heo7a4e3442013-03-12 11:30:00 -07003327/**
3328 * free_workqueue_attrs - free a workqueue_attrs
3329 * @attrs: workqueue_attrs to free
3330 *
3331 * Undo alloc_workqueue_attrs().
3332 */
3333void free_workqueue_attrs(struct workqueue_attrs *attrs)
3334{
3335 if (attrs) {
3336 free_cpumask_var(attrs->cpumask);
3337 kfree(attrs);
3338 }
3339}
3340
3341/**
3342 * alloc_workqueue_attrs - allocate a workqueue_attrs
3343 * @gfp_mask: allocation mask to use
3344 *
3345 * Allocate a new workqueue_attrs, initialize with default settings and
3346 * return it. Returns NULL on failure.
3347 */
3348struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3349{
3350 struct workqueue_attrs *attrs;
3351
3352 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3353 if (!attrs)
3354 goto fail;
3355 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3356 goto fail;
3357
Tejun Heo13e2e552013-04-01 11:23:31 -07003358 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003359 return attrs;
3360fail:
3361 free_workqueue_attrs(attrs);
3362 return NULL;
3363}
3364
Tejun Heo29c91e92013-03-12 11:30:03 -07003365static void copy_workqueue_attrs(struct workqueue_attrs *to,
3366 const struct workqueue_attrs *from)
3367{
3368 to->nice = from->nice;
3369 cpumask_copy(to->cpumask, from->cpumask);
3370}
3371
Tejun Heo29c91e92013-03-12 11:30:03 -07003372/* hash value of the content of @attr */
3373static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3374{
3375 u32 hash = 0;
3376
3377 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003378 hash = jhash(cpumask_bits(attrs->cpumask),
3379 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003380 return hash;
3381}
3382
3383/* content equality test */
3384static bool wqattrs_equal(const struct workqueue_attrs *a,
3385 const struct workqueue_attrs *b)
3386{
3387 if (a->nice != b->nice)
3388 return false;
3389 if (!cpumask_equal(a->cpumask, b->cpumask))
3390 return false;
3391 return true;
3392}
3393
Tejun Heo7a4e3442013-03-12 11:30:00 -07003394/**
3395 * init_worker_pool - initialize a newly zalloc'd worker_pool
3396 * @pool: worker_pool to initialize
3397 *
3398 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003399 * Returns 0 on success, -errno on failure. Even on failure, all fields
3400 * inside @pool proper are initialized and put_unbound_pool() can be called
3401 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003402 */
3403static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003404{
3405 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003406 pool->id = -1;
3407 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003408 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003409 pool->flags |= POOL_DISASSOCIATED;
3410 INIT_LIST_HEAD(&pool->worklist);
3411 INIT_LIST_HEAD(&pool->idle_list);
3412 hash_init(pool->busy_hash);
3413
3414 init_timer_deferrable(&pool->idle_timer);
3415 pool->idle_timer.function = idle_worker_timeout;
3416 pool->idle_timer.data = (unsigned long)pool;
3417
3418 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3419 (unsigned long)pool);
3420
3421 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003422 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003423 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003424
Tejun Heo29c91e92013-03-12 11:30:03 -07003425 INIT_HLIST_NODE(&pool->hash_node);
3426 pool->refcnt = 1;
3427
3428 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003429 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3430 if (!pool->attrs)
3431 return -ENOMEM;
3432 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003433}
3434
Tejun Heo29c91e92013-03-12 11:30:03 -07003435static void rcu_free_pool(struct rcu_head *rcu)
3436{
3437 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3438
Tejun Heo822d8402013-03-19 13:45:21 -07003439 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003440 free_workqueue_attrs(pool->attrs);
3441 kfree(pool);
3442}
3443
3444/**
3445 * put_unbound_pool - put a worker_pool
3446 * @pool: worker_pool to put
3447 *
3448 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003449 * safe manner. get_unbound_pool() calls this function on its failure path
3450 * and this function should be able to release pools which went through,
3451 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003452 *
3453 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003454 */
3455static void put_unbound_pool(struct worker_pool *pool)
3456{
3457 struct worker *worker;
3458
Tejun Heoa892cac2013-04-01 11:23:32 -07003459 lockdep_assert_held(&wq_pool_mutex);
3460
3461 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003462 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003463
3464 /* sanity checks */
3465 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003466 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003467 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003468
3469 /* release id and unhash */
3470 if (pool->id >= 0)
3471 idr_remove(&worker_pool_idr, pool->id);
3472 hash_del(&pool->hash_node);
3473
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003474 /*
3475 * Become the manager and destroy all workers. Grabbing
3476 * manager_arb prevents @pool's workers from blocking on
3477 * manager_mutex.
3478 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003479 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003480 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003481 spin_lock_irq(&pool->lock);
3482
3483 while ((worker = first_worker(pool)))
3484 destroy_worker(worker);
3485 WARN_ON(pool->nr_workers || pool->nr_idle);
3486
3487 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003488 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003489 mutex_unlock(&pool->manager_arb);
3490
3491 /* shut down the timers */
3492 del_timer_sync(&pool->idle_timer);
3493 del_timer_sync(&pool->mayday_timer);
3494
3495 /* sched-RCU protected to allow dereferences from get_work_pool() */
3496 call_rcu_sched(&pool->rcu, rcu_free_pool);
3497}
3498
3499/**
3500 * get_unbound_pool - get a worker_pool with the specified attributes
3501 * @attrs: the attributes of the worker_pool to get
3502 *
3503 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3504 * reference count and return it. If there already is a matching
3505 * worker_pool, it will be used; otherwise, this function attempts to
3506 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003507 *
3508 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003509 */
3510static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3511{
Tejun Heo29c91e92013-03-12 11:30:03 -07003512 u32 hash = wqattrs_hash(attrs);
3513 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003514 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003515
Tejun Heoa892cac2013-04-01 11:23:32 -07003516 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003517
3518 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003519 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3520 if (wqattrs_equal(pool->attrs, attrs)) {
3521 pool->refcnt++;
3522 goto out_unlock;
3523 }
3524 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003525
3526 /* nope, create a new one */
3527 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3528 if (!pool || init_worker_pool(pool) < 0)
3529 goto fail;
3530
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003531 if (workqueue_freezing)
3532 pool->flags |= POOL_FREEZING;
3533
Tejun Heo8864b4e2013-03-12 11:30:04 -07003534 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003535 copy_workqueue_attrs(pool->attrs, attrs);
3536
Tejun Heof3f90ad2013-04-01 11:23:34 -07003537 /* if cpumask is contained inside a NUMA node, we belong to that node */
3538 if (wq_numa_enabled) {
3539 for_each_node(node) {
3540 if (cpumask_subset(pool->attrs->cpumask,
3541 wq_numa_possible_cpumask[node])) {
3542 pool->node = node;
3543 break;
3544 }
3545 }
3546 }
3547
Tejun Heo29c91e92013-03-12 11:30:03 -07003548 if (worker_pool_assign_id(pool) < 0)
3549 goto fail;
3550
3551 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003552 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003553 goto fail;
3554
Tejun Heo29c91e92013-03-12 11:30:03 -07003555 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003556 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3557out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003558 return pool;
3559fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003560 if (pool)
3561 put_unbound_pool(pool);
3562 return NULL;
3563}
3564
Tejun Heo8864b4e2013-03-12 11:30:04 -07003565static void rcu_free_pwq(struct rcu_head *rcu)
3566{
3567 kmem_cache_free(pwq_cache,
3568 container_of(rcu, struct pool_workqueue, rcu));
3569}
3570
3571/*
3572 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3573 * and needs to be destroyed.
3574 */
3575static void pwq_unbound_release_workfn(struct work_struct *work)
3576{
3577 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3578 unbound_release_work);
3579 struct workqueue_struct *wq = pwq->wq;
3580 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003581 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003582
3583 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3584 return;
3585
Tejun Heo75ccf592013-03-12 11:30:04 -07003586 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003587 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003588 * necessary on release but do it anyway. It's easier to verify
3589 * and consistent with the linking path.
3590 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003591 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003592 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003593 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003594 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003595
Tejun Heoa892cac2013-04-01 11:23:32 -07003596 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003597 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003598 mutex_unlock(&wq_pool_mutex);
3599
Tejun Heo8864b4e2013-03-12 11:30:04 -07003600 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3601
3602 /*
3603 * If we're the last pwq going away, @wq is already dead and no one
3604 * is gonna access it anymore. Free it.
3605 */
Tejun Heo6029a912013-04-01 11:23:34 -07003606 if (is_last) {
3607 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003608 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003609 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003610}
3611
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003612/**
Tejun Heo699ce092013-03-13 16:51:35 -07003613 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003614 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003615 *
Tejun Heo699ce092013-03-13 16:51:35 -07003616 * If @pwq isn't freezing, set @pwq->max_active to the associated
3617 * workqueue's saved_max_active and activate delayed work items
3618 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003619 */
Tejun Heo699ce092013-03-13 16:51:35 -07003620static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003621{
Tejun Heo699ce092013-03-13 16:51:35 -07003622 struct workqueue_struct *wq = pwq->wq;
3623 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003624
Tejun Heo699ce092013-03-13 16:51:35 -07003625 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003626 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003627
3628 /* fast exit for non-freezable wqs */
3629 if (!freezable && pwq->max_active == wq->saved_max_active)
3630 return;
3631
Lai Jiangshana357fc02013-03-25 16:57:19 -07003632 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003633
3634 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3635 pwq->max_active = wq->saved_max_active;
3636
3637 while (!list_empty(&pwq->delayed_works) &&
3638 pwq->nr_active < pwq->max_active)
3639 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003640
3641 /*
3642 * Need to kick a worker after thawed or an unbound wq's
3643 * max_active is bumped. It's a slow path. Do it always.
3644 */
3645 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003646 } else {
3647 pwq->max_active = 0;
3648 }
3649
Lai Jiangshana357fc02013-03-25 16:57:19 -07003650 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003651}
3652
Tejun Heoe50aba92013-04-01 11:23:35 -07003653/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003654static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3655 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003656{
3657 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3658
Tejun Heoe50aba92013-04-01 11:23:35 -07003659 memset(pwq, 0, sizeof(*pwq));
3660
Tejun Heod2c1d402013-03-12 11:30:04 -07003661 pwq->pool = pool;
3662 pwq->wq = wq;
3663 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003664 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003665 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003666 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003667 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003668 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003669}
Tejun Heod2c1d402013-03-12 11:30:04 -07003670
Tejun Heof147f292013-04-01 11:23:35 -07003671/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003672static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003673{
3674 struct workqueue_struct *wq = pwq->wq;
3675
3676 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003677
Tejun Heo1befcf32013-04-01 11:23:35 -07003678 /* may be called multiple times, ignore if already linked */
3679 if (!list_empty(&pwq->pwqs_node))
3680 return;
3681
Tejun Heo983ca252013-03-13 16:51:35 -07003682 /*
3683 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003684 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003685 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003686 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003687
3688 /* sync max_active to the current setting */
3689 pwq_adjust_max_active(pwq);
3690
3691 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003692 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003693}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003694
Tejun Heof147f292013-04-01 11:23:35 -07003695/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3696static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3697 const struct workqueue_attrs *attrs)
3698{
3699 struct worker_pool *pool;
3700 struct pool_workqueue *pwq;
3701
3702 lockdep_assert_held(&wq_pool_mutex);
3703
3704 pool = get_unbound_pool(attrs);
3705 if (!pool)
3706 return NULL;
3707
Tejun Heoe50aba92013-04-01 11:23:35 -07003708 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003709 if (!pwq) {
3710 put_unbound_pool(pool);
3711 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003712 }
Tejun Heo6029a912013-04-01 11:23:34 -07003713
Tejun Heof147f292013-04-01 11:23:35 -07003714 init_pwq(pwq, wq, pool);
3715 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003716}
3717
Tejun Heo4c16bd32013-04-01 11:23:36 -07003718/* undo alloc_unbound_pwq(), used only in the error path */
3719static void free_unbound_pwq(struct pool_workqueue *pwq)
3720{
3721 lockdep_assert_held(&wq_pool_mutex);
3722
3723 if (pwq) {
3724 put_unbound_pool(pwq->pool);
3725 kfree(pwq);
3726 }
3727}
3728
3729/**
3730 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3731 * @attrs: the wq_attrs of interest
3732 * @node: the target NUMA node
3733 * @cpu_going_down: if >= 0, the CPU to consider as offline
3734 * @cpumask: outarg, the resulting cpumask
3735 *
3736 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3737 * @cpu_going_down is >= 0, that cpu is considered offline during
3738 * calculation. The result is stored in @cpumask. This function returns
3739 * %true if the resulting @cpumask is different from @attrs->cpumask,
3740 * %false if equal.
3741 *
3742 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3743 * enabled and @node has online CPUs requested by @attrs, the returned
3744 * cpumask is the intersection of the possible CPUs of @node and
3745 * @attrs->cpumask.
3746 *
3747 * The caller is responsible for ensuring that the cpumask of @node stays
3748 * stable.
3749 */
3750static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3751 int cpu_going_down, cpumask_t *cpumask)
3752{
3753 if (!wq_numa_enabled)
3754 goto use_dfl;
3755
3756 /* does @node have any online CPUs @attrs wants? */
3757 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3758 if (cpu_going_down >= 0)
3759 cpumask_clear_cpu(cpu_going_down, cpumask);
3760
3761 if (cpumask_empty(cpumask))
3762 goto use_dfl;
3763
3764 /* yeap, return possible CPUs in @node that @attrs wants */
3765 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3766 return !cpumask_equal(cpumask, attrs->cpumask);
3767
3768use_dfl:
3769 cpumask_copy(cpumask, attrs->cpumask);
3770 return false;
3771}
3772
Tejun Heo1befcf32013-04-01 11:23:35 -07003773/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3774static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3775 int node,
3776 struct pool_workqueue *pwq)
3777{
3778 struct pool_workqueue *old_pwq;
3779
3780 lockdep_assert_held(&wq->mutex);
3781
3782 /* link_pwq() can handle duplicate calls */
3783 link_pwq(pwq);
3784
3785 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3786 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3787 return old_pwq;
3788}
3789
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003790/**
3791 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3792 * @wq: the target workqueue
3793 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3794 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003795 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3796 * machines, this function maps a separate pwq to each NUMA node with
3797 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3798 * NUMA node it was issued on. Older pwqs are released as in-flight work
3799 * items finish. Note that a work item which repeatedly requeues itself
3800 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003801 *
3802 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3803 * failure.
3804 */
3805int apply_workqueue_attrs(struct workqueue_struct *wq,
3806 const struct workqueue_attrs *attrs)
3807{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003808 struct workqueue_attrs *new_attrs, *tmp_attrs;
3809 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003810 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003811
Tejun Heo8719dce2013-03-12 11:30:04 -07003812 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003813 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3814 return -EINVAL;
3815
Tejun Heo8719dce2013-03-12 11:30:04 -07003816 /* creating multiple pwqs breaks ordering guarantee */
3817 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3818 return -EINVAL;
3819
Tejun Heo4c16bd32013-04-01 11:23:36 -07003820 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003821 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003822 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3823 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003824 goto enomem;
3825
Tejun Heo4c16bd32013-04-01 11:23:36 -07003826 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003827 copy_workqueue_attrs(new_attrs, attrs);
3828 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3829
Tejun Heo4c16bd32013-04-01 11:23:36 -07003830 /*
3831 * We may create multiple pwqs with differing cpumasks. Make a
3832 * copy of @new_attrs which will be modified and used to obtain
3833 * pools.
3834 */
3835 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003836
Tejun Heo4c16bd32013-04-01 11:23:36 -07003837 /*
3838 * CPUs should stay stable across pwq creations and installations.
3839 * Pin CPUs, determine the target cpumask for each node and create
3840 * pwqs accordingly.
3841 */
3842 get_online_cpus();
3843
3844 mutex_lock(&wq_pool_mutex);
3845
3846 /*
3847 * If something goes wrong during CPU up/down, we'll fall back to
3848 * the default pwq covering whole @attrs->cpumask. Always create
3849 * it even if we don't use it immediately.
3850 */
3851 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3852 if (!dfl_pwq)
3853 goto enomem_pwq;
3854
3855 for_each_node(node) {
3856 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3857 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3858 if (!pwq_tbl[node])
3859 goto enomem_pwq;
3860 } else {
3861 dfl_pwq->refcnt++;
3862 pwq_tbl[node] = dfl_pwq;
3863 }
3864 }
3865
3866 mutex_unlock(&wq_pool_mutex);
3867
3868 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003869 mutex_lock(&wq->mutex);
3870
Tejun Heof147f292013-04-01 11:23:35 -07003871 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003872
3873 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003874 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003875 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3876
3877 /* @dfl_pwq might not have been used, ensure it's linked */
3878 link_pwq(dfl_pwq);
3879 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003880
3881 mutex_unlock(&wq->mutex);
3882
Tejun Heo4c16bd32013-04-01 11:23:36 -07003883 /* put the old pwqs */
3884 for_each_node(node)
3885 put_pwq_unlocked(pwq_tbl[node]);
3886 put_pwq_unlocked(dfl_pwq);
3887
3888 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003889 ret = 0;
3890 /* fall through */
3891out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003892 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003893 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003894 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003895 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003896
Tejun Heo4c16bd32013-04-01 11:23:36 -07003897enomem_pwq:
3898 free_unbound_pwq(dfl_pwq);
3899 for_each_node(node)
3900 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3901 free_unbound_pwq(pwq_tbl[node]);
3902 mutex_unlock(&wq_pool_mutex);
3903 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003904enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003905 ret = -ENOMEM;
3906 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003907}
3908
Tejun Heo4c16bd32013-04-01 11:23:36 -07003909/**
3910 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3911 * @wq: the target workqueue
3912 * @cpu: the CPU coming up or going down
3913 * @online: whether @cpu is coming up or going down
3914 *
3915 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3916 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3917 * @wq accordingly.
3918 *
3919 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3920 * falls back to @wq->dfl_pwq which may not be optimal but is always
3921 * correct.
3922 *
3923 * Note that when the last allowed CPU of a NUMA node goes offline for a
3924 * workqueue with a cpumask spanning multiple nodes, the workers which were
3925 * already executing the work items for the workqueue will lose their CPU
3926 * affinity and may execute on any CPU. This is similar to how per-cpu
3927 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3928 * affinity, it's the user's responsibility to flush the work item from
3929 * CPU_DOWN_PREPARE.
3930 */
3931static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3932 bool online)
3933{
3934 int node = cpu_to_node(cpu);
3935 int cpu_off = online ? -1 : cpu;
3936 struct pool_workqueue *old_pwq = NULL, *pwq;
3937 struct workqueue_attrs *target_attrs;
3938 cpumask_t *cpumask;
3939
3940 lockdep_assert_held(&wq_pool_mutex);
3941
3942 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3943 return;
3944
3945 /*
3946 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3947 * Let's use a preallocated one. The following buf is protected by
3948 * CPU hotplug exclusion.
3949 */
3950 target_attrs = wq_update_unbound_numa_attrs_buf;
3951 cpumask = target_attrs->cpumask;
3952
3953 mutex_lock(&wq->mutex);
3954
3955 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3956 pwq = unbound_pwq_by_node(wq, node);
3957
3958 /*
3959 * Let's determine what needs to be done. If the target cpumask is
3960 * different from wq's, we need to compare it to @pwq's and create
3961 * a new one if they don't match. If the target cpumask equals
3962 * wq's, the default pwq should be used. If @pwq is already the
3963 * default one, nothing to do; otherwise, install the default one.
3964 */
3965 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3966 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
3967 goto out_unlock;
3968 } else {
3969 if (pwq == wq->dfl_pwq)
3970 goto out_unlock;
3971 else
3972 goto use_dfl_pwq;
3973 }
3974
3975 mutex_unlock(&wq->mutex);
3976
3977 /* create a new pwq */
3978 pwq = alloc_unbound_pwq(wq, target_attrs);
3979 if (!pwq) {
3980 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
3981 wq->name);
3982 goto out_unlock;
3983 }
3984
3985 /*
3986 * Install the new pwq. As this function is called only from CPU
3987 * hotplug callbacks and applying a new attrs is wrapped with
3988 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
3989 * inbetween.
3990 */
3991 mutex_lock(&wq->mutex);
3992 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
3993 goto out_unlock;
3994
3995use_dfl_pwq:
3996 spin_lock_irq(&wq->dfl_pwq->pool->lock);
3997 get_pwq(wq->dfl_pwq);
3998 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
3999 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4000out_unlock:
4001 mutex_unlock(&wq->mutex);
4002 put_pwq_unlocked(old_pwq);
4003}
4004
Tejun Heo30cdf242013-03-12 11:29:57 -07004005static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004007 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07004008 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004009
Tejun Heo30cdf242013-03-12 11:29:57 -07004010 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004011 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4012 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004013 return -ENOMEM;
4014
4015 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004016 struct pool_workqueue *pwq =
4017 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004018 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004019 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004020
Tejun Heof147f292013-04-01 11:23:35 -07004021 init_pwq(pwq, wq, &cpu_pools[highpri]);
4022
4023 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004024 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004025 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004026 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004027 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07004028 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004029 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004030 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004031}
4032
Tejun Heof3421792010-07-02 10:03:51 +02004033static int wq_clamp_max_active(int max_active, unsigned int flags,
4034 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004035{
Tejun Heof3421792010-07-02 10:03:51 +02004036 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4037
4038 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004039 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4040 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004041
Tejun Heof3421792010-07-02 10:03:51 +02004042 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004043}
4044
Tejun Heob196be82012-01-10 15:11:35 -08004045struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004046 unsigned int flags,
4047 int max_active,
4048 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004049 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004050{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004051 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004052 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004053 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004054 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004055
Tejun Heoecf68812013-04-01 11:23:34 -07004056 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004057 if (flags & WQ_UNBOUND)
4058 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4059
4060 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004061 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004062 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004063
Tejun Heo6029a912013-04-01 11:23:34 -07004064 if (flags & WQ_UNBOUND) {
4065 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4066 if (!wq->unbound_attrs)
4067 goto err_free_wq;
4068 }
4069
Tejun Heoecf68812013-04-01 11:23:34 -07004070 va_start(args, lock_name);
4071 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004072 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004073
Tejun Heod320c032010-06-29 10:07:14 +02004074 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004075 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004076
Tejun Heob196be82012-01-10 15:11:35 -08004077 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004078 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004079 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004080 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004081 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004082 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004083 INIT_LIST_HEAD(&wq->flusher_queue);
4084 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004085 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004086
Johannes Bergeb13ba82008-01-16 09:51:58 +01004087 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004088 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004089
Tejun Heo30cdf242013-03-12 11:29:57 -07004090 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004091 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004092
Tejun Heo493008a2013-03-12 11:30:03 -07004093 /*
4094 * Workqueues which may be used during memory reclaim should
4095 * have a rescuer to guarantee forward progress.
4096 */
4097 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004098 struct worker *rescuer;
4099
Tejun Heod2c1d402013-03-12 11:30:04 -07004100 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004101 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004102 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004103
Tejun Heo111c2252013-01-17 17:16:24 -08004104 rescuer->rescue_wq = wq;
4105 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004106 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004107 if (IS_ERR(rescuer->task)) {
4108 kfree(rescuer);
4109 goto err_destroy;
4110 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004111
Tejun Heod2c1d402013-03-12 11:30:04 -07004112 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004113 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004114 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004115 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004116
Tejun Heo226223a2013-03-12 11:30:05 -07004117 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4118 goto err_destroy;
4119
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004120 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004121 * wq_pool_mutex protects global freeze state and workqueues list.
4122 * Grab it, adjust max_active and add the new @wq to workqueues
4123 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004124 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004125 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004126
Lai Jiangshana357fc02013-03-25 16:57:19 -07004127 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004128 for_each_pwq(pwq, wq)
4129 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004130 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004131
Tejun Heo15376632010-06-29 10:07:11 +02004132 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004133
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004134 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004135
Oleg Nesterov3af244332007-05-09 02:34:09 -07004136 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004137
4138err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004139 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004140 kfree(wq);
4141 return NULL;
4142err_destroy:
4143 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004144 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004145}
Tejun Heod320c032010-06-29 10:07:14 +02004146EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004147
4148/**
4149 * destroy_workqueue - safely terminate a workqueue
4150 * @wq: target workqueue
4151 *
4152 * Safely destroy a workqueue. All work currently pending will be done first.
4153 */
4154void destroy_workqueue(struct workqueue_struct *wq)
4155{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004156 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004157 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004158
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004159 /* drain it before proceeding with destruction */
4160 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004161
Tejun Heo6183c002013-03-12 11:29:57 -07004162 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004163 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004164 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004165 int i;
4166
Tejun Heo76af4d92013-03-12 11:30:00 -07004167 for (i = 0; i < WORK_NR_COLORS; i++) {
4168 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004169 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004170 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004171 }
4172 }
4173
Tejun Heo8864b4e2013-03-12 11:30:04 -07004174 if (WARN_ON(pwq->refcnt > 1) ||
4175 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004176 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004177 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004178 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004179 }
Tejun Heo6183c002013-03-12 11:29:57 -07004180 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004181 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004182
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004183 /*
4184 * wq list is used to freeze wq, remove from list after
4185 * flushing is complete in case freeze races us.
4186 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004187 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004188 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004189 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004190
Tejun Heo226223a2013-03-12 11:30:05 -07004191 workqueue_sysfs_unregister(wq);
4192
Tejun Heo493008a2013-03-12 11:30:03 -07004193 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004194 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004195 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004196 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004197 }
4198
Tejun Heo8864b4e2013-03-12 11:30:04 -07004199 if (!(wq->flags & WQ_UNBOUND)) {
4200 /*
4201 * The base ref is never dropped on per-cpu pwqs. Directly
4202 * free the pwqs and wq.
4203 */
4204 free_percpu(wq->cpu_pwqs);
4205 kfree(wq);
4206 } else {
4207 /*
4208 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004209 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4210 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004211 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004212 for_each_node(node) {
4213 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4214 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4215 put_pwq_unlocked(pwq);
4216 }
4217
4218 /*
4219 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4220 * put. Don't access it afterwards.
4221 */
4222 pwq = wq->dfl_pwq;
4223 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004224 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004225 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004226}
4227EXPORT_SYMBOL_GPL(destroy_workqueue);
4228
Tejun Heodcd989c2010-06-29 10:07:14 +02004229/**
4230 * workqueue_set_max_active - adjust max_active of a workqueue
4231 * @wq: target workqueue
4232 * @max_active: new max_active value.
4233 *
4234 * Set max_active of @wq to @max_active.
4235 *
4236 * CONTEXT:
4237 * Don't call from IRQ context.
4238 */
4239void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4240{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004241 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004242
Tejun Heo8719dce2013-03-12 11:30:04 -07004243 /* disallow meddling with max_active for ordered workqueues */
4244 if (WARN_ON(wq->flags & __WQ_ORDERED))
4245 return;
4246
Tejun Heof3421792010-07-02 10:03:51 +02004247 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004248
Lai Jiangshana357fc02013-03-25 16:57:19 -07004249 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004250
4251 wq->saved_max_active = max_active;
4252
Tejun Heo699ce092013-03-13 16:51:35 -07004253 for_each_pwq(pwq, wq)
4254 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004255
Lai Jiangshana357fc02013-03-25 16:57:19 -07004256 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004257}
4258EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4259
4260/**
Tejun Heoe6267612013-03-12 17:41:37 -07004261 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4262 *
4263 * Determine whether %current is a workqueue rescuer. Can be used from
4264 * work functions to determine whether it's being run off the rescuer task.
4265 */
4266bool current_is_workqueue_rescuer(void)
4267{
4268 struct worker *worker = current_wq_worker();
4269
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004270 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07004271}
4272
4273/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004274 * workqueue_congested - test whether a workqueue is congested
4275 * @cpu: CPU in question
4276 * @wq: target workqueue
4277 *
4278 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4279 * no synchronization around this function and the test result is
4280 * unreliable and only useful as advisory hints or for debugging.
4281 *
4282 * RETURNS:
4283 * %true if congested, %false otherwise.
4284 */
Tejun Heod84ff052013-03-12 11:29:59 -07004285bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004286{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004287 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004288 bool ret;
4289
Lai Jiangshan88109452013-03-20 03:28:10 +08004290 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004291
4292 if (!(wq->flags & WQ_UNBOUND))
4293 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4294 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004295 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004296
Tejun Heo76af4d92013-03-12 11:30:00 -07004297 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004298 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004299
4300 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004301}
4302EXPORT_SYMBOL_GPL(workqueue_congested);
4303
4304/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004305 * work_busy - test whether a work is currently pending or running
4306 * @work: the work to be tested
4307 *
4308 * Test whether @work is currently pending or running. There is no
4309 * synchronization around this function and the test result is
4310 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004311 *
4312 * RETURNS:
4313 * OR'd bitmask of WORK_BUSY_* bits.
4314 */
4315unsigned int work_busy(struct work_struct *work)
4316{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004317 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004318 unsigned long flags;
4319 unsigned int ret = 0;
4320
Tejun Heodcd989c2010-06-29 10:07:14 +02004321 if (work_pending(work))
4322 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004323
Tejun Heofa1b54e2013-03-12 11:30:00 -07004324 local_irq_save(flags);
4325 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004326 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004327 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004328 if (find_worker_executing_work(pool, work))
4329 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004330 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004331 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004332 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004333
4334 return ret;
4335}
4336EXPORT_SYMBOL_GPL(work_busy);
4337
Tejun Heodb7bccf2010-06-29 10:07:12 +02004338/*
4339 * CPU hotplug.
4340 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004341 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004342 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004343 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004344 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004345 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004346 * blocked draining impractical.
4347 *
Tejun Heo24647572013-01-24 11:01:33 -08004348 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004349 * running as an unbound one and allowing it to be reattached later if the
4350 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004351 */
4352
Tejun Heo706026c2013-01-24 11:01:34 -08004353static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004354{
Tejun Heo38db41d2013-01-24 11:01:34 -08004355 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004356 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004357 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004358 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004359
Tejun Heof02ae732013-03-12 11:30:03 -07004360 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004361 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004362
Tejun Heobc3a1af2013-03-13 19:47:39 -07004363 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004364 spin_lock_irq(&pool->lock);
4365
4366 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004367 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004368 * unbound and set DISASSOCIATED. Before this, all workers
4369 * except for the ones which are still executing works from
4370 * before the last CPU down must be on the cpu. After
4371 * this, they may become diasporas.
4372 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004373 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004374 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004375
Tejun Heo24647572013-01-24 11:01:33 -08004376 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004377
Tejun Heo94cf58b2013-01-24 11:01:33 -08004378 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004379 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004380 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004381
4382 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004383 * Call schedule() so that we cross rq->lock and thus can guarantee
4384 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4385 * as scheduler callbacks may be invoked from other cpus.
4386 */
4387 schedule();
4388
4389 /*
4390 * Sched callbacks are disabled now. Zap nr_running. After this,
4391 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004392 * are always true as long as the worklist is not empty. Pools on
4393 * @cpu now behave as unbound (in terms of concurrency management)
4394 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004395 *
4396 * On return from this function, the current worker would trigger
4397 * unbound chain execution of pending work items if other workers
4398 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004399 */
Tejun Heof02ae732013-03-12 11:30:03 -07004400 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004401 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004402}
4403
Tejun Heobd7c0892013-03-19 13:45:21 -07004404/**
4405 * rebind_workers - rebind all workers of a pool to the associated CPU
4406 * @pool: pool of interest
4407 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004408 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004409 */
4410static void rebind_workers(struct worker_pool *pool)
4411{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004412 struct worker *worker;
4413 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004414
4415 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004416
Tejun Heoa9ab7752013-03-19 13:45:21 -07004417 /*
4418 * Restore CPU affinity of all workers. As all idle workers should
4419 * be on the run-queue of the associated CPU before any local
4420 * wake-ups for concurrency management happen, restore CPU affinty
4421 * of all workers first and then clear UNBOUND. As we're called
4422 * from CPU_ONLINE, the following shouldn't fail.
4423 */
4424 for_each_pool_worker(worker, wi, pool)
4425 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4426 pool->attrs->cpumask) < 0);
4427
4428 spin_lock_irq(&pool->lock);
4429
4430 for_each_pool_worker(worker, wi, pool) {
4431 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004432
4433 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004434 * A bound idle worker should actually be on the runqueue
4435 * of the associated CPU for local wake-ups targeting it to
4436 * work. Kick all idle workers so that they migrate to the
4437 * associated CPU. Doing this in the same loop as
4438 * replacing UNBOUND with REBOUND is safe as no worker will
4439 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004440 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004441 if (worker_flags & WORKER_IDLE)
4442 wake_up_process(worker->task);
4443
4444 /*
4445 * We want to clear UNBOUND but can't directly call
4446 * worker_clr_flags() or adjust nr_running. Atomically
4447 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4448 * @worker will clear REBOUND using worker_clr_flags() when
4449 * it initiates the next execution cycle thus restoring
4450 * concurrency management. Note that when or whether
4451 * @worker clears REBOUND doesn't affect correctness.
4452 *
4453 * ACCESS_ONCE() is necessary because @worker->flags may be
4454 * tested without holding any lock in
4455 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4456 * fail incorrectly leading to premature concurrency
4457 * management operations.
4458 */
4459 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4460 worker_flags |= WORKER_REBOUND;
4461 worker_flags &= ~WORKER_UNBOUND;
4462 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004463 }
4464
Tejun Heoa9ab7752013-03-19 13:45:21 -07004465 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004466}
4467
Tejun Heo7dbc7252013-03-19 13:45:21 -07004468/**
4469 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4470 * @pool: unbound pool of interest
4471 * @cpu: the CPU which is coming up
4472 *
4473 * An unbound pool may end up with a cpumask which doesn't have any online
4474 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4475 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4476 * online CPU before, cpus_allowed of all its workers should be restored.
4477 */
4478static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4479{
4480 static cpumask_t cpumask;
4481 struct worker *worker;
4482 int wi;
4483
4484 lockdep_assert_held(&pool->manager_mutex);
4485
4486 /* is @cpu allowed for @pool? */
4487 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4488 return;
4489
4490 /* is @cpu the only online CPU? */
4491 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4492 if (cpumask_weight(&cpumask) != 1)
4493 return;
4494
4495 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4496 for_each_pool_worker(worker, wi, pool)
4497 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4498 pool->attrs->cpumask) < 0);
4499}
4500
Tejun Heo8db25e72012-07-17 12:39:28 -07004501/*
4502 * Workqueues should be brought up before normal priority CPU notifiers.
4503 * This will be registered high priority CPU notifier.
4504 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004505static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004506 unsigned long action,
4507 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004508{
Tejun Heod84ff052013-03-12 11:29:59 -07004509 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004510 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004511 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004512 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Tejun Heo8db25e72012-07-17 12:39:28 -07004514 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004516 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004517 if (pool->nr_workers)
4518 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004519 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004520 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004522 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004523
Tejun Heo65758202012-07-17 12:39:26 -07004524 case CPU_DOWN_FAILED:
4525 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004526 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004527
4528 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004529 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004530
Tejun Heo7dbc7252013-03-19 13:45:21 -07004531 if (pool->cpu == cpu) {
4532 spin_lock_irq(&pool->lock);
4533 pool->flags &= ~POOL_DISASSOCIATED;
4534 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004535
Tejun Heo7dbc7252013-03-19 13:45:21 -07004536 rebind_workers(pool);
4537 } else if (pool->cpu < 0) {
4538 restore_unbound_workers_cpumask(pool, cpu);
4539 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004540
Tejun Heobc3a1af2013-03-13 19:47:39 -07004541 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004542 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004543
Tejun Heo4c16bd32013-04-01 11:23:36 -07004544 /* update NUMA affinity of unbound workqueues */
4545 list_for_each_entry(wq, &workqueues, list)
4546 wq_update_unbound_numa(wq, cpu, true);
4547
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004548 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004549 break;
Tejun Heo65758202012-07-17 12:39:26 -07004550 }
4551 return NOTIFY_OK;
4552}
4553
4554/*
4555 * Workqueues should be brought down after normal priority CPU notifiers.
4556 * This will be registered as low priority CPU notifier.
4557 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004558static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004559 unsigned long action,
4560 void *hcpu)
4561{
Tejun Heod84ff052013-03-12 11:29:59 -07004562 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004563 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004564 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004565
Tejun Heo65758202012-07-17 12:39:26 -07004566 switch (action & ~CPU_TASKS_FROZEN) {
4567 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004568 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004569 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004570 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004571
4572 /* update NUMA affinity of unbound workqueues */
4573 mutex_lock(&wq_pool_mutex);
4574 list_for_each_entry(wq, &workqueues, list)
4575 wq_update_unbound_numa(wq, cpu, false);
4576 mutex_unlock(&wq_pool_mutex);
4577
4578 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004579 flush_work(&unbind_work);
4580 break;
Tejun Heo65758202012-07-17 12:39:26 -07004581 }
4582 return NOTIFY_OK;
4583}
4584
Rusty Russell2d3854a2008-11-05 13:39:10 +11004585#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004586
Rusty Russell2d3854a2008-11-05 13:39:10 +11004587struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004588 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004589 long (*fn)(void *);
4590 void *arg;
4591 long ret;
4592};
4593
Tejun Heoed48ece2012-09-18 12:48:43 -07004594static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004595{
Tejun Heoed48ece2012-09-18 12:48:43 -07004596 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4597
Rusty Russell2d3854a2008-11-05 13:39:10 +11004598 wfc->ret = wfc->fn(wfc->arg);
4599}
4600
4601/**
4602 * work_on_cpu - run a function in user context on a particular cpu
4603 * @cpu: the cpu to run on
4604 * @fn: the function to run
4605 * @arg: the function arg
4606 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004607 * This will return the value @fn returns.
4608 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004609 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004610 */
Tejun Heod84ff052013-03-12 11:29:59 -07004611long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004612{
Tejun Heoed48ece2012-09-18 12:48:43 -07004613 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004614
Tejun Heoed48ece2012-09-18 12:48:43 -07004615 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4616 schedule_work_on(cpu, &wfc.work);
4617 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004618 return wfc.ret;
4619}
4620EXPORT_SYMBOL_GPL(work_on_cpu);
4621#endif /* CONFIG_SMP */
4622
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004623#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304624
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004625/**
4626 * freeze_workqueues_begin - begin freezing workqueues
4627 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004628 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004629 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004630 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004631 *
4632 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004633 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004634 */
4635void freeze_workqueues_begin(void)
4636{
Tejun Heo17116962013-03-12 11:29:58 -07004637 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004638 struct workqueue_struct *wq;
4639 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004640 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004641
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004642 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004643
Tejun Heo6183c002013-03-12 11:29:57 -07004644 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004645 workqueue_freezing = true;
4646
Tejun Heo24b8a842013-03-12 11:29:58 -07004647 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004648 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004649 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004650 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4651 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004652 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004653 }
4654
Tejun Heo24b8a842013-03-12 11:29:58 -07004655 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004656 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004657 for_each_pwq(pwq, wq)
4658 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004659 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004660 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004661
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004662 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004664
4665/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004666 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004667 *
4668 * Check whether freezing is complete. This function must be called
4669 * between freeze_workqueues_begin() and thaw_workqueues().
4670 *
4671 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004672 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004673 *
4674 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004675 * %true if some freezable workqueues are still busy. %false if freezing
4676 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004677 */
4678bool freeze_workqueues_busy(void)
4679{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004680 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004681 struct workqueue_struct *wq;
4682 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004683
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004684 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004685
Tejun Heo6183c002013-03-12 11:29:57 -07004686 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004687
Tejun Heo24b8a842013-03-12 11:29:58 -07004688 list_for_each_entry(wq, &workqueues, list) {
4689 if (!(wq->flags & WQ_FREEZABLE))
4690 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004691 /*
4692 * nr_active is monotonically decreasing. It's safe
4693 * to peek without lock.
4694 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004695 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004696 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004697 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004698 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004699 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004700 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004701 goto out_unlock;
4702 }
4703 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004704 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004705 }
4706out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004707 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004708 return busy;
4709}
4710
4711/**
4712 * thaw_workqueues - thaw workqueues
4713 *
4714 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004715 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004716 *
4717 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004718 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004719 */
4720void thaw_workqueues(void)
4721{
Tejun Heo24b8a842013-03-12 11:29:58 -07004722 struct workqueue_struct *wq;
4723 struct pool_workqueue *pwq;
4724 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004725 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004726
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004727 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004728
4729 if (!workqueue_freezing)
4730 goto out_unlock;
4731
Tejun Heo24b8a842013-03-12 11:29:58 -07004732 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004733 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004734 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004735 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4736 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004737 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004738 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004739
Tejun Heo24b8a842013-03-12 11:29:58 -07004740 /* restore max_active and repopulate worklist */
4741 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004742 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004743 for_each_pwq(pwq, wq)
4744 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004745 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004746 }
4747
4748 workqueue_freezing = false;
4749out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004750 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004751}
4752#endif /* CONFIG_FREEZER */
4753
Tejun Heobce90382013-04-01 11:23:32 -07004754static void __init wq_numa_init(void)
4755{
4756 cpumask_var_t *tbl;
4757 int node, cpu;
4758
4759 /* determine NUMA pwq table len - highest node id + 1 */
4760 for_each_node(node)
4761 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4762
4763 if (num_possible_nodes() <= 1)
4764 return;
4765
Tejun Heo4c16bd32013-04-01 11:23:36 -07004766 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4767 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4768
Tejun Heobce90382013-04-01 11:23:32 -07004769 /*
4770 * We want masks of possible CPUs of each node which isn't readily
4771 * available. Build one from cpu_to_node() which should have been
4772 * fully initialized by now.
4773 */
4774 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4775 BUG_ON(!tbl);
4776
4777 for_each_node(node)
4778 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4779
4780 for_each_possible_cpu(cpu) {
4781 node = cpu_to_node(cpu);
4782 if (WARN_ON(node == NUMA_NO_NODE)) {
4783 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4784 /* happens iff arch is bonkers, let's just proceed */
4785 return;
4786 }
4787 cpumask_set_cpu(cpu, tbl[node]);
4788 }
4789
4790 wq_numa_possible_cpumask = tbl;
4791 wq_numa_enabled = true;
4792}
4793
Suresh Siddha6ee05782010-07-30 14:57:37 -07004794static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004796 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4797 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004798
Tejun Heo7c3eed52013-01-24 11:01:33 -08004799 /* make sure we have enough bits for OFFQ pool ID */
4800 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004801 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004802
Tejun Heoe904e6c2013-03-12 11:29:57 -07004803 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4804
4805 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4806
Tejun Heo65758202012-07-17 12:39:26 -07004807 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004808 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004809
Tejun Heobce90382013-04-01 11:23:32 -07004810 wq_numa_init();
4811
Tejun Heo706026c2013-01-24 11:01:34 -08004812 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004813 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004814 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004815
Tejun Heo7a4e3442013-03-12 11:30:00 -07004816 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004817 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004818 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004819 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004820 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004821 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004822 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004823
Tejun Heo9daf9e62013-01-24 11:01:33 -08004824 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004825 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004826 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004827 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004828 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004829 }
4830
Tejun Heoe22bee72010-06-29 10:07:14 +02004831 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004832 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004833 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004834
Tejun Heof02ae732013-03-12 11:30:03 -07004835 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004836 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004837 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004838 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004839 }
4840
Tejun Heo29c91e92013-03-12 11:30:03 -07004841 /* create default unbound wq attrs */
4842 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4843 struct workqueue_attrs *attrs;
4844
4845 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004846 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004847 unbound_std_wq_attrs[i] = attrs;
4848 }
4849
Tejun Heod320c032010-06-29 10:07:14 +02004850 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004851 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004852 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004853 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4854 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004855 system_freezable_wq = alloc_workqueue("events_freezable",
4856 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004857 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004858 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004861early_initcall(init_workqueues);