blob: 154aa12af48e6a29313b6214bcb5c24fbf88e343 [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
Tejun Heod55262c2013-04-01 11:23:38 -0700271static bool wq_disable_numa;
272module_param_named(disable_numa, wq_disable_numa, bool, 0444);
273
Tejun Heobce90382013-04-01 11:23:32 -0700274static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
275
Tejun Heo4c16bd32013-04-01 11:23:36 -0700276/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
277static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
278
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700279static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700280static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700281
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700282static LIST_HEAD(workqueues); /* PL: list of all workqueues */
283static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700284
285/* the per-cpu worker pools */
286static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
287 cpu_worker_pools);
288
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700289static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700290
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700291/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700292static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
293
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700294/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700295static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
296
Tejun Heod320c032010-06-29 10:07:14 +0200297struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200298EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300299struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900300EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300301struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200302EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300303struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200304EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300305struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100306EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200307
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700308static int worker_thread(void *__worker);
309static void copy_workqueue_attrs(struct workqueue_attrs *to,
310 const struct workqueue_attrs *from);
311
Tejun Heo97bd2342010-10-05 10:41:14 +0200312#define CREATE_TRACE_POINTS
313#include <trace/events/workqueue.h>
314
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700315#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700316 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700317 lockdep_is_held(&wq_pool_mutex), \
318 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700319
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700320#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700321 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700322 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700323 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700324
Tejun Heo822d8402013-03-19 13:45:21 -0700325#ifdef CONFIG_LOCKDEP
326#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800327 WARN_ONCE(debug_locks && \
328 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700329 !lockdep_is_held(&(pool)->lock), \
330 "pool->manager_mutex or ->lock should be held")
331#else
332#define assert_manager_or_pool_lock(pool) do { } while (0)
333#endif
334
Tejun Heof02ae732013-03-12 11:30:03 -0700335#define for_each_cpu_worker_pool(pool, cpu) \
336 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
337 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700338 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700339
Tejun Heo49e3cf42013-03-12 11:29:58 -0700340/**
Tejun Heo17116962013-03-12 11:29:58 -0700341 * for_each_pool - iterate through all worker_pools in the system
342 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700343 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700344 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700345 * This must be called either with wq_pool_mutex held or sched RCU read
346 * locked. If the pool needs to be used beyond the locking in effect, the
347 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700348 *
349 * The if/else clause exists only for the lockdep assertion and can be
350 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700351 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700352#define for_each_pool(pool, pi) \
353 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700354 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700355 else
Tejun Heo17116962013-03-12 11:29:58 -0700356
357/**
Tejun Heo822d8402013-03-19 13:45:21 -0700358 * for_each_pool_worker - iterate through all workers of a worker_pool
359 * @worker: iteration cursor
360 * @wi: integer used for iteration
361 * @pool: worker_pool to iterate workers of
362 *
363 * This must be called with either @pool->manager_mutex or ->lock held.
364 *
365 * The if/else clause exists only for the lockdep assertion and can be
366 * ignored.
367 */
368#define for_each_pool_worker(worker, wi, pool) \
369 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
370 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
371 else
372
373/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700374 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
375 * @pwq: iteration cursor
376 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700377 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700378 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700379 * If the pwq needs to be used beyond the locking in effect, the caller is
380 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700381 *
382 * The if/else clause exists only for the lockdep assertion and can be
383 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700384 */
385#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700386 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700387 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700388 else
Tejun Heof3421792010-07-02 10:03:51 +0200389
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900390#ifdef CONFIG_DEBUG_OBJECTS_WORK
391
392static struct debug_obj_descr work_debug_descr;
393
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100394static void *work_debug_hint(void *addr)
395{
396 return ((struct work_struct *) addr)->func;
397}
398
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900399/*
400 * fixup_init is called when:
401 * - an active object is initialized
402 */
403static int work_fixup_init(void *addr, enum debug_obj_state state)
404{
405 struct work_struct *work = addr;
406
407 switch (state) {
408 case ODEBUG_STATE_ACTIVE:
409 cancel_work_sync(work);
410 debug_object_init(work, &work_debug_descr);
411 return 1;
412 default:
413 return 0;
414 }
415}
416
417/*
418 * fixup_activate is called when:
419 * - an active object is activated
420 * - an unknown object is activated (might be a statically initialized object)
421 */
422static int work_fixup_activate(void *addr, enum debug_obj_state state)
423{
424 struct work_struct *work = addr;
425
426 switch (state) {
427
428 case ODEBUG_STATE_NOTAVAILABLE:
429 /*
430 * This is not really a fixup. The work struct was
431 * statically initialized. We just make sure that it
432 * is tracked in the object tracker.
433 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200434 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900435 debug_object_init(work, &work_debug_descr);
436 debug_object_activate(work, &work_debug_descr);
437 return 0;
438 }
439 WARN_ON_ONCE(1);
440 return 0;
441
442 case ODEBUG_STATE_ACTIVE:
443 WARN_ON(1);
444
445 default:
446 return 0;
447 }
448}
449
450/*
451 * fixup_free is called when:
452 * - an active object is freed
453 */
454static int work_fixup_free(void *addr, enum debug_obj_state state)
455{
456 struct work_struct *work = addr;
457
458 switch (state) {
459 case ODEBUG_STATE_ACTIVE:
460 cancel_work_sync(work);
461 debug_object_free(work, &work_debug_descr);
462 return 1;
463 default:
464 return 0;
465 }
466}
467
468static struct debug_obj_descr work_debug_descr = {
469 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100470 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900471 .fixup_init = work_fixup_init,
472 .fixup_activate = work_fixup_activate,
473 .fixup_free = work_fixup_free,
474};
475
476static inline void debug_work_activate(struct work_struct *work)
477{
478 debug_object_activate(work, &work_debug_descr);
479}
480
481static inline void debug_work_deactivate(struct work_struct *work)
482{
483 debug_object_deactivate(work, &work_debug_descr);
484}
485
486void __init_work(struct work_struct *work, int onstack)
487{
488 if (onstack)
489 debug_object_init_on_stack(work, &work_debug_descr);
490 else
491 debug_object_init(work, &work_debug_descr);
492}
493EXPORT_SYMBOL_GPL(__init_work);
494
495void destroy_work_on_stack(struct work_struct *work)
496{
497 debug_object_free(work, &work_debug_descr);
498}
499EXPORT_SYMBOL_GPL(destroy_work_on_stack);
500
501#else
502static inline void debug_work_activate(struct work_struct *work) { }
503static inline void debug_work_deactivate(struct work_struct *work) { }
504#endif
505
Tejun Heo9daf9e62013-01-24 11:01:33 -0800506/* allocate ID and assign it to @pool */
507static int worker_pool_assign_id(struct worker_pool *pool)
508{
509 int ret;
510
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700511 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700512
Tejun Heoe68035f2013-03-13 14:59:38 -0700513 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
Tejun Heo229641a2013-04-01 17:08:13 -0700514 if (ret >= 0) {
Tejun Heoe68035f2013-03-13 14:59:38 -0700515 pool->id = ret;
Tejun Heo229641a2013-04-01 17:08:13 -0700516 return 0;
517 }
Tejun Heo9daf9e62013-01-24 11:01:33 -0800518 return ret;
519}
520
Tejun Heo76af4d92013-03-12 11:30:00 -0700521/**
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700522 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
523 * @wq: the target workqueue
524 * @node: the node ID
525 *
526 * This must be called either with pwq_lock held or sched RCU read locked.
527 * If the pwq needs to be used beyond the locking in effect, the caller is
528 * responsible for guaranteeing that the pwq stays online.
529 */
530static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
531 int node)
532{
533 assert_rcu_or_wq_mutex(wq);
534 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
535}
536
Tejun Heo73f53c42010-06-29 10:07:11 +0200537static unsigned int work_color_to_flags(int color)
538{
539 return color << WORK_STRUCT_COLOR_SHIFT;
540}
541
542static int get_work_color(struct work_struct *work)
543{
544 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
545 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
546}
547
548static int work_next_color(int color)
549{
550 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700551}
552
David Howells4594bf12006-12-07 11:33:26 +0000553/*
Tejun Heo112202d2013-02-13 19:29:12 -0800554 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
555 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800556 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557 *
Tejun Heo112202d2013-02-13 19:29:12 -0800558 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
559 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700560 * work->data. These functions should only be called while the work is
561 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562 *
Tejun Heo112202d2013-02-13 19:29:12 -0800563 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800564 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800565 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800566 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700567 *
568 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
569 * canceled. While being canceled, a work item may have its PENDING set
570 * but stay off timer and worklist for arbitrarily long and nobody should
571 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000572 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573static inline void set_work_data(struct work_struct *work, unsigned long data,
574 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000575{
Tejun Heo6183c002013-03-12 11:29:57 -0700576 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000578}
David Howells365970a2006-11-22 14:54:49 +0000579
Tejun Heo112202d2013-02-13 19:29:12 -0800580static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200582{
Tejun Heo112202d2013-02-13 19:29:12 -0800583 set_work_data(work, (unsigned long)pwq,
584 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200585}
586
Lai Jiangshan4468a002013-02-06 18:04:53 -0800587static void set_work_pool_and_keep_pending(struct work_struct *work,
588 int pool_id)
589{
590 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
591 WORK_STRUCT_PENDING);
592}
593
Tejun Heo7c3eed52013-01-24 11:01:33 -0800594static void set_work_pool_and_clear_pending(struct work_struct *work,
595 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000596{
Tejun Heo23657bb2012-08-13 17:08:19 -0700597 /*
598 * The following wmb is paired with the implied mb in
599 * test_and_set_bit(PENDING) and ensures all updates to @work made
600 * here are visible to and precede any updates by the next PENDING
601 * owner.
602 */
603 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800604 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605}
606
607static void clear_work_data(struct work_struct *work)
608{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800609 smp_wmb(); /* see set_work_pool_and_clear_pending() */
610 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200611}
612
Tejun Heo112202d2013-02-13 19:29:12 -0800613static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200614{
Tejun Heoe1201532010-07-22 14:14:25 +0200615 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200616
Tejun Heo112202d2013-02-13 19:29:12 -0800617 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200618 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
619 else
620 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200621}
622
Tejun Heo7c3eed52013-01-24 11:01:33 -0800623/**
624 * get_work_pool - return the worker_pool a given work was associated with
625 * @work: the work item of interest
626 *
627 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700628 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700629 * Pools are created and destroyed under wq_pool_mutex, and allows read
630 * access under sched-RCU read lock. As such, this function should be
631 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700632 *
633 * All fields of the returned pool are accessible as long as the above
634 * mentioned locking is in effect. If the returned pool needs to be used
635 * beyond the critical section, the caller is responsible for ensuring the
636 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800637 */
638static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200639{
Tejun Heoe1201532010-07-22 14:14:25 +0200640 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800641 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200642
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700643 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700644
Tejun Heo112202d2013-02-13 19:29:12 -0800645 if (data & WORK_STRUCT_PWQ)
646 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800647 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200648
Tejun Heo7c3eed52013-01-24 11:01:33 -0800649 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
650 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200651 return NULL;
652
Tejun Heofa1b54e2013-03-12 11:30:00 -0700653 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800654}
655
656/**
657 * get_work_pool_id - return the worker pool ID a given work is associated with
658 * @work: the work item of interest
659 *
660 * Return the worker_pool ID @work was last associated with.
661 * %WORK_OFFQ_POOL_NONE if none.
662 */
663static int get_work_pool_id(struct work_struct *work)
664{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800665 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800666
Tejun Heo112202d2013-02-13 19:29:12 -0800667 if (data & WORK_STRUCT_PWQ)
668 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800669 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
670
671 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800672}
673
Tejun Heobbb68df2012-08-03 10:30:46 -0700674static void mark_work_canceling(struct work_struct *work)
675{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800676 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700677
Tejun Heo7c3eed52013-01-24 11:01:33 -0800678 pool_id <<= WORK_OFFQ_POOL_SHIFT;
679 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700680}
681
682static bool work_is_canceling(struct work_struct *work)
683{
684 unsigned long data = atomic_long_read(&work->data);
685
Tejun Heo112202d2013-02-13 19:29:12 -0800686 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700687}
688
David Howells365970a2006-11-22 14:54:49 +0000689/*
Tejun Heo32704762012-07-13 22:16:45 -0700690 * Policy functions. These define the policies on how the global worker
691 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800692 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000693 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200694
Tejun Heo63d95a92012-07-12 14:46:37 -0700695static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000696{
Tejun Heoe19e3972013-01-24 11:39:44 -0800697 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000698}
699
Tejun Heoe22bee72010-06-29 10:07:14 +0200700/*
701 * Need to wake up a worker? Called from anything but currently
702 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700703 *
704 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800705 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700706 * worklist isn't empty.
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 Heo63d95a92012-07-12 14:46:37 -0700710 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000711}
712
Tejun Heoe22bee72010-06-29 10:07:14 +0200713/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200717}
718
719/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700720static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200721{
Tejun Heoe19e3972013-01-24 11:39:44 -0800722 return !list_empty(&pool->worklist) &&
723 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200724}
725
726/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700727static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200728{
Tejun Heo63d95a92012-07-12 14:46:37 -0700729 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200730}
731
732/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700733static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200734{
Tejun Heo63d95a92012-07-12 14:46:37 -0700735 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700736 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200737}
738
739/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700740static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200741{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700742 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
744 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200745
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700746 /*
747 * nr_idle and idle_list may disagree if idle rebinding is in
748 * progress. Never return %true if idle_list is empty.
749 */
750 if (list_empty(&pool->idle_list))
751 return false;
752
Tejun Heoe22bee72010-06-29 10:07:14 +0200753 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
754}
755
756/*
757 * Wake up functions.
758 */
759
Tejun Heo7e116292010-06-29 10:07:13 +0200760/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700761static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200762{
Tejun Heo63d95a92012-07-12 14:46:37 -0700763 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200764 return NULL;
765
Tejun Heo63d95a92012-07-12 14:46:37 -0700766 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200767}
768
769/**
770 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700771 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200772 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700773 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200774 *
775 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800776 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200777 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700778static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200779{
Tejun Heo63d95a92012-07-12 14:46:37 -0700780 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200781
782 if (likely(worker))
783 wake_up_process(worker->task);
784}
785
Tejun Heo4690c4a2010-06-29 10:07:10 +0200786/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200787 * wq_worker_waking_up - a worker is waking up
788 * @task: task waking up
789 * @cpu: CPU @task is waking up to
790 *
791 * This function is called during try_to_wake_up() when a worker is
792 * being awoken.
793 *
794 * CONTEXT:
795 * spin_lock_irq(rq->lock)
796 */
Tejun Heod84ff052013-03-12 11:29:59 -0700797void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200798{
799 struct worker *worker = kthread_data(task);
800
Joonsoo Kim36576002012-10-26 23:03:49 +0900801 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800802 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800803 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900804 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200805}
806
807/**
808 * wq_worker_sleeping - a worker is going to sleep
809 * @task: task going to sleep
810 * @cpu: CPU in question, must be the current CPU number
811 *
812 * This function is called during schedule() when a busy worker is
813 * going to sleep. Worker on the same cpu can be woken up by
814 * returning pointer to its task.
815 *
816 * CONTEXT:
817 * spin_lock_irq(rq->lock)
818 *
819 * RETURNS:
820 * Worker task on @cpu to wake up, %NULL if none.
821 */
Tejun Heod84ff052013-03-12 11:29:59 -0700822struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200823{
824 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800825 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200826
Tejun Heo111c2252013-01-17 17:16:24 -0800827 /*
828 * Rescuers, which may not have all the fields set up like normal
829 * workers, also reach here, let's not access anything before
830 * checking NOT_RUNNING.
831 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500832 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200833 return NULL;
834
Tejun Heo111c2252013-01-17 17:16:24 -0800835 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800836
Tejun Heoe22bee72010-06-29 10:07:14 +0200837 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700838 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
839 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
841 /*
842 * The counterpart of the following dec_and_test, implied mb,
843 * worklist not empty test sequence is in insert_work().
844 * Please read comment there.
845 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700846 * NOT_RUNNING is clear. This means that we're bound to and
847 * running on the local cpu w/ rq lock held and preemption
848 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800849 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700850 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200851 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800852 if (atomic_dec_and_test(&pool->nr_running) &&
853 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700854 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200855 return to_wakeup ? to_wakeup->task : NULL;
856}
857
858/**
859 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200860 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200861 * @flags: flags to set
862 * @wakeup: wakeup an idle worker if necessary
863 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200864 * Set @flags in @worker->flags and adjust nr_running accordingly. If
865 * nr_running becomes zero and @wakeup is %true, an idle worker is
866 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200867 *
Tejun Heocb444762010-07-02 10:03:50 +0200868 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800869 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200870 */
871static inline void worker_set_flags(struct worker *worker, unsigned int flags,
872 bool wakeup)
873{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700874 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200875
Tejun Heocb444762010-07-02 10:03:50 +0200876 WARN_ON_ONCE(worker->task != current);
877
Tejun Heoe22bee72010-06-29 10:07:14 +0200878 /*
879 * If transitioning into NOT_RUNNING, adjust nr_running and
880 * wake up an idle worker as necessary if requested by
881 * @wakeup.
882 */
883 if ((flags & WORKER_NOT_RUNNING) &&
884 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200885 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800886 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700887 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700888 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200889 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800890 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200891 }
892
Tejun Heod302f012010-06-29 10:07:13 +0200893 worker->flags |= flags;
894}
895
896/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200897 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200898 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200899 * @flags: flags to clear
900 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200901 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200902 *
Tejun Heocb444762010-07-02 10:03:50 +0200903 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800904 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200905 */
906static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
907{
Tejun Heo63d95a92012-07-12 14:46:37 -0700908 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200909 unsigned int oflags = worker->flags;
910
Tejun Heocb444762010-07-02 10:03:50 +0200911 WARN_ON_ONCE(worker->task != current);
912
Tejun Heod302f012010-06-29 10:07:13 +0200913 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200914
Tejun Heo42c025f2011-01-11 15:58:49 +0100915 /*
916 * If transitioning out of NOT_RUNNING, increment nr_running. Note
917 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
918 * of multiple flags, not a single flag.
919 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200920 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
921 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800922 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200923}
924
925/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200926 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800927 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200928 * @work: work to find worker for
929 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800930 * Find a worker which is executing @work on @pool by searching
931 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800932 * to match, its current execution should match the address of @work and
933 * its work function. This is to avoid unwanted dependency between
934 * unrelated work executions through a work item being recycled while still
935 * being executed.
936 *
937 * This is a bit tricky. A work item may be freed once its execution
938 * starts and nothing prevents the freed area from being recycled for
939 * another work item. If the same work item address ends up being reused
940 * before the original execution finishes, workqueue will identify the
941 * recycled work item as currently executing and make it wait until the
942 * current execution finishes, introducing an unwanted dependency.
943 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700944 * This function checks the work item address and work function to avoid
945 * false positives. Note that this isn't complete as one may construct a
946 * work function which can introduce dependency onto itself through a
947 * recycled work item. Well, if somebody wants to shoot oneself in the
948 * foot that badly, there's only so much we can do, and if such deadlock
949 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200950 *
951 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800952 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200953 *
954 * RETURNS:
955 * Pointer to worker which is executing @work if found, NULL
956 * otherwise.
957 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800958static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200959 struct work_struct *work)
960{
Sasha Levin42f85702012-12-17 10:01:23 -0500961 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500962
Sasha Levinb67bfe02013-02-27 17:06:00 -0800963 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800964 (unsigned long)work)
965 if (worker->current_work == work &&
966 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500967 return worker;
968
969 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200970}
971
972/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700973 * move_linked_works - move linked works to a list
974 * @work: start of series of works to be scheduled
975 * @head: target list to append @work to
976 * @nextp: out paramter for nested worklist walking
977 *
978 * Schedule linked works starting from @work to @head. Work series to
979 * be scheduled starts at @work and includes any consecutive work with
980 * WORK_STRUCT_LINKED set in its predecessor.
981 *
982 * If @nextp is not NULL, it's updated to point to the next work of
983 * the last scheduled work. This allows move_linked_works() to be
984 * nested inside outer list_for_each_entry_safe().
985 *
986 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800987 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700988 */
989static void move_linked_works(struct work_struct *work, struct list_head *head,
990 struct work_struct **nextp)
991{
992 struct work_struct *n;
993
994 /*
995 * Linked worklist will always end before the end of the list,
996 * use NULL for list head.
997 */
998 list_for_each_entry_safe_from(work, n, NULL, entry) {
999 list_move_tail(&work->entry, head);
1000 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1001 break;
1002 }
1003
1004 /*
1005 * If we're already inside safe list traversal and have moved
1006 * multiple works to the scheduled queue, the next position
1007 * needs to be updated.
1008 */
1009 if (nextp)
1010 *nextp = n;
1011}
1012
Tejun Heo8864b4e2013-03-12 11:30:04 -07001013/**
1014 * get_pwq - get an extra reference on the specified pool_workqueue
1015 * @pwq: pool_workqueue to get
1016 *
1017 * Obtain an extra reference on @pwq. The caller should guarantee that
1018 * @pwq has positive refcnt and be holding the matching pool->lock.
1019 */
1020static void get_pwq(struct pool_workqueue *pwq)
1021{
1022 lockdep_assert_held(&pwq->pool->lock);
1023 WARN_ON_ONCE(pwq->refcnt <= 0);
1024 pwq->refcnt++;
1025}
1026
1027/**
1028 * put_pwq - put a pool_workqueue reference
1029 * @pwq: pool_workqueue to put
1030 *
1031 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1032 * destruction. The caller should be holding the matching pool->lock.
1033 */
1034static void put_pwq(struct pool_workqueue *pwq)
1035{
1036 lockdep_assert_held(&pwq->pool->lock);
1037 if (likely(--pwq->refcnt))
1038 return;
1039 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1040 return;
1041 /*
1042 * @pwq can't be released under pool->lock, bounce to
1043 * pwq_unbound_release_workfn(). This never recurses on the same
1044 * pool->lock as this path is taken only for unbound workqueues and
1045 * the release work item is scheduled on a per-cpu workqueue. To
1046 * avoid lockdep warning, unbound pool->locks are given lockdep
1047 * subclass of 1 in get_unbound_pool().
1048 */
1049 schedule_work(&pwq->unbound_release_work);
1050}
1051
Tejun Heodce90d42013-04-01 11:23:35 -07001052/**
1053 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1054 * @pwq: pool_workqueue to put (can be %NULL)
1055 *
1056 * put_pwq() with locking. This function also allows %NULL @pwq.
1057 */
1058static void put_pwq_unlocked(struct pool_workqueue *pwq)
1059{
1060 if (pwq) {
1061 /*
1062 * As both pwqs and pools are sched-RCU protected, the
1063 * following lock operations are safe.
1064 */
1065 spin_lock_irq(&pwq->pool->lock);
1066 put_pwq(pwq);
1067 spin_unlock_irq(&pwq->pool->lock);
1068 }
1069}
1070
Tejun Heo112202d2013-02-13 19:29:12 -08001071static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001072{
Tejun Heo112202d2013-02-13 19:29:12 -08001073 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001074
1075 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001076 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001077 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001078 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001079}
1080
Tejun Heo112202d2013-02-13 19:29:12 -08001081static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001082{
Tejun Heo112202d2013-02-13 19:29:12 -08001083 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001084 struct work_struct, entry);
1085
Tejun Heo112202d2013-02-13 19:29:12 -08001086 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001087}
1088
Tejun Heobf4ede02012-08-03 10:30:46 -07001089/**
Tejun Heo112202d2013-02-13 19:29:12 -08001090 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1091 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001093 *
1094 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001095 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001096 *
1097 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001098 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001099 */
Tejun Heo112202d2013-02-13 19:29:12 -08001100static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001101{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001102 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001103 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001104 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001105
Tejun Heo112202d2013-02-13 19:29:12 -08001106 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001107
Tejun Heo112202d2013-02-13 19:29:12 -08001108 pwq->nr_active--;
1109 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001110 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001111 if (pwq->nr_active < pwq->max_active)
1112 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001113 }
1114
1115 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001116 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001117 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001118
1119 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001120 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001121 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001122
Tejun Heo112202d2013-02-13 19:29:12 -08001123 /* this pwq is done, clear flush_color */
1124 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001125
1126 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001127 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001128 * will handle the rest.
1129 */
Tejun Heo112202d2013-02-13 19:29:12 -08001130 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1131 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001132out_put:
1133 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001134}
1135
Tejun Heo36e227d2012-08-03 10:30:46 -07001136/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001137 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001138 * @work: work item to steal
1139 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001140 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001141 *
1142 * Try to grab PENDING bit of @work. This function can handle @work in any
1143 * stable state - idle, on timer or on worklist. Return values are
1144 *
1145 * 1 if @work was pending and we successfully stole PENDING
1146 * 0 if @work was idle and we claimed PENDING
1147 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001148 * -ENOENT if someone else is canceling @work, this state may persist
1149 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001150 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001151 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001152 * interrupted while holding PENDING and @work off queue, irq must be
1153 * disabled on entry. This, combined with delayed_work->timer being
1154 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001155 *
1156 * On successful return, >= 0, irq is disabled and the caller is
1157 * responsible for releasing it using local_irq_restore(*@flags).
1158 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001159 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001160 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001161static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1162 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001163{
Tejun Heod565ed62013-01-24 11:01:33 -08001164 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001165 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001166
Tejun Heobbb68df2012-08-03 10:30:46 -07001167 local_irq_save(*flags);
1168
Tejun Heo36e227d2012-08-03 10:30:46 -07001169 /* try to steal the timer if it exists */
1170 if (is_dwork) {
1171 struct delayed_work *dwork = to_delayed_work(work);
1172
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001173 /*
1174 * dwork->timer is irqsafe. If del_timer() fails, it's
1175 * guaranteed that the timer is not queued anywhere and not
1176 * running on the local CPU.
1177 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001178 if (likely(del_timer(&dwork->timer)))
1179 return 1;
1180 }
1181
1182 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001183 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1184 return 0;
1185
1186 /*
1187 * The queueing is in progress, or it is already queued. Try to
1188 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1189 */
Tejun Heod565ed62013-01-24 11:01:33 -08001190 pool = get_work_pool(work);
1191 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001192 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001193
Tejun Heod565ed62013-01-24 11:01:33 -08001194 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001195 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001196 * work->data is guaranteed to point to pwq only while the work
1197 * item is queued on pwq->wq, and both updating work->data to point
1198 * to pwq on queueing and to pool on dequeueing are done under
1199 * pwq->pool->lock. This in turn guarantees that, if work->data
1200 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001201 * item is currently queued on that pool.
1202 */
Tejun Heo112202d2013-02-13 19:29:12 -08001203 pwq = get_work_pwq(work);
1204 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001205 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001206
Tejun Heo16062832013-02-06 18:04:53 -08001207 /*
1208 * A delayed work item cannot be grabbed directly because
1209 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001210 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001211 * management later on and cause stall. Make sure the work
1212 * item is activated before grabbing.
1213 */
1214 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001215 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001216
Tejun Heo16062832013-02-06 18:04:53 -08001217 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001218 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001219
Tejun Heo112202d2013-02-13 19:29:12 -08001220 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001221 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001222
Tejun Heo16062832013-02-06 18:04:53 -08001223 spin_unlock(&pool->lock);
1224 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001225 }
Tejun Heod565ed62013-01-24 11:01:33 -08001226 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001227fail:
1228 local_irq_restore(*flags);
1229 if (work_is_canceling(work))
1230 return -ENOENT;
1231 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001232 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001233}
1234
1235/**
Tejun Heo706026c2013-01-24 11:01:34 -08001236 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001237 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001238 * @work: work to insert
1239 * @head: insertion point
1240 * @extra_flags: extra WORK_STRUCT_* flags to set
1241 *
Tejun Heo112202d2013-02-13 19:29:12 -08001242 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001243 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001244 *
1245 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001246 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001247 */
Tejun Heo112202d2013-02-13 19:29:12 -08001248static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1249 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001250{
Tejun Heo112202d2013-02-13 19:29:12 -08001251 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001252
Tejun Heo4690c4a2010-06-29 10:07:10 +02001253 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001254 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001255 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001256 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001257
1258 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001259 * Ensure either wq_worker_sleeping() sees the above
1260 * list_add_tail() or we see zero nr_running to avoid workers lying
1261 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001262 */
1263 smp_mb();
1264
Tejun Heo63d95a92012-07-12 14:46:37 -07001265 if (__need_more_worker(pool))
1266 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001267}
1268
Tejun Heoc8efcc22010-12-20 19:32:04 +01001269/*
1270 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001271 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001272 */
1273static bool is_chained_work(struct workqueue_struct *wq)
1274{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001275 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001276
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001277 worker = current_wq_worker();
1278 /*
1279 * Return %true iff I'm a worker execuing a work item on @wq. If
1280 * I'm @worker, it's safe to dereference it without locking.
1281 */
Tejun Heo112202d2013-02-13 19:29:12 -08001282 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001283}
1284
Tejun Heod84ff052013-03-12 11:29:59 -07001285static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct work_struct *work)
1287{
Tejun Heo112202d2013-02-13 19:29:12 -08001288 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001289 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001290 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001291 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001292 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001293
1294 /*
1295 * While a work item is PENDING && off queue, a task trying to
1296 * steal the PENDING will busy-loop waiting for it to either get
1297 * queued or lose PENDING. Grabbing PENDING and queueing should
1298 * happen with IRQ disabled.
1299 */
1300 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001302 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001303
Tejun Heoc8efcc22010-12-20 19:32:04 +01001304 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001305 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001306 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001307 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001308retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001309 if (req_cpu == WORK_CPU_UNBOUND)
1310 cpu = raw_smp_processor_id();
1311
Tejun Heoc9178082013-03-12 11:30:04 -07001312 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001313 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001314 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001315 else
1316 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001317
Tejun Heoc9178082013-03-12 11:30:04 -07001318 /*
1319 * If @work was previously on a different pool, it might still be
1320 * running there, in which case the work needs to be queued on that
1321 * pool to guarantee non-reentrancy.
1322 */
1323 last_pool = get_work_pool(work);
1324 if (last_pool && last_pool != pwq->pool) {
1325 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001326
Tejun Heoc9178082013-03-12 11:30:04 -07001327 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001328
Tejun Heoc9178082013-03-12 11:30:04 -07001329 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001330
Tejun Heoc9178082013-03-12 11:30:04 -07001331 if (worker && worker->current_pwq->wq == wq) {
1332 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001333 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001334 /* meh... not running there, queue here */
1335 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001336 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001337 }
Tejun Heof3421792010-07-02 10:03:51 +02001338 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001339 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001340 }
1341
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001342 /*
1343 * pwq is determined and locked. For unbound pools, we could have
1344 * raced with pwq release and it could already be dead. If its
1345 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001346 * without another pwq replacing it in the numa_pwq_tbl or while
1347 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001348 * make forward-progress.
1349 */
1350 if (unlikely(!pwq->refcnt)) {
1351 if (wq->flags & WQ_UNBOUND) {
1352 spin_unlock(&pwq->pool->lock);
1353 cpu_relax();
1354 goto retry;
1355 }
1356 /* oops */
1357 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1358 wq->name, cpu);
1359 }
1360
Tejun Heo112202d2013-02-13 19:29:12 -08001361 /* pwq determined, queue */
1362 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001363
Dan Carpenterf5b25522012-04-13 22:06:58 +03001364 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001365 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001366 return;
1367 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001368
Tejun Heo112202d2013-02-13 19:29:12 -08001369 pwq->nr_in_flight[pwq->work_color]++;
1370 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001371
Tejun Heo112202d2013-02-13 19:29:12 -08001372 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001373 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001374 pwq->nr_active++;
1375 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001376 } else {
1377 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001378 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001379 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001380
Tejun Heo112202d2013-02-13 19:29:12 -08001381 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001382
Tejun Heo112202d2013-02-13 19:29:12 -08001383 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001386/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001387 * queue_work_on - queue work on specific cpu
1388 * @cpu: CPU number to execute work on
1389 * @wq: workqueue to use
1390 * @work: work to queue
1391 *
Tejun Heod4283e92012-08-03 10:30:44 -07001392 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001393 *
1394 * We queue the work to a specific CPU, the caller must ensure it
1395 * can't go away.
1396 */
Tejun Heod4283e92012-08-03 10:30:44 -07001397bool queue_work_on(int cpu, struct workqueue_struct *wq,
1398 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001399{
Tejun Heod4283e92012-08-03 10:30:44 -07001400 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001401 unsigned long flags;
1402
1403 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001404
Tejun Heo22df02b2010-06-29 10:07:10 +02001405 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001406 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001407 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001408 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001409
1410 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001411 return ret;
1412}
1413EXPORT_SYMBOL_GPL(queue_work_on);
1414
Tejun Heod8e794d2012-08-03 10:30:45 -07001415void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
David Howells52bad642006-11-22 14:54:01 +00001417 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001419 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001420 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001422EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001424static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1425 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001427 struct timer_list *timer = &dwork->timer;
1428 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001430 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1431 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001432 WARN_ON_ONCE(timer_pending(timer));
1433 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001434
Tejun Heo8852aac2012-12-01 16:23:42 -08001435 /*
1436 * If @delay is 0, queue @dwork->work immediately. This is for
1437 * both optimization and correctness. The earliest @timer can
1438 * expire is on the closest next tick and delayed_work users depend
1439 * on that there's no such delay when @delay is 0.
1440 */
1441 if (!delay) {
1442 __queue_work(cpu, wq, &dwork->work);
1443 return;
1444 }
1445
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001446 timer_stats_timer_set_start_info(&dwork->timer);
1447
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001448 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001449 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001450 timer->expires = jiffies + delay;
1451
1452 if (unlikely(cpu != WORK_CPU_UNBOUND))
1453 add_timer_on(timer, cpu);
1454 else
1455 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001458/**
1459 * queue_delayed_work_on - queue work on specific CPU after delay
1460 * @cpu: CPU number to execute work on
1461 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001462 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001463 * @delay: number of jiffies to wait before queueing
1464 *
Tejun Heo715f1302012-08-03 10:30:46 -07001465 * Returns %false if @work was already on a queue, %true otherwise. If
1466 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1467 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001468 */
Tejun Heod4283e92012-08-03 10:30:44 -07001469bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1470 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001471{
David Howells52bad642006-11-22 14:54:01 +00001472 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001473 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001474 unsigned long flags;
1475
1476 /* read the comment in __queue_work() */
1477 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001478
Tejun Heo22df02b2010-06-29 10:07:10 +02001479 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001480 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001481 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001482 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001483
1484 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001485 return ret;
1486}
Dave Jonesae90dd52006-06-30 01:40:45 -04001487EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001490 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1491 * @cpu: CPU number to execute work on
1492 * @wq: workqueue to use
1493 * @dwork: work to queue
1494 * @delay: number of jiffies to wait before queueing
1495 *
1496 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1497 * modify @dwork's timer so that it expires after @delay. If @delay is
1498 * zero, @work is guaranteed to be scheduled immediately regardless of its
1499 * current state.
1500 *
1501 * Returns %false if @dwork was idle and queued, %true if @dwork was
1502 * pending and its timer was modified.
1503 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001504 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001505 * See try_to_grab_pending() for details.
1506 */
1507bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1508 struct delayed_work *dwork, unsigned long delay)
1509{
1510 unsigned long flags;
1511 int ret;
1512
1513 do {
1514 ret = try_to_grab_pending(&dwork->work, true, &flags);
1515 } while (unlikely(ret == -EAGAIN));
1516
1517 if (likely(ret >= 0)) {
1518 __queue_delayed_work(cpu, wq, dwork, delay);
1519 local_irq_restore(flags);
1520 }
1521
1522 /* -ENOENT from try_to_grab_pending() becomes %true */
1523 return ret;
1524}
1525EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1526
1527/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001528 * worker_enter_idle - enter idle state
1529 * @worker: worker which is entering idle state
1530 *
1531 * @worker is entering idle state. Update stats and idle timer if
1532 * necessary.
1533 *
1534 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001535 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001536 */
1537static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001539 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Tejun Heo6183c002013-03-12 11:29:57 -07001541 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1542 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1543 (worker->hentry.next || worker->hentry.pprev)))
1544 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Tejun Heocb444762010-07-02 10:03:50 +02001546 /* can't use worker_set_flags(), also called from start_worker() */
1547 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001548 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001549 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001550
Tejun Heoc8e55f32010-06-29 10:07:12 +02001551 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001552 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001553
Tejun Heo628c78e2012-07-17 12:39:27 -07001554 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1555 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001556
Tejun Heo544ecf32012-05-14 15:04:50 -07001557 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001558 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001559 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001560 * nr_running, the warning may trigger spuriously. Check iff
1561 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001562 */
Tejun Heo24647572013-01-24 11:01:33 -08001563 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001564 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001565 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Tejun Heoc8e55f32010-06-29 10:07:12 +02001568/**
1569 * worker_leave_idle - leave idle state
1570 * @worker: worker which is leaving idle state
1571 *
1572 * @worker is leaving idle state. Update stats.
1573 *
1574 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001575 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001576 */
1577static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001579 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Tejun Heo6183c002013-03-12 11:29:57 -07001581 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1582 return;
Tejun Heod302f012010-06-29 10:07:13 +02001583 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001584 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001585 list_del_init(&worker->entry);
1586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Tejun Heoe22bee72010-06-29 10:07:14 +02001588/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001589 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1590 * @pool: target worker_pool
1591 *
1592 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 *
1594 * Works which are scheduled while the cpu is online must at least be
1595 * scheduled to a worker which is bound to the cpu so that if they are
1596 * flushed from cpu callbacks while cpu is going down, they are
1597 * guaranteed to execute on the cpu.
1598 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001599 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 * themselves to the target cpu and may race with cpu going down or
1601 * coming online. kthread_bind() can't be used because it may put the
1602 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001603 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 * [dis]associated in the meantime.
1605 *
Tejun Heo706026c2013-01-24 11:01:34 -08001606 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001607 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001608 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1609 * enters idle state or fetches works without dropping lock, it can
1610 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 *
1612 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001613 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 * held.
1615 *
1616 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001617 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001618 * bound), %false if offline.
1619 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001620static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001621__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001622{
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 while (true) {
1624 /*
1625 * The following call may fail, succeed or succeed
1626 * without actually migrating the task to the cpu if
1627 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001628 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001629 */
Tejun Heo24647572013-01-24 11:01:33 -08001630 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001631 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001632
Tejun Heod565ed62013-01-24 11:01:33 -08001633 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001634 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001636 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001637 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001639 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001640
Tejun Heo5035b202011-04-29 18:08:37 +02001641 /*
1642 * We've raced with CPU hot[un]plug. Give it a breather
1643 * and retry migration. cond_resched() is required here;
1644 * otherwise, we might deadlock against cpu_stop trying to
1645 * bring down the CPU on non-preemptive kernel.
1646 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001647 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001648 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001649 }
1650}
1651
Tejun Heoc34056a2010-06-29 10:07:11 +02001652static struct worker *alloc_worker(void)
1653{
1654 struct worker *worker;
1655
1656 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001657 if (worker) {
1658 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001659 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001660 /* on creation a worker is in !idle && prep state */
1661 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001662 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001663 return worker;
1664}
1665
1666/**
1667 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001668 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001669 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001670 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001671 * can be started by calling start_worker() or destroyed using
1672 * destroy_worker().
1673 *
1674 * CONTEXT:
1675 * Might sleep. Does GFP_KERNEL allocations.
1676 *
1677 * RETURNS:
1678 * Pointer to the newly created worker.
1679 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001680static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001681{
Tejun Heoc34056a2010-06-29 10:07:11 +02001682 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001683 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001684 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001685
Tejun Heocd549682013-03-13 19:47:39 -07001686 lockdep_assert_held(&pool->manager_mutex);
1687
Tejun Heo822d8402013-03-19 13:45:21 -07001688 /*
1689 * ID is needed to determine kthread name. Allocate ID first
1690 * without installing the pointer.
1691 */
1692 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001693 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001694
1695 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1696
Tejun Heod565ed62013-01-24 11:01:33 -08001697 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001698 idr_preload_end();
1699 if (id < 0)
1700 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001701
1702 worker = alloc_worker();
1703 if (!worker)
1704 goto fail;
1705
Tejun Heobd7bdd42012-07-12 14:46:37 -07001706 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001707 worker->id = id;
1708
Tejun Heo29c91e92013-03-12 11:30:03 -07001709 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001710 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1711 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001712 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001713 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1714
Tejun Heof3f90ad2013-04-01 11:23:34 -07001715 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001716 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001717 if (IS_ERR(worker->task))
1718 goto fail;
1719
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001720 /*
1721 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1722 * online CPUs. It'll be re-applied when any of the CPUs come up.
1723 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001724 set_user_nice(worker->task, pool->attrs->nice);
1725 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001726
Tejun Heo14a40ff2013-03-19 13:45:20 -07001727 /* prevent userland from meddling with cpumask of workqueue workers */
1728 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001729
1730 /*
1731 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1732 * remains stable across this function. See the comments above the
1733 * flag definition for details.
1734 */
1735 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001736 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001737
Tejun Heo822d8402013-03-19 13:45:21 -07001738 /* successful, commit the pointer to idr */
1739 spin_lock_irq(&pool->lock);
1740 idr_replace(&pool->worker_idr, worker, worker->id);
1741 spin_unlock_irq(&pool->lock);
1742
Tejun Heoc34056a2010-06-29 10:07:11 +02001743 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001744
Tejun Heoc34056a2010-06-29 10:07:11 +02001745fail:
1746 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001747 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001748 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001749 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001750 }
1751 kfree(worker);
1752 return NULL;
1753}
1754
1755/**
1756 * start_worker - start a newly created worker
1757 * @worker: worker to start
1758 *
Tejun Heo706026c2013-01-24 11:01:34 -08001759 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001760 *
1761 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001762 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 */
1764static void start_worker(struct worker *worker)
1765{
Tejun Heocb444762010-07-02 10:03:50 +02001766 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001767 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001768 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001769 wake_up_process(worker->task);
1770}
1771
1772/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001773 * create_and_start_worker - create and start a worker for a pool
1774 * @pool: the target pool
1775 *
Tejun Heocd549682013-03-13 19:47:39 -07001776 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001777 */
1778static int create_and_start_worker(struct worker_pool *pool)
1779{
1780 struct worker *worker;
1781
Tejun Heocd549682013-03-13 19:47:39 -07001782 mutex_lock(&pool->manager_mutex);
1783
Tejun Heoebf44d12013-03-13 19:47:39 -07001784 worker = create_worker(pool);
1785 if (worker) {
1786 spin_lock_irq(&pool->lock);
1787 start_worker(worker);
1788 spin_unlock_irq(&pool->lock);
1789 }
1790
Tejun Heocd549682013-03-13 19:47:39 -07001791 mutex_unlock(&pool->manager_mutex);
1792
Tejun Heoebf44d12013-03-13 19:47:39 -07001793 return worker ? 0 : -ENOMEM;
1794}
1795
1796/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001797 * destroy_worker - destroy a workqueue worker
1798 * @worker: worker to be destroyed
1799 *
Tejun Heo706026c2013-01-24 11:01:34 -08001800 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001801 *
1802 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001803 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001804 */
1805static void destroy_worker(struct worker *worker)
1806{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001807 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001808
Tejun Heocd549682013-03-13 19:47:39 -07001809 lockdep_assert_held(&pool->manager_mutex);
1810 lockdep_assert_held(&pool->lock);
1811
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001813 if (WARN_ON(worker->current_work) ||
1814 WARN_ON(!list_empty(&worker->scheduled)))
1815 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001816
Tejun Heoc8e55f32010-06-29 10:07:12 +02001817 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001818 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001819 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001820 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001821
1822 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001823 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001824
Tejun Heo822d8402013-03-19 13:45:21 -07001825 idr_remove(&pool->worker_idr, worker->id);
1826
Tejun Heod565ed62013-01-24 11:01:33 -08001827 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001828
Tejun Heoc34056a2010-06-29 10:07:11 +02001829 kthread_stop(worker->task);
1830 kfree(worker);
1831
Tejun Heod565ed62013-01-24 11:01:33 -08001832 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001833}
1834
Tejun Heo63d95a92012-07-12 14:46:37 -07001835static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001836{
Tejun Heo63d95a92012-07-12 14:46:37 -07001837 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001838
Tejun Heod565ed62013-01-24 11:01:33 -08001839 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001840
Tejun Heo63d95a92012-07-12 14:46:37 -07001841 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 struct worker *worker;
1843 unsigned long expires;
1844
1845 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001846 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001847 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1848
1849 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001850 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001851 else {
1852 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001853 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 }
1856 }
1857
Tejun Heod565ed62013-01-24 11:01:33 -08001858 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001859}
1860
Tejun Heo493a1722013-03-12 11:29:59 -07001861static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001862{
Tejun Heo112202d2013-02-13 19:29:12 -08001863 struct pool_workqueue *pwq = get_work_pwq(work);
1864 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001865
Tejun Heo2e109a22013-03-13 19:47:40 -07001866 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001867
Tejun Heo493008a2013-03-12 11:30:03 -07001868 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001869 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001870
1871 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001872 if (list_empty(&pwq->mayday_node)) {
1873 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001874 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001875 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001876}
1877
Tejun Heo706026c2013-01-24 11:01:34 -08001878static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001879{
Tejun Heo63d95a92012-07-12 14:46:37 -07001880 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 struct work_struct *work;
1882
Tejun Heo2e109a22013-03-13 19:47:40 -07001883 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001884 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001885
Tejun Heo63d95a92012-07-12 14:46:37 -07001886 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001887 /*
1888 * We've been trying to create a new worker but
1889 * haven't been successful. We might be hitting an
1890 * allocation deadlock. Send distress signals to
1891 * rescuers.
1892 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001893 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001894 send_mayday(work);
1895 }
1896
Tejun Heo493a1722013-03-12 11:29:59 -07001897 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001898 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001899
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001901}
1902
1903/**
1904 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001907 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 * have at least one idle worker on return from this function. If
1909 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 * possible allocation deadlock.
1912 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001913 * On return, need_to_create_worker() is guaranteed to be %false and
1914 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 *
1916 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001917 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 * multiple times. Does GFP_KERNEL allocations. Called only from
1919 * manager.
1920 *
1921 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001922 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 * otherwise.
1924 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001925static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001926__releases(&pool->lock)
1927__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001928{
Tejun Heo63d95a92012-07-12 14:46:37 -07001929 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 return false;
1931restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001932 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001933
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001935 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001936
1937 while (true) {
1938 struct worker *worker;
1939
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001940 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001941 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001942 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001943 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001945 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1946 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 return true;
1948 }
1949
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 break;
1952
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 __set_current_state(TASK_INTERRUPTIBLE);
1954 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001955
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 break;
1958 }
1959
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001961 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001962 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 goto restart;
1964 return true;
1965}
1966
1967/**
1968 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001969 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001971 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 * IDLE_WORKER_TIMEOUT.
1973 *
1974 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001975 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 * multiple times. Called only from manager.
1977 *
1978 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001979 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * otherwise.
1981 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001982static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001983{
1984 bool ret = false;
1985
Tejun Heo63d95a92012-07-12 14:46:37 -07001986 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 struct worker *worker;
1988 unsigned long expires;
1989
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1992
1993 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 break;
1996 }
1997
1998 destroy_worker(worker);
1999 ret = true;
2000 }
2001
2002 return ret;
2003}
2004
2005/**
2006 * manage_workers - manage worker pool
2007 * @worker: self
2008 *
Tejun Heo706026c2013-01-24 11:01:34 -08002009 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002011 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 *
2013 * The caller can safely start processing works on false return. On
2014 * true return, it's guaranteed that need_to_create_worker() is false
2015 * and may_start_working() is true.
2016 *
2017 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002018 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 * multiple times. Does GFP_KERNEL allocations.
2020 *
2021 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002022 * spin_lock_irq(pool->lock) which may be released and regrabbed
2023 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 */
2025static bool manage_workers(struct worker *worker)
2026{
Tejun Heo63d95a92012-07-12 14:46:37 -07002027 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 bool ret = false;
2029
Tejun Heobc3a1af2013-03-13 19:47:39 -07002030 /*
2031 * Managership is governed by two mutexes - manager_arb and
2032 * manager_mutex. manager_arb handles arbitration of manager role.
2033 * Anyone who successfully grabs manager_arb wins the arbitration
2034 * and becomes the manager. mutex_trylock() on pool->manager_arb
2035 * failure while holding pool->lock reliably indicates that someone
2036 * else is managing the pool and the worker which failed trylock
2037 * can proceed to executing work items. This means that anyone
2038 * grabbing manager_arb is responsible for actually performing
2039 * manager duties. If manager_arb is grabbed and released without
2040 * actual management, the pool may stall indefinitely.
2041 *
2042 * manager_mutex is used for exclusion of actual management
2043 * operations. The holder of manager_mutex can be sure that none
2044 * of management operations, including creation and destruction of
2045 * workers, won't take place until the mutex is released. Because
2046 * manager_mutex doesn't interfere with manager role arbitration,
2047 * it is guaranteed that the pool's management, while may be
2048 * delayed, won't be disturbed by someone else grabbing
2049 * manager_mutex.
2050 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002051 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002052 return ret;
2053
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002054 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002055 * With manager arbitration won, manager_mutex would be free in
2056 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002057 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002058 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002059 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002060 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002061 ret = true;
2062 }
2063
Tejun Heo11ebea52012-07-12 14:46:37 -07002064 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002065
2066 /*
2067 * Destroy and then create so that may_start_working() is true
2068 * on return.
2069 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002070 ret |= maybe_destroy_workers(pool);
2071 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002072
Tejun Heobc3a1af2013-03-13 19:47:39 -07002073 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002074 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002075 return ret;
2076}
2077
Tejun Heoa62428c2010-06-29 10:07:10 +02002078/**
2079 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002080 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002081 * @work: work to process
2082 *
2083 * Process @work. This function contains all the logics necessary to
2084 * process a single work including synchronization against and
2085 * interaction with other workers on the same cpu, queueing and
2086 * flushing. As long as context requirement is met, any worker can
2087 * call this function to process a work.
2088 *
2089 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002090 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002091 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002092static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002093__releases(&pool->lock)
2094__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002095{
Tejun Heo112202d2013-02-13 19:29:12 -08002096 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002097 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002098 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002099 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002100 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002101#ifdef CONFIG_LOCKDEP
2102 /*
2103 * It is permissible to free the struct work_struct from
2104 * inside the function that is called from it, this we need to
2105 * take into account for lockdep too. To avoid bogus "held
2106 * lock freed" warnings as well as problems when looking into
2107 * work->lockdep_map, make a copy and use that here.
2108 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002109 struct lockdep_map lockdep_map;
2110
2111 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002112#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002113 /*
2114 * Ensure we're on the correct CPU. DISASSOCIATED test is
2115 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002116 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002117 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002118 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002119 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002120 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002121
Tejun Heo7e116292010-06-29 10:07:13 +02002122 /*
2123 * A single work shouldn't be executed concurrently by
2124 * multiple workers on a single cpu. Check whether anyone is
2125 * already processing the work. If so, defer the work to the
2126 * currently executing one.
2127 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002128 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002129 if (unlikely(collision)) {
2130 move_linked_works(work, &collision->scheduled, NULL);
2131 return;
2132 }
2133
Tejun Heo8930cab2012-08-03 10:30:45 -07002134 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002136 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002137 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002138 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002139 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002140 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002141
Tejun Heoa62428c2010-06-29 10:07:10 +02002142 list_del_init(&work->entry);
2143
Tejun Heo649027d2010-06-29 10:07:14 +02002144 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002145 * CPU intensive works don't participate in concurrency
2146 * management. They're the scheduler's responsibility.
2147 */
2148 if (unlikely(cpu_intensive))
2149 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2150
Tejun Heo974271c2012-07-12 14:46:37 -07002151 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002152 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002153 * executed ASAP. Wake up another worker if necessary.
2154 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002155 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2156 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002157
Tejun Heo8930cab2012-08-03 10:30:45 -07002158 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002159 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002160 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002161 * PENDING and queued state changes happen together while IRQ is
2162 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002163 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002164 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002165
Tejun Heod565ed62013-01-24 11:01:33 -08002166 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002167
Tejun Heo112202d2013-02-13 19:29:12 -08002168 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002169 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002170 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002171 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002172 /*
2173 * While we must be careful to not use "work" after this, the trace
2174 * point will only record its address.
2175 */
2176 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002177 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002178 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002179
2180 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002181 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2182 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002183 current->comm, preempt_count(), task_pid_nr(current),
2184 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002185 debug_show_held_locks(current);
2186 dump_stack();
2187 }
2188
Tejun Heod565ed62013-01-24 11:01:33 -08002189 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002190
Tejun Heofb0e7be2010-06-29 10:07:15 +02002191 /* clear cpu intensive status */
2192 if (unlikely(cpu_intensive))
2193 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2194
Tejun Heoa62428c2010-06-29 10:07:10 +02002195 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002196 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002197 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002198 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002199 worker->current_pwq = NULL;
2200 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201}
2202
Tejun Heoaffee4b2010-06-29 10:07:12 +02002203/**
2204 * process_scheduled_works - process scheduled works
2205 * @worker: self
2206 *
2207 * Process all scheduled works. Please note that the scheduled list
2208 * may change while processing a work, so this function repeatedly
2209 * fetches a work from the top and executes it.
2210 *
2211 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002212 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002213 * multiple times.
2214 */
2215static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002217 while (!list_empty(&worker->scheduled)) {
2218 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002220 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Tejun Heo4690c4a2010-06-29 10:07:10 +02002224/**
2225 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002226 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002227 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002228 * The worker thread function. All workers belong to a worker_pool -
2229 * either a per-cpu one or dynamic unbound one. These workers process all
2230 * work items regardless of their specific target workqueue. The only
2231 * exception is work items which belong to workqueues with a rescuer which
2232 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002233 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002234static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Tejun Heoc34056a2010-06-29 10:07:11 +02002236 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002237 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Tejun Heoe22bee72010-06-29 10:07:14 +02002239 /* tell the scheduler that this is a workqueue worker */
2240 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002241woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002242 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Tejun Heoa9ab7752013-03-19 13:45:21 -07002244 /* am I supposed to die? */
2245 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002246 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002247 WARN_ON_ONCE(!list_empty(&worker->entry));
2248 worker->task->flags &= ~PF_WQ_WORKER;
2249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251
Tejun Heoc8e55f32010-06-29 10:07:12 +02002252 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002253recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002254 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002255 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002256 goto sleep;
2257
2258 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002259 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002260 goto recheck;
2261
Tejun Heoc8e55f32010-06-29 10:07:12 +02002262 /*
2263 * ->scheduled list can only be filled while a worker is
2264 * preparing to process a work or actually processing it.
2265 * Make sure nobody diddled with it while I was sleeping.
2266 */
Tejun Heo6183c002013-03-12 11:29:57 -07002267 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002268
Tejun Heoe22bee72010-06-29 10:07:14 +02002269 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002270 * Finish PREP stage. We're guaranteed to have at least one idle
2271 * worker or that someone else has already assumed the manager
2272 * role. This is where @worker starts participating in concurrency
2273 * management if applicable and concurrency management is restored
2274 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002275 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002276 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002277
2278 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002280 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002281 struct work_struct, entry);
2282
2283 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2284 /* optimization path, not strictly necessary */
2285 process_one_work(worker, work);
2286 if (unlikely(!list_empty(&worker->scheduled)))
2287 process_scheduled_works(worker);
2288 } else {
2289 move_linked_works(work, &worker->scheduled, NULL);
2290 process_scheduled_works(worker);
2291 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002292 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002293
Tejun Heoe22bee72010-06-29 10:07:14 +02002294 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002295sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002296 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002297 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002298
Tejun Heoc8e55f32010-06-29 10:07:12 +02002299 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002300 * pool->lock is held and there's no work to process and no need to
2301 * manage, sleep. Workers are woken up only while holding
2302 * pool->lock or from local cpu, so setting the current state
2303 * before releasing pool->lock is enough to prevent losing any
2304 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002305 */
2306 worker_enter_idle(worker);
2307 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002308 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002309 schedule();
2310 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Tejun Heoe22bee72010-06-29 10:07:14 +02002313/**
2314 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002315 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002316 *
2317 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002318 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 *
Tejun Heo706026c2013-01-24 11:01:34 -08002320 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002321 * worker which uses GFP_KERNEL allocation which has slight chance of
2322 * developing into deadlock if some works currently on the same queue
2323 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2324 * the problem rescuer solves.
2325 *
Tejun Heo706026c2013-01-24 11:01:34 -08002326 * When such condition is possible, the pool summons rescuers of all
2327 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002328 * those works so that forward progress can be guaranteed.
2329 *
2330 * This should happen rarely.
2331 */
Tejun Heo111c2252013-01-17 17:16:24 -08002332static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002333{
Tejun Heo111c2252013-01-17 17:16:24 -08002334 struct worker *rescuer = __rescuer;
2335 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002337
2338 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002339
2340 /*
2341 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2342 * doesn't participate in concurrency management.
2343 */
2344 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002345repeat:
2346 set_current_state(TASK_INTERRUPTIBLE);
2347
Mike Galbraith412d32e2012-11-28 07:17:18 +01002348 if (kthread_should_stop()) {
2349 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002350 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002351 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002352 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002353
Tejun Heo493a1722013-03-12 11:29:59 -07002354 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002355 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002356
2357 while (!list_empty(&wq->maydays)) {
2358 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2359 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002360 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002361 struct work_struct *work, *n;
2362
2363 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002364 list_del_init(&pwq->mayday_node);
2365
Tejun Heo2e109a22013-03-13 19:47:40 -07002366 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002367
2368 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002369 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002370 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002371
2372 /*
2373 * Slurp in all works issued via this workqueue and
2374 * process'em.
2375 */
Tejun Heo6183c002013-03-12 11:29:57 -07002376 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002377 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002378 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002379 move_linked_works(work, scheduled, &n);
2380
2381 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002382
2383 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002384 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002385 * regular worker; otherwise, we end up with 0 concurrency
2386 * and stalling the execution.
2387 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002388 if (keep_working(pool))
2389 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002390
Lai Jiangshanb3104102013-02-19 12:17:02 -08002391 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002392 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002393 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002394 }
2395
Tejun Heo2e109a22013-03-13 19:47:40 -07002396 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002397
Tejun Heo111c2252013-01-17 17:16:24 -08002398 /* rescuers should never participate in concurrency management */
2399 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002400 schedule();
2401 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002404struct wq_barrier {
2405 struct work_struct work;
2406 struct completion done;
2407};
2408
2409static void wq_barrier_func(struct work_struct *work)
2410{
2411 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2412 complete(&barr->done);
2413}
2414
Tejun Heo4690c4a2010-06-29 10:07:10 +02002415/**
2416 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002417 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002418 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002419 * @target: target work to attach @barr to
2420 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002421 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002422 * @barr is linked to @target such that @barr is completed only after
2423 * @target finishes execution. Please note that the ordering
2424 * guarantee is observed only with respect to @target and on the local
2425 * cpu.
2426 *
2427 * Currently, a queued barrier can't be canceled. This is because
2428 * try_to_grab_pending() can't determine whether the work to be
2429 * grabbed is at the head of the queue and thus can't clear LINKED
2430 * flag of the previous work while there must be a valid next work
2431 * after a work with LINKED flag set.
2432 *
2433 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002434 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002435 *
2436 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002437 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002438 */
Tejun Heo112202d2013-02-13 19:29:12 -08002439static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002440 struct wq_barrier *barr,
2441 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002442{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002443 struct list_head *head;
2444 unsigned int linked = 0;
2445
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002446 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002447 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002448 * as we know for sure that this will not trigger any of the
2449 * checks and call back into the fixup functions where we
2450 * might deadlock.
2451 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002452 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002453 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002454 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002455
Tejun Heoaffee4b2010-06-29 10:07:12 +02002456 /*
2457 * If @target is currently being executed, schedule the
2458 * barrier to the worker; otherwise, put it after @target.
2459 */
2460 if (worker)
2461 head = worker->scheduled.next;
2462 else {
2463 unsigned long *bits = work_data_bits(target);
2464
2465 head = target->entry.next;
2466 /* there can already be other linked works, inherit and set */
2467 linked = *bits & WORK_STRUCT_LINKED;
2468 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2469 }
2470
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002471 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002472 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002473 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002474}
2475
Tejun Heo73f53c42010-06-29 10:07:11 +02002476/**
Tejun Heo112202d2013-02-13 19:29:12 -08002477 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002478 * @wq: workqueue being flushed
2479 * @flush_color: new flush color, < 0 for no-op
2480 * @work_color: new work color, < 0 for no-op
2481 *
Tejun Heo112202d2013-02-13 19:29:12 -08002482 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002483 *
Tejun Heo112202d2013-02-13 19:29:12 -08002484 * If @flush_color is non-negative, flush_color on all pwqs should be
2485 * -1. If no pwq has in-flight commands at the specified color, all
2486 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2487 * has in flight commands, its pwq->flush_color is set to
2488 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002489 * wakeup logic is armed and %true is returned.
2490 *
2491 * The caller should have initialized @wq->first_flusher prior to
2492 * calling this function with non-negative @flush_color. If
2493 * @flush_color is negative, no flush color update is done and %false
2494 * is returned.
2495 *
Tejun Heo112202d2013-02-13 19:29:12 -08002496 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002497 * work_color which is previous to @work_color and all will be
2498 * advanced to @work_color.
2499 *
2500 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002501 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002502 *
2503 * RETURNS:
2504 * %true if @flush_color >= 0 and there's something to flush. %false
2505 * otherwise.
2506 */
Tejun Heo112202d2013-02-13 19:29:12 -08002507static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002511 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002512
Tejun Heo73f53c42010-06-29 10:07:11 +02002513 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002514 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002515 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002516 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002517
Tejun Heo49e3cf42013-03-12 11:29:58 -07002518 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002519 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002520
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002521 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002522
2523 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002524 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002525
Tejun Heo112202d2013-02-13 19:29:12 -08002526 if (pwq->nr_in_flight[flush_color]) {
2527 pwq->flush_color = flush_color;
2528 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002529 wait = true;
2530 }
2531 }
2532
2533 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002534 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002535 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002536 }
2537
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002538 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002539 }
2540
Tejun Heo112202d2013-02-13 19:29:12 -08002541 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002542 complete(&wq->first_flusher->done);
2543
2544 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002547/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002549 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002551 * This function sleeps until all work items which were queued on entry
2552 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002554void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 struct wq_flusher this_flusher = {
2557 .list = LIST_HEAD_INIT(this_flusher.list),
2558 .flush_color = -1,
2559 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2560 };
2561 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002562
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002563 lock_map_acquire(&wq->lockdep_map);
2564 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002565
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002566 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002567
2568 /*
2569 * Start-to-wait phase
2570 */
2571 next_color = work_next_color(wq->work_color);
2572
2573 if (next_color != wq->flush_color) {
2574 /*
2575 * Color space is not full. The current work_color
2576 * becomes our flush_color and work_color is advanced
2577 * by one.
2578 */
Tejun Heo6183c002013-03-12 11:29:57 -07002579 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002580 this_flusher.flush_color = wq->work_color;
2581 wq->work_color = next_color;
2582
2583 if (!wq->first_flusher) {
2584 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002585 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002586
2587 wq->first_flusher = &this_flusher;
2588
Tejun Heo112202d2013-02-13 19:29:12 -08002589 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002590 wq->work_color)) {
2591 /* nothing to flush, done */
2592 wq->flush_color = next_color;
2593 wq->first_flusher = NULL;
2594 goto out_unlock;
2595 }
2596 } else {
2597 /* wait in queue */
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 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002600 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002601 }
2602 } else {
2603 /*
2604 * Oops, color space is full, wait on overflow queue.
2605 * The next flush completion will assign us
2606 * flush_color and transfer to flusher_queue.
2607 */
2608 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2609 }
2610
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002611 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002612
2613 wait_for_completion(&this_flusher.done);
2614
2615 /*
2616 * Wake-up-and-cascade phase
2617 *
2618 * First flushers are responsible for cascading flushes and
2619 * handling overflow. Non-first flushers can simply return.
2620 */
2621 if (wq->first_flusher != &this_flusher)
2622 return;
2623
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002624 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002625
Tejun Heo4ce48b32010-07-02 10:03:51 +02002626 /* we might have raced, check again with mutex held */
2627 if (wq->first_flusher != &this_flusher)
2628 goto out_unlock;
2629
Tejun Heo73f53c42010-06-29 10:07:11 +02002630 wq->first_flusher = NULL;
2631
Tejun Heo6183c002013-03-12 11:29:57 -07002632 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2633 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002634
2635 while (true) {
2636 struct wq_flusher *next, *tmp;
2637
2638 /* complete all the flushers sharing the current flush color */
2639 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2640 if (next->flush_color != wq->flush_color)
2641 break;
2642 list_del_init(&next->list);
2643 complete(&next->done);
2644 }
2645
Tejun Heo6183c002013-03-12 11:29:57 -07002646 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2647 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002648
2649 /* this flush_color is finished, advance by one */
2650 wq->flush_color = work_next_color(wq->flush_color);
2651
2652 /* one color has been freed, handle overflow queue */
2653 if (!list_empty(&wq->flusher_overflow)) {
2654 /*
2655 * Assign the same color to all overflowed
2656 * flushers, advance work_color and append to
2657 * flusher_queue. This is the start-to-wait
2658 * phase for these overflowed flushers.
2659 */
2660 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2661 tmp->flush_color = wq->work_color;
2662
2663 wq->work_color = work_next_color(wq->work_color);
2664
2665 list_splice_tail_init(&wq->flusher_overflow,
2666 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002667 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002668 }
2669
2670 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002671 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002672 break;
2673 }
2674
2675 /*
2676 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002677 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002678 */
Tejun Heo6183c002013-03-12 11:29:57 -07002679 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2680 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002681
2682 list_del_init(&next->list);
2683 wq->first_flusher = next;
2684
Tejun Heo112202d2013-02-13 19:29:12 -08002685 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002686 break;
2687
2688 /*
2689 * Meh... this color is already done, clear first
2690 * flusher and repeat cascading.
2691 */
2692 wq->first_flusher = NULL;
2693 }
2694
2695out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002696 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
Dave Jonesae90dd52006-06-30 01:40:45 -04002698EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002700/**
2701 * drain_workqueue - drain a workqueue
2702 * @wq: workqueue to drain
2703 *
2704 * Wait until the workqueue becomes empty. While draining is in progress,
2705 * only chain queueing is allowed. IOW, only currently pending or running
2706 * work items on @wq can queue further work items on it. @wq is flushed
2707 * repeatedly until it becomes empty. The number of flushing is detemined
2708 * by the depth of chaining and should be relatively short. Whine if it
2709 * takes too long.
2710 */
2711void drain_workqueue(struct workqueue_struct *wq)
2712{
2713 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002714 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002715
2716 /*
2717 * __queue_work() needs to test whether there are drainers, is much
2718 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002719 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002721 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002722 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002723 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002724 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002725reflush:
2726 flush_workqueue(wq);
2727
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002728 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002729
Tejun Heo49e3cf42013-03-12 11:29:58 -07002730 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002731 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002732
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002733 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002734 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002735 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002736
2737 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002738 continue;
2739
2740 if (++flush_cnt == 10 ||
2741 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002742 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002743 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002744
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002745 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002746 goto reflush;
2747 }
2748
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002749 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002750 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002751 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002752}
2753EXPORT_SYMBOL_GPL(drain_workqueue);
2754
Tejun Heo606a5022012-08-20 14:51:23 -07002755static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002756{
2757 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002758 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002759 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002760
2761 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002762
Tejun Heofa1b54e2013-03-12 11:30:00 -07002763 local_irq_disable();
2764 pool = get_work_pool(work);
2765 if (!pool) {
2766 local_irq_enable();
2767 return false;
2768 }
2769
2770 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002771 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002772 pwq = get_work_pwq(work);
2773 if (pwq) {
2774 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002775 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002776 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002777 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002778 if (!worker)
2779 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002780 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002781 }
Tejun Heobaf59022010-09-16 10:42:16 +02002782
Tejun Heo112202d2013-02-13 19:29:12 -08002783 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002784 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002785
Tejun Heoe1594892011-01-09 23:32:15 +01002786 /*
2787 * If @max_active is 1 or rescuer is in use, flushing another work
2788 * item on the same workqueue may lead to deadlock. Make sure the
2789 * flusher is not running on the same workqueue by verifying write
2790 * access.
2791 */
Tejun Heo493008a2013-03-12 11:30:03 -07002792 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002793 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002794 else
Tejun Heo112202d2013-02-13 19:29:12 -08002795 lock_map_acquire_read(&pwq->wq->lockdep_map);
2796 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002797
Tejun Heobaf59022010-09-16 10:42:16 +02002798 return true;
2799already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002800 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002801 return false;
2802}
2803
Oleg Nesterovdb700892008-07-25 01:47:49 -07002804/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002805 * flush_work - wait for a work to finish executing the last queueing instance
2806 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002807 *
Tejun Heo606a5022012-08-20 14:51:23 -07002808 * Wait until @work has finished execution. @work is guaranteed to be idle
2809 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002810 *
2811 * RETURNS:
2812 * %true if flush_work() waited for the work to finish execution,
2813 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002814 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002815bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002816{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002817 struct wq_barrier barr;
2818
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002819 lock_map_acquire(&work->lockdep_map);
2820 lock_map_release(&work->lockdep_map);
2821
Tejun Heo606a5022012-08-20 14:51:23 -07002822 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002823 wait_for_completion(&barr.done);
2824 destroy_work_on_stack(&barr.work);
2825 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002826 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002827 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002828 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002829}
2830EXPORT_SYMBOL_GPL(flush_work);
2831
Tejun Heo36e227d2012-08-03 10:30:46 -07002832static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002833{
Tejun Heobbb68df2012-08-03 10:30:46 -07002834 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002835 int ret;
2836
2837 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002838 ret = try_to_grab_pending(work, is_dwork, &flags);
2839 /*
2840 * If someone else is canceling, wait for the same event it
2841 * would be waiting for before retrying.
2842 */
2843 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002844 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002845 } while (unlikely(ret < 0));
2846
Tejun Heobbb68df2012-08-03 10:30:46 -07002847 /* tell other tasks trying to grab @work to back off */
2848 mark_work_canceling(work);
2849 local_irq_restore(flags);
2850
Tejun Heo606a5022012-08-20 14:51:23 -07002851 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002852 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002853 return ret;
2854}
2855
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002856/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002857 * cancel_work_sync - cancel a work and wait for it to finish
2858 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002859 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002860 * Cancel @work and wait for its execution to finish. This function
2861 * can be used even if the work re-queues itself or migrates to
2862 * another workqueue. On return from this function, @work is
2863 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002864 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002865 * cancel_work_sync(&delayed_work->work) must not be used for
2866 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002867 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002868 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002869 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002870 *
2871 * RETURNS:
2872 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002873 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002874bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002875{
Tejun Heo36e227d2012-08-03 10:30:46 -07002876 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002877}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002878EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002879
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002880/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002881 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2882 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002883 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002884 * Delayed timer is cancelled and the pending work is queued for
2885 * immediate execution. Like flush_work(), this function only
2886 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002887 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * RETURNS:
2889 * %true if flush_work() waited for the work to finish execution,
2890 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002891 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002892bool flush_delayed_work(struct delayed_work *dwork)
2893{
Tejun Heo8930cab2012-08-03 10:30:45 -07002894 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002895 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002896 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002897 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002898 return flush_work(&dwork->work);
2899}
2900EXPORT_SYMBOL(flush_delayed_work);
2901
2902/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002903 * cancel_delayed_work - cancel a delayed work
2904 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002905 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002906 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2907 * and canceled; %false if wasn't pending. Note that the work callback
2908 * function may still be running on return, unless it returns %true and the
2909 * work doesn't re-arm itself. Explicitly flush or use
2910 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002911 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002912 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002913 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002914bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002915{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002916 unsigned long flags;
2917 int ret;
2918
2919 do {
2920 ret = try_to_grab_pending(&dwork->work, true, &flags);
2921 } while (unlikely(ret == -EAGAIN));
2922
2923 if (unlikely(ret < 0))
2924 return false;
2925
Tejun Heo7c3eed52013-01-24 11:01:33 -08002926 set_work_pool_and_clear_pending(&dwork->work,
2927 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002928 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002929 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002930}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002931EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002932
2933/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002934 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2935 * @dwork: the delayed work cancel
2936 *
2937 * This is cancel_work_sync() for delayed works.
2938 *
2939 * RETURNS:
2940 * %true if @dwork was pending, %false otherwise.
2941 */
2942bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002943{
Tejun Heo36e227d2012-08-03 10:30:46 -07002944 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002945}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002946EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002948/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002949 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002950 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002951 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002952 * schedule_on_each_cpu() executes @func on each online CPU using the
2953 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002954 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002955 *
2956 * RETURNS:
2957 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002958 */
David Howells65f27f32006-11-22 14:55:48 +00002959int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002960{
2961 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002962 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002963
Andrew Mortonb6136772006-06-25 05:47:49 -07002964 works = alloc_percpu(struct work_struct);
2965 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002966 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002967
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002968 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002969
Christoph Lameter15316ba2006-01-08 01:00:43 -08002970 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002971 struct work_struct *work = per_cpu_ptr(works, cpu);
2972
2973 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002974 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002975 }
Tejun Heo93981802009-11-17 14:06:20 -08002976
2977 for_each_online_cpu(cpu)
2978 flush_work(per_cpu_ptr(works, cpu));
2979
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002980 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002981 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002982 return 0;
2983}
2984
Alan Sterneef6a7d2010-02-12 17:39:21 +09002985/**
2986 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2987 *
2988 * Forces execution of the kernel-global workqueue and blocks until its
2989 * completion.
2990 *
2991 * Think twice before calling this function! It's very easy to get into
2992 * trouble if you don't take great care. Either of the following situations
2993 * will lead to deadlock:
2994 *
2995 * One of the work items currently on the workqueue needs to acquire
2996 * a lock held by your code or its caller.
2997 *
2998 * Your code is running in the context of a work routine.
2999 *
3000 * They will be detected by lockdep when they occur, but the first might not
3001 * occur very often. It depends on what work items are on the workqueue and
3002 * what locks they need, which you have no control over.
3003 *
3004 * In most situations flushing the entire workqueue is overkill; you merely
3005 * need to know that a particular work item isn't queued and isn't running.
3006 * In such cases you should use cancel_delayed_work_sync() or
3007 * cancel_work_sync() instead.
3008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009void flush_scheduled_work(void)
3010{
Tejun Heod320c032010-06-29 10:07:14 +02003011 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
Dave Jonesae90dd52006-06-30 01:40:45 -04003013EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003016 * execute_in_process_context - reliably execute the routine with user context
3017 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003018 * @ew: guaranteed storage for the execute work structure (must
3019 * be available when the work executes)
3020 *
3021 * Executes the function immediately if process context is available,
3022 * otherwise schedules the function for delayed execution.
3023 *
3024 * Returns: 0 - function was executed
3025 * 1 - function was scheduled for execution
3026 */
David Howells65f27f32006-11-22 14:55:48 +00003027int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003028{
3029 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003030 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003031 return 0;
3032 }
3033
David Howells65f27f32006-11-22 14:55:48 +00003034 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003035 schedule_work(&ew->work);
3036
3037 return 1;
3038}
3039EXPORT_SYMBOL_GPL(execute_in_process_context);
3040
Tejun Heo226223a2013-03-12 11:30:05 -07003041#ifdef CONFIG_SYSFS
3042/*
3043 * Workqueues with WQ_SYSFS flag set is visible to userland via
3044 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3045 * following attributes.
3046 *
3047 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3048 * max_active RW int : maximum number of in-flight work items
3049 *
3050 * Unbound workqueues have the following extra attributes.
3051 *
3052 * id RO int : the associated pool ID
3053 * nice RW int : nice value of the workers
3054 * cpumask RW mask : bitmask of allowed CPUs for the workers
3055 */
3056struct wq_device {
3057 struct workqueue_struct *wq;
3058 struct device dev;
3059};
3060
3061static struct workqueue_struct *dev_to_wq(struct device *dev)
3062{
3063 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3064
3065 return wq_dev->wq;
3066}
3067
3068static ssize_t wq_per_cpu_show(struct device *dev,
3069 struct device_attribute *attr, char *buf)
3070{
3071 struct workqueue_struct *wq = dev_to_wq(dev);
3072
3073 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3074}
3075
3076static ssize_t wq_max_active_show(struct device *dev,
3077 struct device_attribute *attr, char *buf)
3078{
3079 struct workqueue_struct *wq = dev_to_wq(dev);
3080
3081 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3082}
3083
3084static ssize_t wq_max_active_store(struct device *dev,
3085 struct device_attribute *attr,
3086 const char *buf, size_t count)
3087{
3088 struct workqueue_struct *wq = dev_to_wq(dev);
3089 int val;
3090
3091 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3092 return -EINVAL;
3093
3094 workqueue_set_max_active(wq, val);
3095 return count;
3096}
3097
3098static struct device_attribute wq_sysfs_attrs[] = {
3099 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3100 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3101 __ATTR_NULL,
3102};
3103
Tejun Heod55262c2013-04-01 11:23:38 -07003104static ssize_t wq_pool_ids_show(struct device *dev,
3105 struct device_attribute *attr, char *buf)
Tejun Heo226223a2013-03-12 11:30:05 -07003106{
3107 struct workqueue_struct *wq = dev_to_wq(dev);
Tejun Heod55262c2013-04-01 11:23:38 -07003108 const char *delim = "";
3109 int node, written = 0;
Tejun Heo226223a2013-03-12 11:30:05 -07003110
3111 rcu_read_lock_sched();
Tejun Heod55262c2013-04-01 11:23:38 -07003112 for_each_node(node) {
3113 written += scnprintf(buf + written, PAGE_SIZE - written,
3114 "%s%d:%d", delim, node,
3115 unbound_pwq_by_node(wq, node)->pool->id);
3116 delim = " ";
3117 }
3118 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
Tejun Heo226223a2013-03-12 11:30:05 -07003119 rcu_read_unlock_sched();
3120
3121 return written;
3122}
3123
3124static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3125 char *buf)
3126{
3127 struct workqueue_struct *wq = dev_to_wq(dev);
3128 int written;
3129
Tejun Heo6029a912013-04-01 11:23:34 -07003130 mutex_lock(&wq->mutex);
3131 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3132 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003133
3134 return written;
3135}
3136
3137/* prepare workqueue_attrs for sysfs store operations */
3138static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3139{
3140 struct workqueue_attrs *attrs;
3141
3142 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3143 if (!attrs)
3144 return NULL;
3145
Tejun Heo6029a912013-04-01 11:23:34 -07003146 mutex_lock(&wq->mutex);
3147 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3148 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003149 return attrs;
3150}
3151
3152static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3153 const char *buf, size_t count)
3154{
3155 struct workqueue_struct *wq = dev_to_wq(dev);
3156 struct workqueue_attrs *attrs;
3157 int ret;
3158
3159 attrs = wq_sysfs_prep_attrs(wq);
3160 if (!attrs)
3161 return -ENOMEM;
3162
3163 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3164 attrs->nice >= -20 && attrs->nice <= 19)
3165 ret = apply_workqueue_attrs(wq, attrs);
3166 else
3167 ret = -EINVAL;
3168
3169 free_workqueue_attrs(attrs);
3170 return ret ?: count;
3171}
3172
3173static ssize_t wq_cpumask_show(struct device *dev,
3174 struct device_attribute *attr, char *buf)
3175{
3176 struct workqueue_struct *wq = dev_to_wq(dev);
3177 int written;
3178
Tejun Heo6029a912013-04-01 11:23:34 -07003179 mutex_lock(&wq->mutex);
3180 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3181 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003182
3183 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3184 return written;
3185}
3186
3187static ssize_t wq_cpumask_store(struct device *dev,
3188 struct device_attribute *attr,
3189 const char *buf, size_t count)
3190{
3191 struct workqueue_struct *wq = dev_to_wq(dev);
3192 struct workqueue_attrs *attrs;
3193 int ret;
3194
3195 attrs = wq_sysfs_prep_attrs(wq);
3196 if (!attrs)
3197 return -ENOMEM;
3198
3199 ret = cpumask_parse(buf, attrs->cpumask);
3200 if (!ret)
3201 ret = apply_workqueue_attrs(wq, attrs);
3202
3203 free_workqueue_attrs(attrs);
3204 return ret ?: count;
3205}
3206
Tejun Heod55262c2013-04-01 11:23:38 -07003207static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3208 char *buf)
3209{
3210 struct workqueue_struct *wq = dev_to_wq(dev);
3211 int written;
3212
3213 mutex_lock(&wq->mutex);
3214 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3215 !wq->unbound_attrs->no_numa);
3216 mutex_unlock(&wq->mutex);
3217
3218 return written;
3219}
3220
3221static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3222 const char *buf, size_t count)
3223{
3224 struct workqueue_struct *wq = dev_to_wq(dev);
3225 struct workqueue_attrs *attrs;
3226 int v, ret;
3227
3228 attrs = wq_sysfs_prep_attrs(wq);
3229 if (!attrs)
3230 return -ENOMEM;
3231
3232 ret = -EINVAL;
3233 if (sscanf(buf, "%d", &v) == 1) {
3234 attrs->no_numa = !v;
3235 ret = apply_workqueue_attrs(wq, attrs);
3236 }
3237
3238 free_workqueue_attrs(attrs);
3239 return ret ?: count;
3240}
3241
Tejun Heo226223a2013-03-12 11:30:05 -07003242static struct device_attribute wq_sysfs_unbound_attrs[] = {
Tejun Heod55262c2013-04-01 11:23:38 -07003243 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
Tejun Heo226223a2013-03-12 11:30:05 -07003244 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3245 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
Tejun Heod55262c2013-04-01 11:23:38 -07003246 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
Tejun Heo226223a2013-03-12 11:30:05 -07003247 __ATTR_NULL,
3248};
3249
3250static struct bus_type wq_subsys = {
3251 .name = "workqueue",
3252 .dev_attrs = wq_sysfs_attrs,
3253};
3254
3255static int __init wq_sysfs_init(void)
3256{
3257 return subsys_virtual_register(&wq_subsys, NULL);
3258}
3259core_initcall(wq_sysfs_init);
3260
3261static void wq_device_release(struct device *dev)
3262{
3263 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3264
3265 kfree(wq_dev);
3266}
3267
3268/**
3269 * workqueue_sysfs_register - make a workqueue visible in sysfs
3270 * @wq: the workqueue to register
3271 *
3272 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3273 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3274 * which is the preferred method.
3275 *
3276 * Workqueue user should use this function directly iff it wants to apply
3277 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3278 * apply_workqueue_attrs() may race against userland updating the
3279 * attributes.
3280 *
3281 * Returns 0 on success, -errno on failure.
3282 */
3283int workqueue_sysfs_register(struct workqueue_struct *wq)
3284{
3285 struct wq_device *wq_dev;
3286 int ret;
3287
3288 /*
3289 * Adjusting max_active or creating new pwqs by applyting
3290 * attributes breaks ordering guarantee. Disallow exposing ordered
3291 * workqueues.
3292 */
3293 if (WARN_ON(wq->flags & __WQ_ORDERED))
3294 return -EINVAL;
3295
3296 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3297 if (!wq_dev)
3298 return -ENOMEM;
3299
3300 wq_dev->wq = wq;
3301 wq_dev->dev.bus = &wq_subsys;
3302 wq_dev->dev.init_name = wq->name;
3303 wq_dev->dev.release = wq_device_release;
3304
3305 /*
3306 * unbound_attrs are created separately. Suppress uevent until
3307 * everything is ready.
3308 */
3309 dev_set_uevent_suppress(&wq_dev->dev, true);
3310
3311 ret = device_register(&wq_dev->dev);
3312 if (ret) {
3313 kfree(wq_dev);
3314 wq->wq_dev = NULL;
3315 return ret;
3316 }
3317
3318 if (wq->flags & WQ_UNBOUND) {
3319 struct device_attribute *attr;
3320
3321 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3322 ret = device_create_file(&wq_dev->dev, attr);
3323 if (ret) {
3324 device_unregister(&wq_dev->dev);
3325 wq->wq_dev = NULL;
3326 return ret;
3327 }
3328 }
3329 }
3330
3331 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3332 return 0;
3333}
3334
3335/**
3336 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3337 * @wq: the workqueue to unregister
3338 *
3339 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3340 */
3341static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3342{
3343 struct wq_device *wq_dev = wq->wq_dev;
3344
3345 if (!wq->wq_dev)
3346 return;
3347
3348 wq->wq_dev = NULL;
3349 device_unregister(&wq_dev->dev);
3350}
3351#else /* CONFIG_SYSFS */
3352static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3353#endif /* CONFIG_SYSFS */
3354
Tejun Heo7a4e3442013-03-12 11:30:00 -07003355/**
3356 * free_workqueue_attrs - free a workqueue_attrs
3357 * @attrs: workqueue_attrs to free
3358 *
3359 * Undo alloc_workqueue_attrs().
3360 */
3361void free_workqueue_attrs(struct workqueue_attrs *attrs)
3362{
3363 if (attrs) {
3364 free_cpumask_var(attrs->cpumask);
3365 kfree(attrs);
3366 }
3367}
3368
3369/**
3370 * alloc_workqueue_attrs - allocate a workqueue_attrs
3371 * @gfp_mask: allocation mask to use
3372 *
3373 * Allocate a new workqueue_attrs, initialize with default settings and
3374 * return it. Returns NULL on failure.
3375 */
3376struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3377{
3378 struct workqueue_attrs *attrs;
3379
3380 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3381 if (!attrs)
3382 goto fail;
3383 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3384 goto fail;
3385
Tejun Heo13e2e552013-04-01 11:23:31 -07003386 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003387 return attrs;
3388fail:
3389 free_workqueue_attrs(attrs);
3390 return NULL;
3391}
3392
Tejun Heo29c91e92013-03-12 11:30:03 -07003393static void copy_workqueue_attrs(struct workqueue_attrs *to,
3394 const struct workqueue_attrs *from)
3395{
3396 to->nice = from->nice;
3397 cpumask_copy(to->cpumask, from->cpumask);
3398}
3399
Tejun Heo29c91e92013-03-12 11:30:03 -07003400/* hash value of the content of @attr */
3401static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3402{
3403 u32 hash = 0;
3404
3405 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003406 hash = jhash(cpumask_bits(attrs->cpumask),
3407 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003408 return hash;
3409}
3410
3411/* content equality test */
3412static bool wqattrs_equal(const struct workqueue_attrs *a,
3413 const struct workqueue_attrs *b)
3414{
3415 if (a->nice != b->nice)
3416 return false;
3417 if (!cpumask_equal(a->cpumask, b->cpumask))
3418 return false;
3419 return true;
3420}
3421
Tejun Heo7a4e3442013-03-12 11:30:00 -07003422/**
3423 * init_worker_pool - initialize a newly zalloc'd worker_pool
3424 * @pool: worker_pool to initialize
3425 *
3426 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003427 * Returns 0 on success, -errno on failure. Even on failure, all fields
3428 * inside @pool proper are initialized and put_unbound_pool() can be called
3429 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003430 */
3431static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003432{
3433 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003434 pool->id = -1;
3435 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003436 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003437 pool->flags |= POOL_DISASSOCIATED;
3438 INIT_LIST_HEAD(&pool->worklist);
3439 INIT_LIST_HEAD(&pool->idle_list);
3440 hash_init(pool->busy_hash);
3441
3442 init_timer_deferrable(&pool->idle_timer);
3443 pool->idle_timer.function = idle_worker_timeout;
3444 pool->idle_timer.data = (unsigned long)pool;
3445
3446 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3447 (unsigned long)pool);
3448
3449 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003450 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003451 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003452
Tejun Heo29c91e92013-03-12 11:30:03 -07003453 INIT_HLIST_NODE(&pool->hash_node);
3454 pool->refcnt = 1;
3455
3456 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003457 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3458 if (!pool->attrs)
3459 return -ENOMEM;
3460 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003461}
3462
Tejun Heo29c91e92013-03-12 11:30:03 -07003463static void rcu_free_pool(struct rcu_head *rcu)
3464{
3465 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3466
Tejun Heo822d8402013-03-19 13:45:21 -07003467 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003468 free_workqueue_attrs(pool->attrs);
3469 kfree(pool);
3470}
3471
3472/**
3473 * put_unbound_pool - put a worker_pool
3474 * @pool: worker_pool to put
3475 *
3476 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003477 * safe manner. get_unbound_pool() calls this function on its failure path
3478 * and this function should be able to release pools which went through,
3479 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003480 *
3481 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003482 */
3483static void put_unbound_pool(struct worker_pool *pool)
3484{
3485 struct worker *worker;
3486
Tejun Heoa892cac2013-04-01 11:23:32 -07003487 lockdep_assert_held(&wq_pool_mutex);
3488
3489 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003490 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003491
3492 /* sanity checks */
3493 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003494 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003495 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003496
3497 /* release id and unhash */
3498 if (pool->id >= 0)
3499 idr_remove(&worker_pool_idr, pool->id);
3500 hash_del(&pool->hash_node);
3501
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003502 /*
3503 * Become the manager and destroy all workers. Grabbing
3504 * manager_arb prevents @pool's workers from blocking on
3505 * manager_mutex.
3506 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003507 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003508 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003509 spin_lock_irq(&pool->lock);
3510
3511 while ((worker = first_worker(pool)))
3512 destroy_worker(worker);
3513 WARN_ON(pool->nr_workers || pool->nr_idle);
3514
3515 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003516 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003517 mutex_unlock(&pool->manager_arb);
3518
3519 /* shut down the timers */
3520 del_timer_sync(&pool->idle_timer);
3521 del_timer_sync(&pool->mayday_timer);
3522
3523 /* sched-RCU protected to allow dereferences from get_work_pool() */
3524 call_rcu_sched(&pool->rcu, rcu_free_pool);
3525}
3526
3527/**
3528 * get_unbound_pool - get a worker_pool with the specified attributes
3529 * @attrs: the attributes of the worker_pool to get
3530 *
3531 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3532 * reference count and return it. If there already is a matching
3533 * worker_pool, it will be used; otherwise, this function attempts to
3534 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003535 *
3536 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003537 */
3538static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3539{
Tejun Heo29c91e92013-03-12 11:30:03 -07003540 u32 hash = wqattrs_hash(attrs);
3541 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003542 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003543
Tejun Heoa892cac2013-04-01 11:23:32 -07003544 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003545
3546 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003547 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3548 if (wqattrs_equal(pool->attrs, attrs)) {
3549 pool->refcnt++;
3550 goto out_unlock;
3551 }
3552 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003553
3554 /* nope, create a new one */
3555 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3556 if (!pool || init_worker_pool(pool) < 0)
3557 goto fail;
3558
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003559 if (workqueue_freezing)
3560 pool->flags |= POOL_FREEZING;
3561
Tejun Heo8864b4e2013-03-12 11:30:04 -07003562 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003563 copy_workqueue_attrs(pool->attrs, attrs);
3564
Tejun Heof3f90ad2013-04-01 11:23:34 -07003565 /* if cpumask is contained inside a NUMA node, we belong to that node */
3566 if (wq_numa_enabled) {
3567 for_each_node(node) {
3568 if (cpumask_subset(pool->attrs->cpumask,
3569 wq_numa_possible_cpumask[node])) {
3570 pool->node = node;
3571 break;
3572 }
3573 }
3574 }
3575
Tejun Heo29c91e92013-03-12 11:30:03 -07003576 if (worker_pool_assign_id(pool) < 0)
3577 goto fail;
3578
3579 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003580 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003581 goto fail;
3582
Tejun Heo29c91e92013-03-12 11:30:03 -07003583 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003584 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3585out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003586 return pool;
3587fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003588 if (pool)
3589 put_unbound_pool(pool);
3590 return NULL;
3591}
3592
Tejun Heo8864b4e2013-03-12 11:30:04 -07003593static void rcu_free_pwq(struct rcu_head *rcu)
3594{
3595 kmem_cache_free(pwq_cache,
3596 container_of(rcu, struct pool_workqueue, rcu));
3597}
3598
3599/*
3600 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3601 * and needs to be destroyed.
3602 */
3603static void pwq_unbound_release_workfn(struct work_struct *work)
3604{
3605 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3606 unbound_release_work);
3607 struct workqueue_struct *wq = pwq->wq;
3608 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003609 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003610
3611 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3612 return;
3613
Tejun Heo75ccf592013-03-12 11:30:04 -07003614 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003615 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003616 * necessary on release but do it anyway. It's easier to verify
3617 * and consistent with the linking path.
3618 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003619 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003620 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003621 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003622 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003623
Tejun Heoa892cac2013-04-01 11:23:32 -07003624 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003625 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003626 mutex_unlock(&wq_pool_mutex);
3627
Tejun Heo8864b4e2013-03-12 11:30:04 -07003628 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3629
3630 /*
3631 * If we're the last pwq going away, @wq is already dead and no one
3632 * is gonna access it anymore. Free it.
3633 */
Tejun Heo6029a912013-04-01 11:23:34 -07003634 if (is_last) {
3635 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003636 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003637 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003638}
3639
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003640/**
Tejun Heo699ce092013-03-13 16:51:35 -07003641 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003642 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003643 *
Tejun Heo699ce092013-03-13 16:51:35 -07003644 * If @pwq isn't freezing, set @pwq->max_active to the associated
3645 * workqueue's saved_max_active and activate delayed work items
3646 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003647 */
Tejun Heo699ce092013-03-13 16:51:35 -07003648static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003649{
Tejun Heo699ce092013-03-13 16:51:35 -07003650 struct workqueue_struct *wq = pwq->wq;
3651 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003652
Tejun Heo699ce092013-03-13 16:51:35 -07003653 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003654 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003655
3656 /* fast exit for non-freezable wqs */
3657 if (!freezable && pwq->max_active == wq->saved_max_active)
3658 return;
3659
Lai Jiangshana357fc02013-03-25 16:57:19 -07003660 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003661
3662 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3663 pwq->max_active = wq->saved_max_active;
3664
3665 while (!list_empty(&pwq->delayed_works) &&
3666 pwq->nr_active < pwq->max_active)
3667 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003668
3669 /*
3670 * Need to kick a worker after thawed or an unbound wq's
3671 * max_active is bumped. It's a slow path. Do it always.
3672 */
3673 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003674 } else {
3675 pwq->max_active = 0;
3676 }
3677
Lai Jiangshana357fc02013-03-25 16:57:19 -07003678 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003679}
3680
Tejun Heoe50aba92013-04-01 11:23:35 -07003681/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003682static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3683 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003684{
3685 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3686
Tejun Heoe50aba92013-04-01 11:23:35 -07003687 memset(pwq, 0, sizeof(*pwq));
3688
Tejun Heod2c1d402013-03-12 11:30:04 -07003689 pwq->pool = pool;
3690 pwq->wq = wq;
3691 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003692 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003693 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003694 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003695 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003696 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003697}
Tejun Heod2c1d402013-03-12 11:30:04 -07003698
Tejun Heof147f292013-04-01 11:23:35 -07003699/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003700static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003701{
3702 struct workqueue_struct *wq = pwq->wq;
3703
3704 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003705
Tejun Heo1befcf32013-04-01 11:23:35 -07003706 /* may be called multiple times, ignore if already linked */
3707 if (!list_empty(&pwq->pwqs_node))
3708 return;
3709
Tejun Heo983ca252013-03-13 16:51:35 -07003710 /*
3711 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003712 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003713 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003714 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003715
3716 /* sync max_active to the current setting */
3717 pwq_adjust_max_active(pwq);
3718
3719 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003720 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003721}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003722
Tejun Heof147f292013-04-01 11:23:35 -07003723/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3724static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3725 const struct workqueue_attrs *attrs)
3726{
3727 struct worker_pool *pool;
3728 struct pool_workqueue *pwq;
3729
3730 lockdep_assert_held(&wq_pool_mutex);
3731
3732 pool = get_unbound_pool(attrs);
3733 if (!pool)
3734 return NULL;
3735
Tejun Heoe50aba92013-04-01 11:23:35 -07003736 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003737 if (!pwq) {
3738 put_unbound_pool(pool);
3739 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003740 }
Tejun Heo6029a912013-04-01 11:23:34 -07003741
Tejun Heof147f292013-04-01 11:23:35 -07003742 init_pwq(pwq, wq, pool);
3743 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003744}
3745
Tejun Heo4c16bd32013-04-01 11:23:36 -07003746/* undo alloc_unbound_pwq(), used only in the error path */
3747static void free_unbound_pwq(struct pool_workqueue *pwq)
3748{
3749 lockdep_assert_held(&wq_pool_mutex);
3750
3751 if (pwq) {
3752 put_unbound_pool(pwq->pool);
Wei Yongjuncece95d2013-04-09 14:29:11 +08003753 kmem_cache_free(pwq_cache, pwq);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003754 }
3755}
3756
3757/**
3758 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3759 * @attrs: the wq_attrs of interest
3760 * @node: the target NUMA node
3761 * @cpu_going_down: if >= 0, the CPU to consider as offline
3762 * @cpumask: outarg, the resulting cpumask
3763 *
3764 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3765 * @cpu_going_down is >= 0, that cpu is considered offline during
3766 * calculation. The result is stored in @cpumask. This function returns
3767 * %true if the resulting @cpumask is different from @attrs->cpumask,
3768 * %false if equal.
3769 *
3770 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3771 * enabled and @node has online CPUs requested by @attrs, the returned
3772 * cpumask is the intersection of the possible CPUs of @node and
3773 * @attrs->cpumask.
3774 *
3775 * The caller is responsible for ensuring that the cpumask of @node stays
3776 * stable.
3777 */
3778static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3779 int cpu_going_down, cpumask_t *cpumask)
3780{
Tejun Heod55262c2013-04-01 11:23:38 -07003781 if (!wq_numa_enabled || attrs->no_numa)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003782 goto use_dfl;
3783
3784 /* does @node have any online CPUs @attrs wants? */
3785 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3786 if (cpu_going_down >= 0)
3787 cpumask_clear_cpu(cpu_going_down, cpumask);
3788
3789 if (cpumask_empty(cpumask))
3790 goto use_dfl;
3791
3792 /* yeap, return possible CPUs in @node that @attrs wants */
3793 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3794 return !cpumask_equal(cpumask, attrs->cpumask);
3795
3796use_dfl:
3797 cpumask_copy(cpumask, attrs->cpumask);
3798 return false;
3799}
3800
Tejun Heo1befcf32013-04-01 11:23:35 -07003801/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3802static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3803 int node,
3804 struct pool_workqueue *pwq)
3805{
3806 struct pool_workqueue *old_pwq;
3807
3808 lockdep_assert_held(&wq->mutex);
3809
3810 /* link_pwq() can handle duplicate calls */
3811 link_pwq(pwq);
3812
3813 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3814 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3815 return old_pwq;
3816}
3817
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003818/**
3819 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3820 * @wq: the target workqueue
3821 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3822 *
Tejun Heo4c16bd32013-04-01 11:23:36 -07003823 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3824 * machines, this function maps a separate pwq to each NUMA node with
3825 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3826 * NUMA node it was issued on. Older pwqs are released as in-flight work
3827 * items finish. Note that a work item which repeatedly requeues itself
3828 * back-to-back will stay on its current pwq.
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003829 *
3830 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3831 * failure.
3832 */
3833int apply_workqueue_attrs(struct workqueue_struct *wq,
3834 const struct workqueue_attrs *attrs)
3835{
Tejun Heo4c16bd32013-04-01 11:23:36 -07003836 struct workqueue_attrs *new_attrs, *tmp_attrs;
3837 struct pool_workqueue **pwq_tbl, *dfl_pwq;
Tejun Heof147f292013-04-01 11:23:35 -07003838 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003839
Tejun Heo8719dce2013-03-12 11:30:04 -07003840 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003841 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3842 return -EINVAL;
3843
Tejun Heo8719dce2013-03-12 11:30:04 -07003844 /* creating multiple pwqs breaks ordering guarantee */
3845 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3846 return -EINVAL;
3847
Tejun Heo4c16bd32013-04-01 11:23:36 -07003848 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
Tejun Heo13e2e552013-04-01 11:23:31 -07003849 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003850 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3851 if (!pwq_tbl || !new_attrs || !tmp_attrs)
Tejun Heo13e2e552013-04-01 11:23:31 -07003852 goto enomem;
3853
Tejun Heo4c16bd32013-04-01 11:23:36 -07003854 /* make a copy of @attrs and sanitize it */
Tejun Heo13e2e552013-04-01 11:23:31 -07003855 copy_workqueue_attrs(new_attrs, attrs);
3856 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3857
Tejun Heo4c16bd32013-04-01 11:23:36 -07003858 /*
3859 * We may create multiple pwqs with differing cpumasks. Make a
3860 * copy of @new_attrs which will be modified and used to obtain
3861 * pools.
3862 */
3863 copy_workqueue_attrs(tmp_attrs, new_attrs);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003864
Tejun Heo4c16bd32013-04-01 11:23:36 -07003865 /*
3866 * CPUs should stay stable across pwq creations and installations.
3867 * Pin CPUs, determine the target cpumask for each node and create
3868 * pwqs accordingly.
3869 */
3870 get_online_cpus();
3871
3872 mutex_lock(&wq_pool_mutex);
3873
3874 /*
3875 * If something goes wrong during CPU up/down, we'll fall back to
3876 * the default pwq covering whole @attrs->cpumask. Always create
3877 * it even if we don't use it immediately.
3878 */
3879 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3880 if (!dfl_pwq)
3881 goto enomem_pwq;
3882
3883 for_each_node(node) {
3884 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3885 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3886 if (!pwq_tbl[node])
3887 goto enomem_pwq;
3888 } else {
3889 dfl_pwq->refcnt++;
3890 pwq_tbl[node] = dfl_pwq;
3891 }
3892 }
3893
3894 mutex_unlock(&wq_pool_mutex);
3895
3896 /* all pwqs have been created successfully, let's install'em */
Tejun Heof147f292013-04-01 11:23:35 -07003897 mutex_lock(&wq->mutex);
3898
Tejun Heof147f292013-04-01 11:23:35 -07003899 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003900
3901 /* save the previous pwq and install the new one */
Tejun Heof147f292013-04-01 11:23:35 -07003902 for_each_node(node)
Tejun Heo4c16bd32013-04-01 11:23:36 -07003903 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3904
3905 /* @dfl_pwq might not have been used, ensure it's linked */
3906 link_pwq(dfl_pwq);
3907 swap(wq->dfl_pwq, dfl_pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003908
3909 mutex_unlock(&wq->mutex);
3910
Tejun Heo4c16bd32013-04-01 11:23:36 -07003911 /* put the old pwqs */
3912 for_each_node(node)
3913 put_pwq_unlocked(pwq_tbl[node]);
3914 put_pwq_unlocked(dfl_pwq);
3915
3916 put_online_cpus();
Tejun Heo48621252013-04-01 11:23:31 -07003917 ret = 0;
3918 /* fall through */
3919out_free:
Tejun Heo4c16bd32013-04-01 11:23:36 -07003920 free_workqueue_attrs(tmp_attrs);
Tejun Heo48621252013-04-01 11:23:31 -07003921 free_workqueue_attrs(new_attrs);
Tejun Heo4c16bd32013-04-01 11:23:36 -07003922 kfree(pwq_tbl);
Tejun Heo48621252013-04-01 11:23:31 -07003923 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003924
Tejun Heo4c16bd32013-04-01 11:23:36 -07003925enomem_pwq:
3926 free_unbound_pwq(dfl_pwq);
3927 for_each_node(node)
3928 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3929 free_unbound_pwq(pwq_tbl[node]);
3930 mutex_unlock(&wq_pool_mutex);
3931 put_online_cpus();
Tejun Heo13e2e552013-04-01 11:23:31 -07003932enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003933 ret = -ENOMEM;
3934 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003935}
3936
Tejun Heo4c16bd32013-04-01 11:23:36 -07003937/**
3938 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3939 * @wq: the target workqueue
3940 * @cpu: the CPU coming up or going down
3941 * @online: whether @cpu is coming up or going down
3942 *
3943 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3944 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3945 * @wq accordingly.
3946 *
3947 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3948 * falls back to @wq->dfl_pwq which may not be optimal but is always
3949 * correct.
3950 *
3951 * Note that when the last allowed CPU of a NUMA node goes offline for a
3952 * workqueue with a cpumask spanning multiple nodes, the workers which were
3953 * already executing the work items for the workqueue will lose their CPU
3954 * affinity and may execute on any CPU. This is similar to how per-cpu
3955 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3956 * affinity, it's the user's responsibility to flush the work item from
3957 * CPU_DOWN_PREPARE.
3958 */
3959static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3960 bool online)
3961{
3962 int node = cpu_to_node(cpu);
3963 int cpu_off = online ? -1 : cpu;
3964 struct pool_workqueue *old_pwq = NULL, *pwq;
3965 struct workqueue_attrs *target_attrs;
3966 cpumask_t *cpumask;
3967
3968 lockdep_assert_held(&wq_pool_mutex);
3969
3970 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3971 return;
3972
3973 /*
3974 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3975 * Let's use a preallocated one. The following buf is protected by
3976 * CPU hotplug exclusion.
3977 */
3978 target_attrs = wq_update_unbound_numa_attrs_buf;
3979 cpumask = target_attrs->cpumask;
3980
3981 mutex_lock(&wq->mutex);
Tejun Heod55262c2013-04-01 11:23:38 -07003982 if (wq->unbound_attrs->no_numa)
3983 goto out_unlock;
Tejun Heo4c16bd32013-04-01 11:23:36 -07003984
3985 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3986 pwq = unbound_pwq_by_node(wq, node);
3987
3988 /*
3989 * Let's determine what needs to be done. If the target cpumask is
3990 * different from wq's, we need to compare it to @pwq's and create
3991 * a new one if they don't match. If the target cpumask equals
3992 * wq's, the default pwq should be used. If @pwq is already the
3993 * default one, nothing to do; otherwise, install the default one.
3994 */
3995 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3996 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
3997 goto out_unlock;
3998 } else {
3999 if (pwq == wq->dfl_pwq)
4000 goto out_unlock;
4001 else
4002 goto use_dfl_pwq;
4003 }
4004
4005 mutex_unlock(&wq->mutex);
4006
4007 /* create a new pwq */
4008 pwq = alloc_unbound_pwq(wq, target_attrs);
4009 if (!pwq) {
4010 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4011 wq->name);
4012 goto out_unlock;
4013 }
4014
4015 /*
4016 * Install the new pwq. As this function is called only from CPU
4017 * hotplug callbacks and applying a new attrs is wrapped with
4018 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4019 * inbetween.
4020 */
4021 mutex_lock(&wq->mutex);
4022 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4023 goto out_unlock;
4024
4025use_dfl_pwq:
4026 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4027 get_pwq(wq->dfl_pwq);
4028 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4029 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4030out_unlock:
4031 mutex_unlock(&wq->mutex);
4032 put_pwq_unlocked(old_pwq);
4033}
4034
Tejun Heo30cdf242013-03-12 11:29:57 -07004035static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004037 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07004038 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01004039
Tejun Heo30cdf242013-03-12 11:29:57 -07004040 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07004041 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4042 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07004043 return -ENOMEM;
4044
4045 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004046 struct pool_workqueue *pwq =
4047 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07004048 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07004049 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07004050
Tejun Heof147f292013-04-01 11:23:35 -07004051 init_pwq(pwq, wq, &cpu_pools[highpri]);
4052
4053 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07004054 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07004055 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07004056 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004057 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07004058 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07004059 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07004060 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004061}
4062
Tejun Heof3421792010-07-02 10:03:51 +02004063static int wq_clamp_max_active(int max_active, unsigned int flags,
4064 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02004065{
Tejun Heof3421792010-07-02 10:03:51 +02004066 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
4067
4068 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03004069 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
4070 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004071
Tejun Heof3421792010-07-02 10:03:51 +02004072 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02004073}
4074
Tejun Heob196be82012-01-10 15:11:35 -08004075struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02004076 unsigned int flags,
4077 int max_active,
4078 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08004079 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004080{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004081 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07004082 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004083 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07004084 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08004085
Tejun Heoecf68812013-04-01 11:23:34 -07004086 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004087 if (flags & WQ_UNBOUND)
4088 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
4089
4090 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08004091 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07004092 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08004093
Tejun Heo6029a912013-04-01 11:23:34 -07004094 if (flags & WQ_UNBOUND) {
4095 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
4096 if (!wq->unbound_attrs)
4097 goto err_free_wq;
4098 }
4099
Tejun Heoecf68812013-04-01 11:23:34 -07004100 va_start(args, lock_name);
4101 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08004102 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004103
Tejun Heod320c032010-06-29 10:07:14 +02004104 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08004105 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004106
Tejun Heob196be82012-01-10 15:11:35 -08004107 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02004108 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004109 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07004110 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08004111 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07004112 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02004113 INIT_LIST_HEAD(&wq->flusher_queue);
4114 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07004115 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004116
Johannes Bergeb13ba82008-01-16 09:51:58 +01004117 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07004118 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004119
Tejun Heo30cdf242013-03-12 11:29:57 -07004120 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07004121 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004122
Tejun Heo493008a2013-03-12 11:30:03 -07004123 /*
4124 * Workqueues which may be used during memory reclaim should
4125 * have a rescuer to guarantee forward progress.
4126 */
4127 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004128 struct worker *rescuer;
4129
Tejun Heod2c1d402013-03-12 11:30:04 -07004130 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02004131 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07004132 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02004133
Tejun Heo111c2252013-01-17 17:16:24 -08004134 rescuer->rescue_wq = wq;
4135 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08004136 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07004137 if (IS_ERR(rescuer->task)) {
4138 kfree(rescuer);
4139 goto err_destroy;
4140 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004141
Tejun Heod2c1d402013-03-12 11:30:04 -07004142 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07004143 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02004144 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004145 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004146
Tejun Heo226223a2013-03-12 11:30:05 -07004147 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4148 goto err_destroy;
4149
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004150 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004151 * wq_pool_mutex protects global freeze state and workqueues list.
4152 * Grab it, adjust max_active and add the new @wq to workqueues
4153 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004154 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004155 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004156
Lai Jiangshana357fc02013-03-25 16:57:19 -07004157 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004158 for_each_pwq(pwq, wq)
4159 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004160 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004161
Tejun Heo15376632010-06-29 10:07:11 +02004162 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004163
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004164 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02004165
Oleg Nesterov3af244332007-05-09 02:34:09 -07004166 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07004167
4168err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07004169 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07004170 kfree(wq);
4171 return NULL;
4172err_destroy:
4173 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02004174 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004175}
Tejun Heod320c032010-06-29 10:07:14 +02004176EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004177
4178/**
4179 * destroy_workqueue - safely terminate a workqueue
4180 * @wq: target workqueue
4181 *
4182 * Safely destroy a workqueue. All work currently pending will be done first.
4183 */
4184void destroy_workqueue(struct workqueue_struct *wq)
4185{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004186 struct pool_workqueue *pwq;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004187 int node;
Oleg Nesterov3af244332007-05-09 02:34:09 -07004188
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02004189 /* drain it before proceeding with destruction */
4190 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01004191
Tejun Heo6183c002013-03-12 11:29:57 -07004192 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004193 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07004194 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004195 int i;
4196
Tejun Heo76af4d92013-03-12 11:30:00 -07004197 for (i = 0; i < WORK_NR_COLORS; i++) {
4198 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004199 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004200 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004201 }
4202 }
4203
Lai Jiangshan5c529592013-04-04 10:05:38 +08004204 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
Tejun Heo8864b4e2013-03-12 11:30:04 -07004205 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07004206 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004207 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004208 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07004209 }
Tejun Heo6183c002013-03-12 11:29:57 -07004210 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07004211 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07004212
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004213 /*
4214 * wq list is used to freeze wq, remove from list after
4215 * flushing is complete in case freeze races us.
4216 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004217 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07004218 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004219 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07004220
Tejun Heo226223a2013-03-12 11:30:05 -07004221 workqueue_sysfs_unregister(wq);
4222
Tejun Heo493008a2013-03-12 11:30:03 -07004223 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004224 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02004225 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07004226 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02004227 }
4228
Tejun Heo8864b4e2013-03-12 11:30:04 -07004229 if (!(wq->flags & WQ_UNBOUND)) {
4230 /*
4231 * The base ref is never dropped on per-cpu pwqs. Directly
4232 * free the pwqs and wq.
4233 */
4234 free_percpu(wq->cpu_pwqs);
4235 kfree(wq);
4236 } else {
4237 /*
4238 * We're the sole accessor of @wq at this point. Directly
Tejun Heo4c16bd32013-04-01 11:23:36 -07004239 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
4240 * @wq will be freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07004241 */
Tejun Heo4c16bd32013-04-01 11:23:36 -07004242 for_each_node(node) {
4243 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
4244 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
4245 put_pwq_unlocked(pwq);
4246 }
4247
4248 /*
4249 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
4250 * put. Don't access it afterwards.
4251 */
4252 pwq = wq->dfl_pwq;
4253 wq->dfl_pwq = NULL;
Tejun Heodce90d42013-04-01 11:23:35 -07004254 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004255 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004256}
4257EXPORT_SYMBOL_GPL(destroy_workqueue);
4258
Tejun Heodcd989c2010-06-29 10:07:14 +02004259/**
4260 * workqueue_set_max_active - adjust max_active of a workqueue
4261 * @wq: target workqueue
4262 * @max_active: new max_active value.
4263 *
4264 * Set max_active of @wq to @max_active.
4265 *
4266 * CONTEXT:
4267 * Don't call from IRQ context.
4268 */
4269void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4270{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004271 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004272
Tejun Heo8719dce2013-03-12 11:30:04 -07004273 /* disallow meddling with max_active for ordered workqueues */
4274 if (WARN_ON(wq->flags & __WQ_ORDERED))
4275 return;
4276
Tejun Heof3421792010-07-02 10:03:51 +02004277 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004278
Lai Jiangshana357fc02013-03-25 16:57:19 -07004279 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004280
4281 wq->saved_max_active = max_active;
4282
Tejun Heo699ce092013-03-13 16:51:35 -07004283 for_each_pwq(pwq, wq)
4284 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004285
Lai Jiangshana357fc02013-03-25 16:57:19 -07004286 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004287}
4288EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4289
4290/**
Tejun Heoe62676162013-03-12 17:41:37 -07004291 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4292 *
4293 * Determine whether %current is a workqueue rescuer. Can be used from
4294 * work functions to determine whether it's being run off the rescuer task.
4295 */
4296bool current_is_workqueue_rescuer(void)
4297{
4298 struct worker *worker = current_wq_worker();
4299
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004300 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07004301}
4302
4303/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004304 * workqueue_congested - test whether a workqueue is congested
4305 * @cpu: CPU in question
4306 * @wq: target workqueue
4307 *
4308 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4309 * no synchronization around this function and the test result is
4310 * unreliable and only useful as advisory hints or for debugging.
4311 *
4312 * RETURNS:
4313 * %true if congested, %false otherwise.
4314 */
Tejun Heod84ff052013-03-12 11:29:59 -07004315bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004316{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004317 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004318 bool ret;
4319
Lai Jiangshan88109452013-03-20 03:28:10 +08004320 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004321
4322 if (!(wq->flags & WQ_UNBOUND))
4323 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4324 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004325 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004326
Tejun Heo76af4d92013-03-12 11:30:00 -07004327 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004328 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004329
4330 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004331}
4332EXPORT_SYMBOL_GPL(workqueue_congested);
4333
4334/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004335 * work_busy - test whether a work is currently pending or running
4336 * @work: the work to be tested
4337 *
4338 * Test whether @work is currently pending or running. There is no
4339 * synchronization around this function and the test result is
4340 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004341 *
4342 * RETURNS:
4343 * OR'd bitmask of WORK_BUSY_* bits.
4344 */
4345unsigned int work_busy(struct work_struct *work)
4346{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004347 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004348 unsigned long flags;
4349 unsigned int ret = 0;
4350
Tejun Heodcd989c2010-06-29 10:07:14 +02004351 if (work_pending(work))
4352 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004353
Tejun Heofa1b54e2013-03-12 11:30:00 -07004354 local_irq_save(flags);
4355 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004356 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004357 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004358 if (find_worker_executing_work(pool, work))
4359 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004360 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004361 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004362 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004363
4364 return ret;
4365}
4366EXPORT_SYMBOL_GPL(work_busy);
4367
Tejun Heodb7bccf2010-06-29 10:07:12 +02004368/*
4369 * CPU hotplug.
4370 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004371 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004372 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004373 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004374 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004375 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004376 * blocked draining impractical.
4377 *
Tejun Heo24647572013-01-24 11:01:33 -08004378 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004379 * running as an unbound one and allowing it to be reattached later if the
4380 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004381 */
4382
Tejun Heo706026c2013-01-24 11:01:34 -08004383static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004384{
Tejun Heo38db41d2013-01-24 11:01:34 -08004385 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004386 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004387 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004388 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004389
Tejun Heof02ae732013-03-12 11:30:03 -07004390 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004391 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004392
Tejun Heobc3a1af2013-03-13 19:47:39 -07004393 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004394 spin_lock_irq(&pool->lock);
4395
4396 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004397 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004398 * unbound and set DISASSOCIATED. Before this, all workers
4399 * except for the ones which are still executing works from
4400 * before the last CPU down must be on the cpu. After
4401 * this, they may become diasporas.
4402 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004403 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004404 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004405
Tejun Heo24647572013-01-24 11:01:33 -08004406 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004407
Tejun Heo94cf58b2013-01-24 11:01:33 -08004408 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004409 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02004410
Lai Jiangshaneb283422013-03-08 15:18:28 -08004411 /*
4412 * Call schedule() so that we cross rq->lock and thus can
4413 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4414 * This is necessary as scheduler callbacks may be invoked
4415 * from other cpus.
4416 */
4417 schedule();
Tejun Heo628c78e2012-07-17 12:39:27 -07004418
Lai Jiangshaneb283422013-03-08 15:18:28 -08004419 /*
4420 * Sched callbacks are disabled now. Zap nr_running.
4421 * After this, nr_running stays zero and need_more_worker()
4422 * and keep_working() are always true as long as the
4423 * worklist is not empty. This pool now behaves as an
4424 * unbound (in terms of concurrency management) pool which
4425 * are served by workers tied to the pool.
4426 */
Tejun Heoe19e3972013-01-24 11:39:44 -08004427 atomic_set(&pool->nr_running, 0);
Lai Jiangshaneb283422013-03-08 15:18:28 -08004428
4429 /*
4430 * With concurrency management just turned off, a busy
4431 * worker blocking could lead to lengthy stalls. Kick off
4432 * unbound chain execution of currently pending work items.
4433 */
4434 spin_lock_irq(&pool->lock);
4435 wake_up_worker(pool);
4436 spin_unlock_irq(&pool->lock);
4437 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004438}
4439
Tejun Heobd7c0892013-03-19 13:45:21 -07004440/**
4441 * rebind_workers - rebind all workers of a pool to the associated CPU
4442 * @pool: pool of interest
4443 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004444 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004445 */
4446static void rebind_workers(struct worker_pool *pool)
4447{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004448 struct worker *worker;
4449 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004450
4451 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004452
Tejun Heoa9ab7752013-03-19 13:45:21 -07004453 /*
4454 * Restore CPU affinity of all workers. As all idle workers should
4455 * be on the run-queue of the associated CPU before any local
4456 * wake-ups for concurrency management happen, restore CPU affinty
4457 * of all workers first and then clear UNBOUND. As we're called
4458 * from CPU_ONLINE, the following shouldn't fail.
4459 */
4460 for_each_pool_worker(worker, wi, pool)
4461 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4462 pool->attrs->cpumask) < 0);
4463
4464 spin_lock_irq(&pool->lock);
4465
4466 for_each_pool_worker(worker, wi, pool) {
4467 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004468
4469 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004470 * A bound idle worker should actually be on the runqueue
4471 * of the associated CPU for local wake-ups targeting it to
4472 * work. Kick all idle workers so that they migrate to the
4473 * associated CPU. Doing this in the same loop as
4474 * replacing UNBOUND with REBOUND is safe as no worker will
4475 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004476 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004477 if (worker_flags & WORKER_IDLE)
4478 wake_up_process(worker->task);
4479
4480 /*
4481 * We want to clear UNBOUND but can't directly call
4482 * worker_clr_flags() or adjust nr_running. Atomically
4483 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4484 * @worker will clear REBOUND using worker_clr_flags() when
4485 * it initiates the next execution cycle thus restoring
4486 * concurrency management. Note that when or whether
4487 * @worker clears REBOUND doesn't affect correctness.
4488 *
4489 * ACCESS_ONCE() is necessary because @worker->flags may be
4490 * tested without holding any lock in
4491 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4492 * fail incorrectly leading to premature concurrency
4493 * management operations.
4494 */
4495 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4496 worker_flags |= WORKER_REBOUND;
4497 worker_flags &= ~WORKER_UNBOUND;
4498 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004499 }
4500
Tejun Heoa9ab7752013-03-19 13:45:21 -07004501 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004502}
4503
Tejun Heo7dbc7252013-03-19 13:45:21 -07004504/**
4505 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4506 * @pool: unbound pool of interest
4507 * @cpu: the CPU which is coming up
4508 *
4509 * An unbound pool may end up with a cpumask which doesn't have any online
4510 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4511 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4512 * online CPU before, cpus_allowed of all its workers should be restored.
4513 */
4514static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4515{
4516 static cpumask_t cpumask;
4517 struct worker *worker;
4518 int wi;
4519
4520 lockdep_assert_held(&pool->manager_mutex);
4521
4522 /* is @cpu allowed for @pool? */
4523 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4524 return;
4525
4526 /* is @cpu the only online CPU? */
4527 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4528 if (cpumask_weight(&cpumask) != 1)
4529 return;
4530
4531 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4532 for_each_pool_worker(worker, wi, pool)
4533 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4534 pool->attrs->cpumask) < 0);
4535}
4536
Tejun Heo8db25e72012-07-17 12:39:28 -07004537/*
4538 * Workqueues should be brought up before normal priority CPU notifiers.
4539 * This will be registered high priority CPU notifier.
4540 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004541static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004542 unsigned long action,
4543 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004544{
Tejun Heod84ff052013-03-12 11:29:59 -07004545 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004546 struct worker_pool *pool;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004547 struct workqueue_struct *wq;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004548 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Tejun Heo8db25e72012-07-17 12:39:28 -07004550 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004552 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004553 if (pool->nr_workers)
4554 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004555 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004556 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004558 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004559
Tejun Heo65758202012-07-17 12:39:26 -07004560 case CPU_DOWN_FAILED:
4561 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004562 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004563
4564 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004565 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004566
Tejun Heo7dbc7252013-03-19 13:45:21 -07004567 if (pool->cpu == cpu) {
4568 spin_lock_irq(&pool->lock);
4569 pool->flags &= ~POOL_DISASSOCIATED;
4570 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004571
Tejun Heo7dbc7252013-03-19 13:45:21 -07004572 rebind_workers(pool);
4573 } else if (pool->cpu < 0) {
4574 restore_unbound_workers_cpumask(pool, cpu);
4575 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004576
Tejun Heobc3a1af2013-03-13 19:47:39 -07004577 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004578 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004579
Tejun Heo4c16bd32013-04-01 11:23:36 -07004580 /* update NUMA affinity of unbound workqueues */
4581 list_for_each_entry(wq, &workqueues, list)
4582 wq_update_unbound_numa(wq, cpu, true);
4583
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004584 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004585 break;
Tejun Heo65758202012-07-17 12:39:26 -07004586 }
4587 return NOTIFY_OK;
4588}
4589
4590/*
4591 * Workqueues should be brought down after normal priority CPU notifiers.
4592 * This will be registered as low priority CPU notifier.
4593 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004594static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004595 unsigned long action,
4596 void *hcpu)
4597{
Tejun Heod84ff052013-03-12 11:29:59 -07004598 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004599 struct work_struct unbind_work;
Tejun Heo4c16bd32013-04-01 11:23:36 -07004600 struct workqueue_struct *wq;
Tejun Heo8db25e72012-07-17 12:39:28 -07004601
Tejun Heo65758202012-07-17 12:39:26 -07004602 switch (action & ~CPU_TASKS_FROZEN) {
4603 case CPU_DOWN_PREPARE:
Tejun Heo4c16bd32013-04-01 11:23:36 -07004604 /* unbinding per-cpu workers should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004605 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004606 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo4c16bd32013-04-01 11:23:36 -07004607
4608 /* update NUMA affinity of unbound workqueues */
4609 mutex_lock(&wq_pool_mutex);
4610 list_for_each_entry(wq, &workqueues, list)
4611 wq_update_unbound_numa(wq, cpu, false);
4612 mutex_unlock(&wq_pool_mutex);
4613
4614 /* wait for per-cpu unbinding to finish */
Tejun Heo8db25e72012-07-17 12:39:28 -07004615 flush_work(&unbind_work);
4616 break;
Tejun Heo65758202012-07-17 12:39:26 -07004617 }
4618 return NOTIFY_OK;
4619}
4620
Rusty Russell2d3854a2008-11-05 13:39:10 +11004621#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004622
Rusty Russell2d3854a2008-11-05 13:39:10 +11004623struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004624 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004625 long (*fn)(void *);
4626 void *arg;
4627 long ret;
4628};
4629
Tejun Heoed48ece2012-09-18 12:48:43 -07004630static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004631{
Tejun Heoed48ece2012-09-18 12:48:43 -07004632 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4633
Rusty Russell2d3854a2008-11-05 13:39:10 +11004634 wfc->ret = wfc->fn(wfc->arg);
4635}
4636
4637/**
4638 * work_on_cpu - run a function in user context on a particular cpu
4639 * @cpu: the cpu to run on
4640 * @fn: the function to run
4641 * @arg: the function arg
4642 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004643 * This will return the value @fn returns.
4644 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06004645 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004646 */
Tejun Heod84ff052013-03-12 11:29:59 -07004647long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004648{
Tejun Heoed48ece2012-09-18 12:48:43 -07004649 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004650
Tejun Heoed48ece2012-09-18 12:48:43 -07004651 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4652 schedule_work_on(cpu, &wfc.work);
4653 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004654 return wfc.ret;
4655}
4656EXPORT_SYMBOL_GPL(work_on_cpu);
4657#endif /* CONFIG_SMP */
4658
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004659#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304660
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004661/**
4662 * freeze_workqueues_begin - begin freezing workqueues
4663 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004664 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004665 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004666 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004667 *
4668 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004669 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004670 */
4671void freeze_workqueues_begin(void)
4672{
Tejun Heo17116962013-03-12 11:29:58 -07004673 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004674 struct workqueue_struct *wq;
4675 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004676 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004677
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004678 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004679
Tejun Heo6183c002013-03-12 11:29:57 -07004680 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004681 workqueue_freezing = true;
4682
Tejun Heo24b8a842013-03-12 11:29:58 -07004683 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004684 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004685 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004686 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4687 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004688 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004689 }
4690
Tejun Heo24b8a842013-03-12 11:29:58 -07004691 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004692 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004693 for_each_pwq(pwq, wq)
4694 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004695 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004696 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004697
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004698 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004700
4701/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004702 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004703 *
4704 * Check whether freezing is complete. This function must be called
4705 * between freeze_workqueues_begin() and thaw_workqueues().
4706 *
4707 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004708 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004709 *
4710 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004711 * %true if some freezable workqueues are still busy. %false if freezing
4712 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004713 */
4714bool freeze_workqueues_busy(void)
4715{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004716 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004717 struct workqueue_struct *wq;
4718 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004719
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004720 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004721
Tejun Heo6183c002013-03-12 11:29:57 -07004722 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004723
Tejun Heo24b8a842013-03-12 11:29:58 -07004724 list_for_each_entry(wq, &workqueues, list) {
4725 if (!(wq->flags & WQ_FREEZABLE))
4726 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004727 /*
4728 * nr_active is monotonically decreasing. It's safe
4729 * to peek without lock.
4730 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004731 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004732 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004733 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004734 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004735 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004736 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004737 goto out_unlock;
4738 }
4739 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004740 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004741 }
4742out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004743 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004744 return busy;
4745}
4746
4747/**
4748 * thaw_workqueues - thaw workqueues
4749 *
4750 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004751 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004752 *
4753 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004754 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004755 */
4756void thaw_workqueues(void)
4757{
Tejun Heo24b8a842013-03-12 11:29:58 -07004758 struct workqueue_struct *wq;
4759 struct pool_workqueue *pwq;
4760 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004761 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004762
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004763 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004764
4765 if (!workqueue_freezing)
4766 goto out_unlock;
4767
Tejun Heo24b8a842013-03-12 11:29:58 -07004768 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004769 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004770 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004771 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4772 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004773 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004774 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004775
Tejun Heo24b8a842013-03-12 11:29:58 -07004776 /* restore max_active and repopulate worklist */
4777 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004778 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004779 for_each_pwq(pwq, wq)
4780 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004781 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004782 }
4783
4784 workqueue_freezing = false;
4785out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004786 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004787}
4788#endif /* CONFIG_FREEZER */
4789
Tejun Heobce90382013-04-01 11:23:32 -07004790static void __init wq_numa_init(void)
4791{
4792 cpumask_var_t *tbl;
4793 int node, cpu;
4794
4795 /* determine NUMA pwq table len - highest node id + 1 */
4796 for_each_node(node)
4797 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4798
4799 if (num_possible_nodes() <= 1)
4800 return;
4801
Tejun Heod55262c2013-04-01 11:23:38 -07004802 if (wq_disable_numa) {
4803 pr_info("workqueue: NUMA affinity support disabled\n");
4804 return;
4805 }
4806
Tejun Heo4c16bd32013-04-01 11:23:36 -07004807 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4808 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4809
Tejun Heobce90382013-04-01 11:23:32 -07004810 /*
4811 * We want masks of possible CPUs of each node which isn't readily
4812 * available. Build one from cpu_to_node() which should have been
4813 * fully initialized by now.
4814 */
4815 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4816 BUG_ON(!tbl);
4817
4818 for_each_node(node)
4819 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4820
4821 for_each_possible_cpu(cpu) {
4822 node = cpu_to_node(cpu);
4823 if (WARN_ON(node == NUMA_NO_NODE)) {
4824 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4825 /* happens iff arch is bonkers, let's just proceed */
4826 return;
4827 }
4828 cpumask_set_cpu(cpu, tbl[node]);
4829 }
4830
4831 wq_numa_possible_cpumask = tbl;
4832 wq_numa_enabled = true;
4833}
4834
Suresh Siddha6ee05782010-07-30 14:57:37 -07004835static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004837 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4838 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004839
Tejun Heo7c3eed52013-01-24 11:01:33 -08004840 /* make sure we have enough bits for OFFQ pool ID */
4841 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004842 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004843
Tejun Heoe904e6c2013-03-12 11:29:57 -07004844 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4845
4846 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4847
Tejun Heo65758202012-07-17 12:39:26 -07004848 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004849 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004850
Tejun Heobce90382013-04-01 11:23:32 -07004851 wq_numa_init();
4852
Tejun Heo706026c2013-01-24 11:01:34 -08004853 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004854 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004855 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004856
Tejun Heo7a4e3442013-03-12 11:30:00 -07004857 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004858 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004859 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004860 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004861 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004862 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004863 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004864
Tejun Heo9daf9e62013-01-24 11:01:33 -08004865 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004866 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004867 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004868 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004869 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004870 }
4871
Tejun Heoe22bee72010-06-29 10:07:14 +02004872 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004873 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004874 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004875
Tejun Heof02ae732013-03-12 11:30:03 -07004876 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004877 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004878 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004879 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004880 }
4881
Tejun Heo29c91e92013-03-12 11:30:03 -07004882 /* create default unbound wq attrs */
4883 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4884 struct workqueue_attrs *attrs;
4885
4886 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004887 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004888 unbound_std_wq_attrs[i] = attrs;
4889 }
4890
Tejun Heod320c032010-06-29 10:07:14 +02004891 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004892 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004893 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004894 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4895 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004896 system_freezable_wq = alloc_workqueue("events_freezable",
4897 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004898 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004899 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004902early_initcall(init_workqueues);