blob: 32b47446305324312050a5cd4496a5cc57e21aeb [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 Heoe22bee72010-06-29 10:07:14 +020048
Tejun Heoea138442013-01-18 14:05:55 -080049#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Tejun Heoc8e55f32010-06-29 10:07:12 +020051enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070052 /*
Tejun Heo24647572013-01-24 11:01:33 -080053 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070054 *
Tejun Heo24647572013-01-24 11:01:33 -080055 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070056 * While associated (!DISASSOCIATED), all workers are bound to the
57 * CPU and none has %WORKER_UNBOUND set and concurrency management
58 * is in effect.
59 *
60 * While DISASSOCIATED, the cpu may be offline and all workers have
61 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080062 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070063 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070064 * Note that DISASSOCIATED should be flipped only while holding
65 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080066 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070067 */
Tejun Heo11ebea52012-07-12 14:46:37 -070068 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080069 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080070 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020071
Tejun Heoc8e55f32010-06-29 10:07:12 +020072 /* worker flags */
73 WORKER_STARTED = 1 << 0, /* started */
74 WORKER_DIE = 1 << 1, /* die die die */
75 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020076 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020077 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020078 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070079 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020080
Tejun Heoa9ab7752013-03-19 13:45:21 -070081 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
82 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020083
Tejun Heoe34cdddb2013-01-24 11:01:33 -080084 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070085
Tejun Heo29c91e92013-03-12 11:30:03 -070086 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020087 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020088
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
90 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
91
Tejun Heo3233cdb2011-02-16 18:10:19 +010092 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
93 /* call for help after 10ms
94 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
96 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020097
98 /*
99 * Rescue workers are used only on emergencies and shared by
100 * all cpus. Give -20.
101 */
102 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700103 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 * Structure fields follow one of the following exclusion rules.
108 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200109 * I: Modifiable by initialization/destruction paths and read-only for
110 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200112 * P: Preemption protected. Disabling preemption is enough and should
113 * only be modified and accessed from the local cpu.
114 *
Tejun Heod565ed62013-01-24 11:01:33 -0800115 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200116 *
Tejun Heod565ed62013-01-24 11:01:33 -0800117 * X: During normal operation, modification requires pool->lock and should
118 * be done only from local cpu. Either disabling preemption on local
119 * cpu or grabbing pool->lock is enough for read access. If
120 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200121 *
Tejun Heo822d8402013-03-19 13:45:21 -0700122 * MG: pool->manager_mutex and pool->lock protected. Writes require both
123 * locks. Reads can happen under either lock.
124 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700125 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700126 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700127 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700128 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700129 * WQ: wq->mutex protected.
130 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700131 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700132 *
133 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200134 */
135
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800136/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200137
Tejun Heobd7bdd42012-07-12 14:46:37 -0700138struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800139 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700140 int cpu; /* I: the associated cpu */
Tejun Heof3f90ad2013-04-01 11:23:34 -0700141 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800142 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700143 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700144
145 struct list_head worklist; /* L: list of pending works */
146 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700147
148 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149 int nr_idle; /* L: currently idle ones */
150
151 struct list_head idle_list; /* X: list of idle workers */
152 struct timer_list idle_timer; /* L: worker idle timeout */
153 struct timer_list mayday_timer; /* L: SOS timer for workers */
154
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700155 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800156 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
157 /* L: hash of busy workers */
158
Tejun Heobc3a1af2013-03-13 19:47:39 -0700159 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700160 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700161 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700162 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800163
Tejun Heo7a4e3442013-03-12 11:30:00 -0700164 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700165 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
166 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700167
Tejun Heoe19e3972013-01-24 11:39:44 -0800168 /*
169 * The current concurrency level. As it's likely to be accessed
170 * from other CPUs during try_to_wake_up(), put it in a separate
171 * cacheline.
172 */
173 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700174
175 /*
176 * Destruction of pool is sched-RCU protected to allow dereferences
177 * from get_work_pool().
178 */
179 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200180} ____cacheline_aligned_in_smp;
181
182/*
Tejun Heo112202d2013-02-13 19:29:12 -0800183 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
184 * of work_struct->data are used for flags and the remaining high bits
185 * point to the pwq; thus, pwqs need to be aligned at two's power of the
186 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Tejun Heo112202d2013-02-13 19:29:12 -0800188struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700189 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200190 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200191 int work_color; /* L: current color */
192 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700193 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int nr_in_flight[WORK_NR_COLORS];
195 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200197 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700199 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700200 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700201
202 /*
203 * Release of unbound pwq is punted to system_wq. See put_pwq()
204 * and pwq_unbound_release_workfn() for details. pool_workqueue
205 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700206 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700207 */
208 struct work_struct unbound_release_work;
209 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700210} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200213 * Structure used to wait for workqueue flush.
214 */
215struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700216 struct list_head list; /* WQ: list of flushers */
217 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200218 struct completion done; /* flush completion */
219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Tejun Heo226223a2013-03-12 11:30:05 -0700221struct wq_device;
222
Tejun Heo73f53c42010-06-29 10:07:11 +0200223/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700224 * The externally visible workqueue. It relays the issued work items to
225 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227struct workqueue_struct {
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700228 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700229 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700230 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700231 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200232
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700233 struct mutex mutex; /* protects this wq */
234 int work_color; /* WQ: current work color */
235 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800236 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700237 struct wq_flusher *first_flusher; /* WQ: first flusher */
238 struct list_head flusher_queue; /* WQ: flush waiters */
239 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200240
Tejun Heo2e109a22013-03-13 19:47:40 -0700241 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200242 struct worker *rescuer; /* I: rescue worker */
243
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700244 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700245 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700246
Tejun Heo6029a912013-04-01 11:23:34 -0700247 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
248
Tejun Heo226223a2013-03-12 11:30:05 -0700249#ifdef CONFIG_SYSFS
250 struct wq_device *wq_dev; /* I: for sysfs interface */
251#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700252#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200253 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700254#endif
Tejun Heob196be82012-01-10 15:11:35 -0800255 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Tejun Heoe904e6c2013-03-12 11:29:57 -0700258static struct kmem_cache *pwq_cache;
259
Tejun Heobce90382013-04-01 11:23:32 -0700260static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
261static cpumask_var_t *wq_numa_possible_cpumask;
262 /* possible CPUs of each node */
263
264static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
265
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700266static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700267static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700268
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700269static LIST_HEAD(workqueues); /* PL: list of all workqueues */
270static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700271
272/* the per-cpu worker pools */
273static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
274 cpu_worker_pools);
275
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700276static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700277
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700278/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700279static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
280
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700281/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700282static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
283
Tejun Heod320c032010-06-29 10:07:14 +0200284struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200285EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300286struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900287EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300288struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200289EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300290struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200291EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300292struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100293EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200294
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700295static int worker_thread(void *__worker);
296static void copy_workqueue_attrs(struct workqueue_attrs *to,
297 const struct workqueue_attrs *from);
298
Tejun Heo97bd2342010-10-05 10:41:14 +0200299#define CREATE_TRACE_POINTS
300#include <trace/events/workqueue.h>
301
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700302#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700303 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700304 lockdep_is_held(&wq_pool_mutex), \
305 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700306
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700307#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700308 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700309 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700310 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700311
Tejun Heo822d8402013-03-19 13:45:21 -0700312#ifdef CONFIG_LOCKDEP
313#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800314 WARN_ONCE(debug_locks && \
315 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700316 !lockdep_is_held(&(pool)->lock), \
317 "pool->manager_mutex or ->lock should be held")
318#else
319#define assert_manager_or_pool_lock(pool) do { } while (0)
320#endif
321
Tejun Heof02ae732013-03-12 11:30:03 -0700322#define for_each_cpu_worker_pool(pool, cpu) \
323 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
324 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700325 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700326
Tejun Heo49e3cf42013-03-12 11:29:58 -0700327/**
Tejun Heo17116962013-03-12 11:29:58 -0700328 * for_each_pool - iterate through all worker_pools in the system
329 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700330 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700331 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700332 * This must be called either with wq_pool_mutex held or sched RCU read
333 * locked. If the pool needs to be used beyond the locking in effect, the
334 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700335 *
336 * The if/else clause exists only for the lockdep assertion and can be
337 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700338 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700339#define for_each_pool(pool, pi) \
340 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700341 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700342 else
Tejun Heo17116962013-03-12 11:29:58 -0700343
344/**
Tejun Heo822d8402013-03-19 13:45:21 -0700345 * for_each_pool_worker - iterate through all workers of a worker_pool
346 * @worker: iteration cursor
347 * @wi: integer used for iteration
348 * @pool: worker_pool to iterate workers of
349 *
350 * This must be called with either @pool->manager_mutex or ->lock held.
351 *
352 * The if/else clause exists only for the lockdep assertion and can be
353 * ignored.
354 */
355#define for_each_pool_worker(worker, wi, pool) \
356 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
357 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
358 else
359
360/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700361 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
362 * @pwq: iteration cursor
363 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700364 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700365 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700366 * If the pwq needs to be used beyond the locking in effect, the caller is
367 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700368 *
369 * The if/else clause exists only for the lockdep assertion and can be
370 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700371 */
372#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700373 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700374 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700375 else
Tejun Heof3421792010-07-02 10:03:51 +0200376
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900377#ifdef CONFIG_DEBUG_OBJECTS_WORK
378
379static struct debug_obj_descr work_debug_descr;
380
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100381static void *work_debug_hint(void *addr)
382{
383 return ((struct work_struct *) addr)->func;
384}
385
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900386/*
387 * fixup_init is called when:
388 * - an active object is initialized
389 */
390static int work_fixup_init(void *addr, enum debug_obj_state state)
391{
392 struct work_struct *work = addr;
393
394 switch (state) {
395 case ODEBUG_STATE_ACTIVE:
396 cancel_work_sync(work);
397 debug_object_init(work, &work_debug_descr);
398 return 1;
399 default:
400 return 0;
401 }
402}
403
404/*
405 * fixup_activate is called when:
406 * - an active object is activated
407 * - an unknown object is activated (might be a statically initialized object)
408 */
409static int work_fixup_activate(void *addr, enum debug_obj_state state)
410{
411 struct work_struct *work = addr;
412
413 switch (state) {
414
415 case ODEBUG_STATE_NOTAVAILABLE:
416 /*
417 * This is not really a fixup. The work struct was
418 * statically initialized. We just make sure that it
419 * is tracked in the object tracker.
420 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200421 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900422 debug_object_init(work, &work_debug_descr);
423 debug_object_activate(work, &work_debug_descr);
424 return 0;
425 }
426 WARN_ON_ONCE(1);
427 return 0;
428
429 case ODEBUG_STATE_ACTIVE:
430 WARN_ON(1);
431
432 default:
433 return 0;
434 }
435}
436
437/*
438 * fixup_free is called when:
439 * - an active object is freed
440 */
441static int work_fixup_free(void *addr, enum debug_obj_state state)
442{
443 struct work_struct *work = addr;
444
445 switch (state) {
446 case ODEBUG_STATE_ACTIVE:
447 cancel_work_sync(work);
448 debug_object_free(work, &work_debug_descr);
449 return 1;
450 default:
451 return 0;
452 }
453}
454
455static struct debug_obj_descr work_debug_descr = {
456 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100457 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900458 .fixup_init = work_fixup_init,
459 .fixup_activate = work_fixup_activate,
460 .fixup_free = work_fixup_free,
461};
462
463static inline void debug_work_activate(struct work_struct *work)
464{
465 debug_object_activate(work, &work_debug_descr);
466}
467
468static inline void debug_work_deactivate(struct work_struct *work)
469{
470 debug_object_deactivate(work, &work_debug_descr);
471}
472
473void __init_work(struct work_struct *work, int onstack)
474{
475 if (onstack)
476 debug_object_init_on_stack(work, &work_debug_descr);
477 else
478 debug_object_init(work, &work_debug_descr);
479}
480EXPORT_SYMBOL_GPL(__init_work);
481
482void destroy_work_on_stack(struct work_struct *work)
483{
484 debug_object_free(work, &work_debug_descr);
485}
486EXPORT_SYMBOL_GPL(destroy_work_on_stack);
487
488#else
489static inline void debug_work_activate(struct work_struct *work) { }
490static inline void debug_work_deactivate(struct work_struct *work) { }
491#endif
492
Tejun Heo9daf9e62013-01-24 11:01:33 -0800493/* allocate ID and assign it to @pool */
494static int worker_pool_assign_id(struct worker_pool *pool)
495{
496 int ret;
497
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700498 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700499
Tejun Heofa1b54e2013-03-12 11:30:00 -0700500 do {
501 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
502 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700503 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700504 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800505
506 return ret;
507}
508
Tejun Heo76af4d92013-03-12 11:30:00 -0700509/**
510 * first_pwq - return the first pool_workqueue of the specified workqueue
511 * @wq: the target workqueue
512 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700513 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700514 * If the pwq needs to be used beyond the locking in effect, the caller is
515 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700516 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700517static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700518{
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700519 assert_rcu_or_wq_mutex(wq);
Tejun Heo76af4d92013-03-12 11:30:00 -0700520 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
521 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700522}
523
Tejun Heo73f53c42010-06-29 10:07:11 +0200524static unsigned int work_color_to_flags(int color)
525{
526 return color << WORK_STRUCT_COLOR_SHIFT;
527}
528
529static int get_work_color(struct work_struct *work)
530{
531 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
532 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
533}
534
535static int work_next_color(int color)
536{
537 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700538}
539
David Howells4594bf12006-12-07 11:33:26 +0000540/*
Tejun Heo112202d2013-02-13 19:29:12 -0800541 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
542 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800543 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544 *
Tejun Heo112202d2013-02-13 19:29:12 -0800545 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
546 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700547 * work->data. These functions should only be called while the work is
548 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200549 *
Tejun Heo112202d2013-02-13 19:29:12 -0800550 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800551 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800552 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800553 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700554 *
555 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
556 * canceled. While being canceled, a work item may have its PENDING set
557 * but stay off timer and worklist for arbitrarily long and nobody should
558 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000559 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560static inline void set_work_data(struct work_struct *work, unsigned long data,
561 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000562{
Tejun Heo6183c002013-03-12 11:29:57 -0700563 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000565}
David Howells365970a2006-11-22 14:54:49 +0000566
Tejun Heo112202d2013-02-13 19:29:12 -0800567static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200569{
Tejun Heo112202d2013-02-13 19:29:12 -0800570 set_work_data(work, (unsigned long)pwq,
571 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200572}
573
Lai Jiangshan4468a002013-02-06 18:04:53 -0800574static void set_work_pool_and_keep_pending(struct work_struct *work,
575 int pool_id)
576{
577 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
578 WORK_STRUCT_PENDING);
579}
580
Tejun Heo7c3eed52013-01-24 11:01:33 -0800581static void set_work_pool_and_clear_pending(struct work_struct *work,
582 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000583{
Tejun Heo23657bb2012-08-13 17:08:19 -0700584 /*
585 * The following wmb is paired with the implied mb in
586 * test_and_set_bit(PENDING) and ensures all updates to @work made
587 * here are visible to and precede any updates by the next PENDING
588 * owner.
589 */
590 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800591 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592}
593
594static void clear_work_data(struct work_struct *work)
595{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800596 smp_wmb(); /* see set_work_pool_and_clear_pending() */
597 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598}
599
Tejun Heo112202d2013-02-13 19:29:12 -0800600static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200601{
Tejun Heoe1201532010-07-22 14:14:25 +0200602 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200603
Tejun Heo112202d2013-02-13 19:29:12 -0800604 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200605 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
606 else
607 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200608}
609
Tejun Heo7c3eed52013-01-24 11:01:33 -0800610/**
611 * get_work_pool - return the worker_pool a given work was associated with
612 * @work: the work item of interest
613 *
614 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700615 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700616 * Pools are created and destroyed under wq_pool_mutex, and allows read
617 * access under sched-RCU read lock. As such, this function should be
618 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700619 *
620 * All fields of the returned pool are accessible as long as the above
621 * mentioned locking is in effect. If the returned pool needs to be used
622 * beyond the critical section, the caller is responsible for ensuring the
623 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800624 */
625static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200626{
Tejun Heoe1201532010-07-22 14:14:25 +0200627 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800628 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200629
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700630 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700631
Tejun Heo112202d2013-02-13 19:29:12 -0800632 if (data & WORK_STRUCT_PWQ)
633 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800634 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200635
Tejun Heo7c3eed52013-01-24 11:01:33 -0800636 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
637 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200638 return NULL;
639
Tejun Heofa1b54e2013-03-12 11:30:00 -0700640 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800641}
642
643/**
644 * get_work_pool_id - return the worker pool ID a given work is associated with
645 * @work: the work item of interest
646 *
647 * Return the worker_pool ID @work was last associated with.
648 * %WORK_OFFQ_POOL_NONE if none.
649 */
650static int get_work_pool_id(struct work_struct *work)
651{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800652 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800653
Tejun Heo112202d2013-02-13 19:29:12 -0800654 if (data & WORK_STRUCT_PWQ)
655 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800656 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
657
658 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800659}
660
Tejun Heobbb68df2012-08-03 10:30:46 -0700661static void mark_work_canceling(struct work_struct *work)
662{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800663 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700664
Tejun Heo7c3eed52013-01-24 11:01:33 -0800665 pool_id <<= WORK_OFFQ_POOL_SHIFT;
666 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700667}
668
669static bool work_is_canceling(struct work_struct *work)
670{
671 unsigned long data = atomic_long_read(&work->data);
672
Tejun Heo112202d2013-02-13 19:29:12 -0800673 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700674}
675
David Howells365970a2006-11-22 14:54:49 +0000676/*
Tejun Heo32704762012-07-13 22:16:45 -0700677 * Policy functions. These define the policies on how the global worker
678 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800679 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000680 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200681
Tejun Heo63d95a92012-07-12 14:46:37 -0700682static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000683{
Tejun Heoe19e3972013-01-24 11:39:44 -0800684 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000685}
686
Tejun Heoe22bee72010-06-29 10:07:14 +0200687/*
688 * Need to wake up a worker? Called from anything but currently
689 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700690 *
691 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800692 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700693 * worklist isn't empty.
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 Heo63d95a92012-07-12 14:46:37 -0700697 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000698}
699
Tejun Heoe22bee72010-06-29 10:07:14 +0200700/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700701static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200702{
Tejun Heo63d95a92012-07-12 14:46:37 -0700703 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200704}
705
706/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700707static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200708{
Tejun Heoe19e3972013-01-24 11:39:44 -0800709 return !list_empty(&pool->worklist) &&
710 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200711}
712
713/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200717}
718
719/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700720static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200721{
Tejun Heo63d95a92012-07-12 14:46:37 -0700722 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700723 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200724}
725
726/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700727static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200728{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700729 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700730 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
731 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200732
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700733 /*
734 * nr_idle and idle_list may disagree if idle rebinding is in
735 * progress. Never return %true if idle_list is empty.
736 */
737 if (list_empty(&pool->idle_list))
738 return false;
739
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
741}
742
743/*
744 * Wake up functions.
745 */
746
Tejun Heo7e116292010-06-29 10:07:13 +0200747/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700748static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200749{
Tejun Heo63d95a92012-07-12 14:46:37 -0700750 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200751 return NULL;
752
Tejun Heo63d95a92012-07-12 14:46:37 -0700753 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200754}
755
756/**
757 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700758 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200759 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700760 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200761 *
762 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800763 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200764 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700765static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200766{
Tejun Heo63d95a92012-07-12 14:46:37 -0700767 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200768
769 if (likely(worker))
770 wake_up_process(worker->task);
771}
772
Tejun Heo4690c4a2010-06-29 10:07:10 +0200773/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200774 * wq_worker_waking_up - a worker is waking up
775 * @task: task waking up
776 * @cpu: CPU @task is waking up to
777 *
778 * This function is called during try_to_wake_up() when a worker is
779 * being awoken.
780 *
781 * CONTEXT:
782 * spin_lock_irq(rq->lock)
783 */
Tejun Heod84ff052013-03-12 11:29:59 -0700784void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200785{
786 struct worker *worker = kthread_data(task);
787
Joonsoo Kim36576002012-10-26 23:03:49 +0900788 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800789 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800790 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900791 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200792}
793
794/**
795 * wq_worker_sleeping - a worker is going to sleep
796 * @task: task going to sleep
797 * @cpu: CPU in question, must be the current CPU number
798 *
799 * This function is called during schedule() when a busy worker is
800 * going to sleep. Worker on the same cpu can be woken up by
801 * returning pointer to its task.
802 *
803 * CONTEXT:
804 * spin_lock_irq(rq->lock)
805 *
806 * RETURNS:
807 * Worker task on @cpu to wake up, %NULL if none.
808 */
Tejun Heod84ff052013-03-12 11:29:59 -0700809struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200810{
811 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800812 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200813
Tejun Heo111c2252013-01-17 17:16:24 -0800814 /*
815 * Rescuers, which may not have all the fields set up like normal
816 * workers, also reach here, let's not access anything before
817 * checking NOT_RUNNING.
818 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500819 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200820 return NULL;
821
Tejun Heo111c2252013-01-17 17:16:24 -0800822 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800823
Tejun Heoe22bee72010-06-29 10:07:14 +0200824 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700825 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
826 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200827
828 /*
829 * The counterpart of the following dec_and_test, implied mb,
830 * worklist not empty test sequence is in insert_work().
831 * Please read comment there.
832 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700833 * NOT_RUNNING is clear. This means that we're bound to and
834 * running on the local cpu w/ rq lock held and preemption
835 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800836 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700837 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200838 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800839 if (atomic_dec_and_test(&pool->nr_running) &&
840 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700841 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200842 return to_wakeup ? to_wakeup->task : NULL;
843}
844
845/**
846 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200847 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200848 * @flags: flags to set
849 * @wakeup: wakeup an idle worker if necessary
850 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200851 * Set @flags in @worker->flags and adjust nr_running accordingly. If
852 * nr_running becomes zero and @wakeup is %true, an idle worker is
853 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200854 *
Tejun Heocb444762010-07-02 10:03:50 +0200855 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800856 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200857 */
858static inline void worker_set_flags(struct worker *worker, unsigned int flags,
859 bool wakeup)
860{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700861 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200862
Tejun Heocb444762010-07-02 10:03:50 +0200863 WARN_ON_ONCE(worker->task != current);
864
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 /*
866 * If transitioning into NOT_RUNNING, adjust nr_running and
867 * wake up an idle worker as necessary if requested by
868 * @wakeup.
869 */
870 if ((flags & WORKER_NOT_RUNNING) &&
871 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200872 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800873 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700874 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700875 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200876 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800877 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200878 }
879
Tejun Heod302f012010-06-29 10:07:13 +0200880 worker->flags |= flags;
881}
882
883/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200884 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200885 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200886 * @flags: flags to clear
887 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200888 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200889 *
Tejun Heocb444762010-07-02 10:03:50 +0200890 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800891 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200892 */
893static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
894{
Tejun Heo63d95a92012-07-12 14:46:37 -0700895 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200896 unsigned int oflags = worker->flags;
897
Tejun Heocb444762010-07-02 10:03:50 +0200898 WARN_ON_ONCE(worker->task != current);
899
Tejun Heod302f012010-06-29 10:07:13 +0200900 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200901
Tejun Heo42c025f2011-01-11 15:58:49 +0100902 /*
903 * If transitioning out of NOT_RUNNING, increment nr_running. Note
904 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
905 * of multiple flags, not a single flag.
906 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200907 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
908 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800909 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200910}
911
912/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200913 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800914 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200915 * @work: work to find worker for
916 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800917 * Find a worker which is executing @work on @pool by searching
918 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800919 * to match, its current execution should match the address of @work and
920 * its work function. This is to avoid unwanted dependency between
921 * unrelated work executions through a work item being recycled while still
922 * being executed.
923 *
924 * This is a bit tricky. A work item may be freed once its execution
925 * starts and nothing prevents the freed area from being recycled for
926 * another work item. If the same work item address ends up being reused
927 * before the original execution finishes, workqueue will identify the
928 * recycled work item as currently executing and make it wait until the
929 * current execution finishes, introducing an unwanted dependency.
930 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700931 * This function checks the work item address and work function to avoid
932 * false positives. Note that this isn't complete as one may construct a
933 * work function which can introduce dependency onto itself through a
934 * recycled work item. Well, if somebody wants to shoot oneself in the
935 * foot that badly, there's only so much we can do, and if such deadlock
936 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200937 *
938 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800939 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200940 *
941 * RETURNS:
942 * Pointer to worker which is executing @work if found, NULL
943 * otherwise.
944 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800945static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200946 struct work_struct *work)
947{
Sasha Levin42f85702012-12-17 10:01:23 -0500948 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500949
Sasha Levinb67bfe02013-02-27 17:06:00 -0800950 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800951 (unsigned long)work)
952 if (worker->current_work == work &&
953 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500954 return worker;
955
956 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200957}
958
959/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700960 * move_linked_works - move linked works to a list
961 * @work: start of series of works to be scheduled
962 * @head: target list to append @work to
963 * @nextp: out paramter for nested worklist walking
964 *
965 * Schedule linked works starting from @work to @head. Work series to
966 * be scheduled starts at @work and includes any consecutive work with
967 * WORK_STRUCT_LINKED set in its predecessor.
968 *
969 * If @nextp is not NULL, it's updated to point to the next work of
970 * the last scheduled work. This allows move_linked_works() to be
971 * nested inside outer list_for_each_entry_safe().
972 *
973 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800974 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700975 */
976static void move_linked_works(struct work_struct *work, struct list_head *head,
977 struct work_struct **nextp)
978{
979 struct work_struct *n;
980
981 /*
982 * Linked worklist will always end before the end of the list,
983 * use NULL for list head.
984 */
985 list_for_each_entry_safe_from(work, n, NULL, entry) {
986 list_move_tail(&work->entry, head);
987 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
988 break;
989 }
990
991 /*
992 * If we're already inside safe list traversal and have moved
993 * multiple works to the scheduled queue, the next position
994 * needs to be updated.
995 */
996 if (nextp)
997 *nextp = n;
998}
999
Tejun Heo8864b4e2013-03-12 11:30:04 -07001000/**
1001 * get_pwq - get an extra reference on the specified pool_workqueue
1002 * @pwq: pool_workqueue to get
1003 *
1004 * Obtain an extra reference on @pwq. The caller should guarantee that
1005 * @pwq has positive refcnt and be holding the matching pool->lock.
1006 */
1007static void get_pwq(struct pool_workqueue *pwq)
1008{
1009 lockdep_assert_held(&pwq->pool->lock);
1010 WARN_ON_ONCE(pwq->refcnt <= 0);
1011 pwq->refcnt++;
1012}
1013
1014/**
1015 * put_pwq - put a pool_workqueue reference
1016 * @pwq: pool_workqueue to put
1017 *
1018 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1019 * destruction. The caller should be holding the matching pool->lock.
1020 */
1021static void put_pwq(struct pool_workqueue *pwq)
1022{
1023 lockdep_assert_held(&pwq->pool->lock);
1024 if (likely(--pwq->refcnt))
1025 return;
1026 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1027 return;
1028 /*
1029 * @pwq can't be released under pool->lock, bounce to
1030 * pwq_unbound_release_workfn(). This never recurses on the same
1031 * pool->lock as this path is taken only for unbound workqueues and
1032 * the release work item is scheduled on a per-cpu workqueue. To
1033 * avoid lockdep warning, unbound pool->locks are given lockdep
1034 * subclass of 1 in get_unbound_pool().
1035 */
1036 schedule_work(&pwq->unbound_release_work);
1037}
1038
Tejun Heo112202d2013-02-13 19:29:12 -08001039static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001040{
Tejun Heo112202d2013-02-13 19:29:12 -08001041 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001042
1043 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001044 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001045 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001046 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001047}
1048
Tejun Heo112202d2013-02-13 19:29:12 -08001049static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001050{
Tejun Heo112202d2013-02-13 19:29:12 -08001051 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001052 struct work_struct, entry);
1053
Tejun Heo112202d2013-02-13 19:29:12 -08001054 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001055}
1056
Tejun Heobf4ede02012-08-03 10:30:46 -07001057/**
Tejun Heo112202d2013-02-13 19:29:12 -08001058 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1059 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001060 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001061 *
1062 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001063 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001064 *
1065 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001066 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001067 */
Tejun Heo112202d2013-02-13 19:29:12 -08001068static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001069{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001070 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001071 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001072 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001073
Tejun Heo112202d2013-02-13 19:29:12 -08001074 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001075
Tejun Heo112202d2013-02-13 19:29:12 -08001076 pwq->nr_active--;
1077 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001078 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001079 if (pwq->nr_active < pwq->max_active)
1080 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001081 }
1082
1083 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001084 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001085 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001086
1087 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001088 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001089 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001090
Tejun Heo112202d2013-02-13 19:29:12 -08001091 /* this pwq is done, clear flush_color */
1092 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001093
1094 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001095 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001096 * will handle the rest.
1097 */
Tejun Heo112202d2013-02-13 19:29:12 -08001098 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1099 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001100out_put:
1101 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001102}
1103
Tejun Heo36e227d2012-08-03 10:30:46 -07001104/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001105 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001106 * @work: work item to steal
1107 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001108 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001109 *
1110 * Try to grab PENDING bit of @work. This function can handle @work in any
1111 * stable state - idle, on timer or on worklist. Return values are
1112 *
1113 * 1 if @work was pending and we successfully stole PENDING
1114 * 0 if @work was idle and we claimed PENDING
1115 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001116 * -ENOENT if someone else is canceling @work, this state may persist
1117 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001118 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001119 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001120 * interrupted while holding PENDING and @work off queue, irq must be
1121 * disabled on entry. This, combined with delayed_work->timer being
1122 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001123 *
1124 * On successful return, >= 0, irq is disabled and the caller is
1125 * responsible for releasing it using local_irq_restore(*@flags).
1126 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001127 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001128 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001129static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1130 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001131{
Tejun Heod565ed62013-01-24 11:01:33 -08001132 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001133 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001134
Tejun Heobbb68df2012-08-03 10:30:46 -07001135 local_irq_save(*flags);
1136
Tejun Heo36e227d2012-08-03 10:30:46 -07001137 /* try to steal the timer if it exists */
1138 if (is_dwork) {
1139 struct delayed_work *dwork = to_delayed_work(work);
1140
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001141 /*
1142 * dwork->timer is irqsafe. If del_timer() fails, it's
1143 * guaranteed that the timer is not queued anywhere and not
1144 * running on the local CPU.
1145 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001146 if (likely(del_timer(&dwork->timer)))
1147 return 1;
1148 }
1149
1150 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001151 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1152 return 0;
1153
1154 /*
1155 * The queueing is in progress, or it is already queued. Try to
1156 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1157 */
Tejun Heod565ed62013-01-24 11:01:33 -08001158 pool = get_work_pool(work);
1159 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001160 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001161
Tejun Heod565ed62013-01-24 11:01:33 -08001162 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001163 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001164 * work->data is guaranteed to point to pwq only while the work
1165 * item is queued on pwq->wq, and both updating work->data to point
1166 * to pwq on queueing and to pool on dequeueing are done under
1167 * pwq->pool->lock. This in turn guarantees that, if work->data
1168 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001169 * item is currently queued on that pool.
1170 */
Tejun Heo112202d2013-02-13 19:29:12 -08001171 pwq = get_work_pwq(work);
1172 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001173 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001174
Tejun Heo16062832013-02-06 18:04:53 -08001175 /*
1176 * A delayed work item cannot be grabbed directly because
1177 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001178 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001179 * management later on and cause stall. Make sure the work
1180 * item is activated before grabbing.
1181 */
1182 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001183 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001184
Tejun Heo16062832013-02-06 18:04:53 -08001185 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001186 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001187
Tejun Heo112202d2013-02-13 19:29:12 -08001188 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001189 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001190
Tejun Heo16062832013-02-06 18:04:53 -08001191 spin_unlock(&pool->lock);
1192 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001193 }
Tejun Heod565ed62013-01-24 11:01:33 -08001194 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001195fail:
1196 local_irq_restore(*flags);
1197 if (work_is_canceling(work))
1198 return -ENOENT;
1199 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001200 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001201}
1202
1203/**
Tejun Heo706026c2013-01-24 11:01:34 -08001204 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001205 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001206 * @work: work to insert
1207 * @head: insertion point
1208 * @extra_flags: extra WORK_STRUCT_* flags to set
1209 *
Tejun Heo112202d2013-02-13 19:29:12 -08001210 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001211 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001212 *
1213 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001214 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001215 */
Tejun Heo112202d2013-02-13 19:29:12 -08001216static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1217 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001218{
Tejun Heo112202d2013-02-13 19:29:12 -08001219 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001220
Tejun Heo4690c4a2010-06-29 10:07:10 +02001221 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001222 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001223 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001224 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001225
1226 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001227 * Ensure either wq_worker_sleeping() sees the above
1228 * list_add_tail() or we see zero nr_running to avoid workers lying
1229 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001230 */
1231 smp_mb();
1232
Tejun Heo63d95a92012-07-12 14:46:37 -07001233 if (__need_more_worker(pool))
1234 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001235}
1236
Tejun Heoc8efcc22010-12-20 19:32:04 +01001237/*
1238 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001239 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001240 */
1241static bool is_chained_work(struct workqueue_struct *wq)
1242{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001243 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001244
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001245 worker = current_wq_worker();
1246 /*
1247 * Return %true iff I'm a worker execuing a work item on @wq. If
1248 * I'm @worker, it's safe to dereference it without locking.
1249 */
Tejun Heo112202d2013-02-13 19:29:12 -08001250 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001251}
1252
Tejun Heod84ff052013-03-12 11:29:59 -07001253static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct work_struct *work)
1255{
Tejun Heo112202d2013-02-13 19:29:12 -08001256 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001257 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001258 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001259 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001260 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001261
1262 /*
1263 * While a work item is PENDING && off queue, a task trying to
1264 * steal the PENDING will busy-loop waiting for it to either get
1265 * queued or lose PENDING. Grabbing PENDING and queueing should
1266 * happen with IRQ disabled.
1267 */
1268 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001270 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001271
Tejun Heoc8efcc22010-12-20 19:32:04 +01001272 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001273 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001274 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001275 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001276retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001277 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001278 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001279 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001280 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001281 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001282 } else {
1283 pwq = first_pwq(wq);
1284 }
Tejun Heodbf25762012-08-20 14:51:23 -07001285
Tejun Heoc9178082013-03-12 11:30:04 -07001286 /*
1287 * If @work was previously on a different pool, it might still be
1288 * running there, in which case the work needs to be queued on that
1289 * pool to guarantee non-reentrancy.
1290 */
1291 last_pool = get_work_pool(work);
1292 if (last_pool && last_pool != pwq->pool) {
1293 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001294
Tejun Heoc9178082013-03-12 11:30:04 -07001295 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001296
Tejun Heoc9178082013-03-12 11:30:04 -07001297 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001298
Tejun Heoc9178082013-03-12 11:30:04 -07001299 if (worker && worker->current_pwq->wq == wq) {
1300 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001301 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001302 /* meh... not running there, queue here */
1303 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001304 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001305 }
Tejun Heof3421792010-07-02 10:03:51 +02001306 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001307 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001308 }
1309
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001310 /*
1311 * pwq is determined and locked. For unbound pools, we could have
1312 * raced with pwq release and it could already be dead. If its
1313 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1314 * without another pwq replacing it as the first pwq or while a
1315 * work item is executing on it, so the retying is guaranteed to
1316 * make forward-progress.
1317 */
1318 if (unlikely(!pwq->refcnt)) {
1319 if (wq->flags & WQ_UNBOUND) {
1320 spin_unlock(&pwq->pool->lock);
1321 cpu_relax();
1322 goto retry;
1323 }
1324 /* oops */
1325 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1326 wq->name, cpu);
1327 }
1328
Tejun Heo112202d2013-02-13 19:29:12 -08001329 /* pwq determined, queue */
1330 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001331
Dan Carpenterf5b25522012-04-13 22:06:58 +03001332 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001333 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001334 return;
1335 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001336
Tejun Heo112202d2013-02-13 19:29:12 -08001337 pwq->nr_in_flight[pwq->work_color]++;
1338 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001339
Tejun Heo112202d2013-02-13 19:29:12 -08001340 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001341 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001342 pwq->nr_active++;
1343 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001344 } else {
1345 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001346 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001347 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001348
Tejun Heo112202d2013-02-13 19:29:12 -08001349 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001350
Tejun Heo112202d2013-02-13 19:29:12 -08001351 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001354/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001355 * queue_work_on - queue work on specific cpu
1356 * @cpu: CPU number to execute work on
1357 * @wq: workqueue to use
1358 * @work: work to queue
1359 *
Tejun Heod4283e92012-08-03 10:30:44 -07001360 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001361 *
1362 * We queue the work to a specific CPU, the caller must ensure it
1363 * can't go away.
1364 */
Tejun Heod4283e92012-08-03 10:30:44 -07001365bool queue_work_on(int cpu, struct workqueue_struct *wq,
1366 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001367{
Tejun Heod4283e92012-08-03 10:30:44 -07001368 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001369 unsigned long flags;
1370
1371 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001372
Tejun Heo22df02b2010-06-29 10:07:10 +02001373 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001374 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001375 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001376 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001377
1378 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001379 return ret;
1380}
1381EXPORT_SYMBOL_GPL(queue_work_on);
1382
Tejun Heod8e794d2012-08-03 10:30:45 -07001383void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
David Howells52bad642006-11-22 14:54:01 +00001385 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001387 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001388 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001390EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001392static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1393 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001395 struct timer_list *timer = &dwork->timer;
1396 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001398 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1399 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001400 WARN_ON_ONCE(timer_pending(timer));
1401 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001402
Tejun Heo8852aac2012-12-01 16:23:42 -08001403 /*
1404 * If @delay is 0, queue @dwork->work immediately. This is for
1405 * both optimization and correctness. The earliest @timer can
1406 * expire is on the closest next tick and delayed_work users depend
1407 * on that there's no such delay when @delay is 0.
1408 */
1409 if (!delay) {
1410 __queue_work(cpu, wq, &dwork->work);
1411 return;
1412 }
1413
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001414 timer_stats_timer_set_start_info(&dwork->timer);
1415
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001416 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001417 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001418 timer->expires = jiffies + delay;
1419
1420 if (unlikely(cpu != WORK_CPU_UNBOUND))
1421 add_timer_on(timer, cpu);
1422 else
1423 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001426/**
1427 * queue_delayed_work_on - queue work on specific CPU after delay
1428 * @cpu: CPU number to execute work on
1429 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001430 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001431 * @delay: number of jiffies to wait before queueing
1432 *
Tejun Heo715f1302012-08-03 10:30:46 -07001433 * Returns %false if @work was already on a queue, %true otherwise. If
1434 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1435 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001436 */
Tejun Heod4283e92012-08-03 10:30:44 -07001437bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1438 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001439{
David Howells52bad642006-11-22 14:54:01 +00001440 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001441 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001442 unsigned long flags;
1443
1444 /* read the comment in __queue_work() */
1445 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001446
Tejun Heo22df02b2010-06-29 10:07:10 +02001447 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001448 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001449 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001450 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001451
1452 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001453 return ret;
1454}
Dave Jonesae90dd52006-06-30 01:40:45 -04001455EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Tejun Heoc8e55f32010-06-29 10:07:12 +02001457/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001458 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1459 * @cpu: CPU number to execute work on
1460 * @wq: workqueue to use
1461 * @dwork: work to queue
1462 * @delay: number of jiffies to wait before queueing
1463 *
1464 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1465 * modify @dwork's timer so that it expires after @delay. If @delay is
1466 * zero, @work is guaranteed to be scheduled immediately regardless of its
1467 * current state.
1468 *
1469 * Returns %false if @dwork was idle and queued, %true if @dwork was
1470 * pending and its timer was modified.
1471 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001472 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001473 * See try_to_grab_pending() for details.
1474 */
1475bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1476 struct delayed_work *dwork, unsigned long delay)
1477{
1478 unsigned long flags;
1479 int ret;
1480
1481 do {
1482 ret = try_to_grab_pending(&dwork->work, true, &flags);
1483 } while (unlikely(ret == -EAGAIN));
1484
1485 if (likely(ret >= 0)) {
1486 __queue_delayed_work(cpu, wq, dwork, delay);
1487 local_irq_restore(flags);
1488 }
1489
1490 /* -ENOENT from try_to_grab_pending() becomes %true */
1491 return ret;
1492}
1493EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1494
1495/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001496 * worker_enter_idle - enter idle state
1497 * @worker: worker which is entering idle state
1498 *
1499 * @worker is entering idle state. Update stats and idle timer if
1500 * necessary.
1501 *
1502 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001503 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001504 */
1505static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001507 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Tejun Heo6183c002013-03-12 11:29:57 -07001509 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1510 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1511 (worker->hentry.next || worker->hentry.pprev)))
1512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Tejun Heocb444762010-07-02 10:03:50 +02001514 /* can't use worker_set_flags(), also called from start_worker() */
1515 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001516 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001517 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001518
Tejun Heoc8e55f32010-06-29 10:07:12 +02001519 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001520 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001521
Tejun Heo628c78e2012-07-17 12:39:27 -07001522 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1523 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001524
Tejun Heo544ecf32012-05-14 15:04:50 -07001525 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001526 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001527 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001528 * nr_running, the warning may trigger spuriously. Check iff
1529 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001530 */
Tejun Heo24647572013-01-24 11:01:33 -08001531 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001532 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001533 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Tejun Heoc8e55f32010-06-29 10:07:12 +02001536/**
1537 * worker_leave_idle - leave idle state
1538 * @worker: worker which is leaving idle state
1539 *
1540 * @worker is leaving idle state. Update stats.
1541 *
1542 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001543 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001544 */
1545static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001547 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Tejun Heo6183c002013-03-12 11:29:57 -07001549 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1550 return;
Tejun Heod302f012010-06-29 10:07:13 +02001551 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001552 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001553 list_del_init(&worker->entry);
1554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Tejun Heoe22bee72010-06-29 10:07:14 +02001556/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001557 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1558 * @pool: target worker_pool
1559 *
1560 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 *
1562 * Works which are scheduled while the cpu is online must at least be
1563 * scheduled to a worker which is bound to the cpu so that if they are
1564 * flushed from cpu callbacks while cpu is going down, they are
1565 * guaranteed to execute on the cpu.
1566 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001567 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 * themselves to the target cpu and may race with cpu going down or
1569 * coming online. kthread_bind() can't be used because it may put the
1570 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001571 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001572 * [dis]associated in the meantime.
1573 *
Tejun Heo706026c2013-01-24 11:01:34 -08001574 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001575 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001576 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1577 * enters idle state or fetches works without dropping lock, it can
1578 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 *
1580 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001581 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 * held.
1583 *
1584 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001585 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001586 * bound), %false if offline.
1587 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001588static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001589__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001590{
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 while (true) {
1592 /*
1593 * The following call may fail, succeed or succeed
1594 * without actually migrating the task to the cpu if
1595 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001596 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 */
Tejun Heo24647572013-01-24 11:01:33 -08001598 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001599 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001600
Tejun Heod565ed62013-01-24 11:01:33 -08001601 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001602 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001603 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001604 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001605 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001607 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001608
Tejun Heo5035b202011-04-29 18:08:37 +02001609 /*
1610 * We've raced with CPU hot[un]plug. Give it a breather
1611 * and retry migration. cond_resched() is required here;
1612 * otherwise, we might deadlock against cpu_stop trying to
1613 * bring down the CPU on non-preemptive kernel.
1614 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001615 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001616 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 }
1618}
1619
Tejun Heoc34056a2010-06-29 10:07:11 +02001620static struct worker *alloc_worker(void)
1621{
1622 struct worker *worker;
1623
1624 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001625 if (worker) {
1626 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001627 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001628 /* on creation a worker is in !idle && prep state */
1629 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001630 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001631 return worker;
1632}
1633
1634/**
1635 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001636 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001637 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001638 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001639 * can be started by calling start_worker() or destroyed using
1640 * destroy_worker().
1641 *
1642 * CONTEXT:
1643 * Might sleep. Does GFP_KERNEL allocations.
1644 *
1645 * RETURNS:
1646 * Pointer to the newly created worker.
1647 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001648static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001649{
Tejun Heoc34056a2010-06-29 10:07:11 +02001650 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001651 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001652 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001653
Tejun Heocd549682013-03-13 19:47:39 -07001654 lockdep_assert_held(&pool->manager_mutex);
1655
Tejun Heo822d8402013-03-19 13:45:21 -07001656 /*
1657 * ID is needed to determine kthread name. Allocate ID first
1658 * without installing the pointer.
1659 */
1660 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001661 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001662
1663 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1664
Tejun Heod565ed62013-01-24 11:01:33 -08001665 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001666 idr_preload_end();
1667 if (id < 0)
1668 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001669
1670 worker = alloc_worker();
1671 if (!worker)
1672 goto fail;
1673
Tejun Heobd7bdd42012-07-12 14:46:37 -07001674 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001675 worker->id = id;
1676
Tejun Heo29c91e92013-03-12 11:30:03 -07001677 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001678 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1679 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001680 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001681 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1682
Tejun Heof3f90ad2013-04-01 11:23:34 -07001683 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001684 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001685 if (IS_ERR(worker->task))
1686 goto fail;
1687
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001688 /*
1689 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1690 * online CPUs. It'll be re-applied when any of the CPUs come up.
1691 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001692 set_user_nice(worker->task, pool->attrs->nice);
1693 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001694
Tejun Heo14a40ff2013-03-19 13:45:20 -07001695 /* prevent userland from meddling with cpumask of workqueue workers */
1696 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001697
1698 /*
1699 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1700 * remains stable across this function. See the comments above the
1701 * flag definition for details.
1702 */
1703 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001704 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001705
Tejun Heo822d8402013-03-19 13:45:21 -07001706 /* successful, commit the pointer to idr */
1707 spin_lock_irq(&pool->lock);
1708 idr_replace(&pool->worker_idr, worker, worker->id);
1709 spin_unlock_irq(&pool->lock);
1710
Tejun Heoc34056a2010-06-29 10:07:11 +02001711 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001712
Tejun Heoc34056a2010-06-29 10:07:11 +02001713fail:
1714 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001715 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001716 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001717 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001718 }
1719 kfree(worker);
1720 return NULL;
1721}
1722
1723/**
1724 * start_worker - start a newly created worker
1725 * @worker: worker to start
1726 *
Tejun Heo706026c2013-01-24 11:01:34 -08001727 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001728 *
1729 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001730 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001731 */
1732static void start_worker(struct worker *worker)
1733{
Tejun Heocb444762010-07-02 10:03:50 +02001734 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001735 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001736 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 wake_up_process(worker->task);
1738}
1739
1740/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001741 * create_and_start_worker - create and start a worker for a pool
1742 * @pool: the target pool
1743 *
Tejun Heocd549682013-03-13 19:47:39 -07001744 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001745 */
1746static int create_and_start_worker(struct worker_pool *pool)
1747{
1748 struct worker *worker;
1749
Tejun Heocd549682013-03-13 19:47:39 -07001750 mutex_lock(&pool->manager_mutex);
1751
Tejun Heoebf44d12013-03-13 19:47:39 -07001752 worker = create_worker(pool);
1753 if (worker) {
1754 spin_lock_irq(&pool->lock);
1755 start_worker(worker);
1756 spin_unlock_irq(&pool->lock);
1757 }
1758
Tejun Heocd549682013-03-13 19:47:39 -07001759 mutex_unlock(&pool->manager_mutex);
1760
Tejun Heoebf44d12013-03-13 19:47:39 -07001761 return worker ? 0 : -ENOMEM;
1762}
1763
1764/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 * destroy_worker - destroy a workqueue worker
1766 * @worker: worker to be destroyed
1767 *
Tejun Heo706026c2013-01-24 11:01:34 -08001768 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001769 *
1770 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001771 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001772 */
1773static void destroy_worker(struct worker *worker)
1774{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001775 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001776
Tejun Heocd549682013-03-13 19:47:39 -07001777 lockdep_assert_held(&pool->manager_mutex);
1778 lockdep_assert_held(&pool->lock);
1779
Tejun Heoc34056a2010-06-29 10:07:11 +02001780 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001781 if (WARN_ON(worker->current_work) ||
1782 WARN_ON(!list_empty(&worker->scheduled)))
1783 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001784
Tejun Heoc8e55f32010-06-29 10:07:12 +02001785 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001786 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001787 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001788 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001789
1790 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001791 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001792
Tejun Heo822d8402013-03-19 13:45:21 -07001793 idr_remove(&pool->worker_idr, worker->id);
1794
Tejun Heod565ed62013-01-24 11:01:33 -08001795 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001796
Tejun Heoc34056a2010-06-29 10:07:11 +02001797 kthread_stop(worker->task);
1798 kfree(worker);
1799
Tejun Heod565ed62013-01-24 11:01:33 -08001800 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001801}
1802
Tejun Heo63d95a92012-07-12 14:46:37 -07001803static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001804{
Tejun Heo63d95a92012-07-12 14:46:37 -07001805 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001806
Tejun Heod565ed62013-01-24 11:01:33 -08001807 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001808
Tejun Heo63d95a92012-07-12 14:46:37 -07001809 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001810 struct worker *worker;
1811 unsigned long expires;
1812
1813 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001814 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001815 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1816
1817 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001818 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001819 else {
1820 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001821 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001822 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001823 }
1824 }
1825
Tejun Heod565ed62013-01-24 11:01:33 -08001826 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001827}
1828
Tejun Heo493a1722013-03-12 11:29:59 -07001829static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001830{
Tejun Heo112202d2013-02-13 19:29:12 -08001831 struct pool_workqueue *pwq = get_work_pwq(work);
1832 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001833
Tejun Heo2e109a22013-03-13 19:47:40 -07001834 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001835
Tejun Heo493008a2013-03-12 11:30:03 -07001836 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001837 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001838
1839 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001840 if (list_empty(&pwq->mayday_node)) {
1841 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001843 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001844}
1845
Tejun Heo706026c2013-01-24 11:01:34 -08001846static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001847{
Tejun Heo63d95a92012-07-12 14:46:37 -07001848 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001849 struct work_struct *work;
1850
Tejun Heo2e109a22013-03-13 19:47:40 -07001851 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001852 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001853
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 /*
1856 * We've been trying to create a new worker but
1857 * haven't been successful. We might be hitting an
1858 * allocation deadlock. Send distress signals to
1859 * rescuers.
1860 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001861 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001862 send_mayday(work);
1863 }
1864
Tejun Heo493a1722013-03-12 11:29:59 -07001865 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001866 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001867
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869}
1870
1871/**
1872 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001873 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001874 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001875 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 * have at least one idle worker on return from this function. If
1877 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001878 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001879 * possible allocation deadlock.
1880 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001881 * On return, need_to_create_worker() is guaranteed to be %false and
1882 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001883 *
1884 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001885 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001886 * multiple times. Does GFP_KERNEL allocations. Called only from
1887 * manager.
1888 *
1889 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001890 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001891 * otherwise.
1892 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001893static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001894__releases(&pool->lock)
1895__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001896{
Tejun Heo63d95a92012-07-12 14:46:37 -07001897 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001898 return false;
1899restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001900 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001901
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001903 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001904
1905 while (true) {
1906 struct worker *worker;
1907
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001908 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001911 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001912 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001913 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1914 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 return true;
1916 }
1917
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 break;
1920
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 __set_current_state(TASK_INTERRUPTIBLE);
1922 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001923
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 break;
1926 }
1927
Tejun Heo63d95a92012-07-12 14:46:37 -07001928 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001929 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001930 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 goto restart;
1932 return true;
1933}
1934
1935/**
1936 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001937 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001939 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 * IDLE_WORKER_TIMEOUT.
1941 *
1942 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001943 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 * multiple times. Called only from manager.
1945 *
1946 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001947 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 * otherwise.
1949 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001950static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001951{
1952 bool ret = false;
1953
Tejun Heo63d95a92012-07-12 14:46:37 -07001954 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 struct worker *worker;
1956 unsigned long expires;
1957
Tejun Heo63d95a92012-07-12 14:46:37 -07001958 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001959 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1960
1961 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001962 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 break;
1964 }
1965
1966 destroy_worker(worker);
1967 ret = true;
1968 }
1969
1970 return ret;
1971}
1972
1973/**
1974 * manage_workers - manage worker pool
1975 * @worker: self
1976 *
Tejun Heo706026c2013-01-24 11:01:34 -08001977 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001979 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 *
1981 * The caller can safely start processing works on false return. On
1982 * true return, it's guaranteed that need_to_create_worker() is false
1983 * and may_start_working() is true.
1984 *
1985 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001986 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 * multiple times. Does GFP_KERNEL allocations.
1988 *
1989 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001990 * spin_lock_irq(pool->lock) which may be released and regrabbed
1991 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 */
1993static bool manage_workers(struct worker *worker)
1994{
Tejun Heo63d95a92012-07-12 14:46:37 -07001995 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 bool ret = false;
1997
Tejun Heobc3a1af2013-03-13 19:47:39 -07001998 /*
1999 * Managership is governed by two mutexes - manager_arb and
2000 * manager_mutex. manager_arb handles arbitration of manager role.
2001 * Anyone who successfully grabs manager_arb wins the arbitration
2002 * and becomes the manager. mutex_trylock() on pool->manager_arb
2003 * failure while holding pool->lock reliably indicates that someone
2004 * else is managing the pool and the worker which failed trylock
2005 * can proceed to executing work items. This means that anyone
2006 * grabbing manager_arb is responsible for actually performing
2007 * manager duties. If manager_arb is grabbed and released without
2008 * actual management, the pool may stall indefinitely.
2009 *
2010 * manager_mutex is used for exclusion of actual management
2011 * operations. The holder of manager_mutex can be sure that none
2012 * of management operations, including creation and destruction of
2013 * workers, won't take place until the mutex is released. Because
2014 * manager_mutex doesn't interfere with manager role arbitration,
2015 * it is guaranteed that the pool's management, while may be
2016 * delayed, won't be disturbed by someone else grabbing
2017 * manager_mutex.
2018 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002019 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002020 return ret;
2021
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002022 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002023 * With manager arbitration won, manager_mutex would be free in
2024 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002025 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002026 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002027 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002028 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002029 ret = true;
2030 }
2031
Tejun Heo11ebea52012-07-12 14:46:37 -07002032 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002033
2034 /*
2035 * Destroy and then create so that may_start_working() is true
2036 * on return.
2037 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002038 ret |= maybe_destroy_workers(pool);
2039 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002040
Tejun Heobc3a1af2013-03-13 19:47:39 -07002041 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002042 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002043 return ret;
2044}
2045
Tejun Heoa62428c2010-06-29 10:07:10 +02002046/**
2047 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002048 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002049 * @work: work to process
2050 *
2051 * Process @work. This function contains all the logics necessary to
2052 * process a single work including synchronization against and
2053 * interaction with other workers on the same cpu, queueing and
2054 * flushing. As long as context requirement is met, any worker can
2055 * call this function to process a work.
2056 *
2057 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002058 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002059 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002060static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002061__releases(&pool->lock)
2062__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002063{
Tejun Heo112202d2013-02-13 19:29:12 -08002064 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002065 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002066 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002067 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002068 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002069#ifdef CONFIG_LOCKDEP
2070 /*
2071 * It is permissible to free the struct work_struct from
2072 * inside the function that is called from it, this we need to
2073 * take into account for lockdep too. To avoid bogus "held
2074 * lock freed" warnings as well as problems when looking into
2075 * work->lockdep_map, make a copy and use that here.
2076 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002077 struct lockdep_map lockdep_map;
2078
2079 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002080#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002081 /*
2082 * Ensure we're on the correct CPU. DISASSOCIATED test is
2083 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002084 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002085 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002086 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002087 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002088 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002089
Tejun Heo7e116292010-06-29 10:07:13 +02002090 /*
2091 * A single work shouldn't be executed concurrently by
2092 * multiple workers on a single cpu. Check whether anyone is
2093 * already processing the work. If so, defer the work to the
2094 * currently executing one.
2095 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002096 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002097 if (unlikely(collision)) {
2098 move_linked_works(work, &collision->scheduled, NULL);
2099 return;
2100 }
2101
Tejun Heo8930cab2012-08-03 10:30:45 -07002102 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002103 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002104 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002105 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002106 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002107 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002108 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002109
Tejun Heoa62428c2010-06-29 10:07:10 +02002110 list_del_init(&work->entry);
2111
Tejun Heo649027d2010-06-29 10:07:14 +02002112 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002113 * CPU intensive works don't participate in concurrency
2114 * management. They're the scheduler's responsibility.
2115 */
2116 if (unlikely(cpu_intensive))
2117 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2118
Tejun Heo974271c2012-07-12 14:46:37 -07002119 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002120 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002121 * executed ASAP. Wake up another worker if necessary.
2122 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002123 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2124 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002125
Tejun Heo8930cab2012-08-03 10:30:45 -07002126 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002127 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002128 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002129 * PENDING and queued state changes happen together while IRQ is
2130 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002131 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002132 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002133
Tejun Heod565ed62013-01-24 11:01:33 -08002134 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002135
Tejun Heo112202d2013-02-13 19:29:12 -08002136 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002137 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002138 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002139 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002140 /*
2141 * While we must be careful to not use "work" after this, the trace
2142 * point will only record its address.
2143 */
2144 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002145 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002146 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002147
2148 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002149 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2150 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002151 current->comm, preempt_count(), task_pid_nr(current),
2152 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002153 debug_show_held_locks(current);
2154 dump_stack();
2155 }
2156
Tejun Heod565ed62013-01-24 11:01:33 -08002157 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002158
Tejun Heofb0e7be2010-06-29 10:07:15 +02002159 /* clear cpu intensive status */
2160 if (unlikely(cpu_intensive))
2161 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2162
Tejun Heoa62428c2010-06-29 10:07:10 +02002163 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002164 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002165 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002166 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002167 worker->current_pwq = NULL;
2168 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002169}
2170
Tejun Heoaffee4b2010-06-29 10:07:12 +02002171/**
2172 * process_scheduled_works - process scheduled works
2173 * @worker: self
2174 *
2175 * Process all scheduled works. Please note that the scheduled list
2176 * may change while processing a work, so this function repeatedly
2177 * fetches a work from the top and executes it.
2178 *
2179 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002180 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002181 * multiple times.
2182 */
2183static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002185 while (!list_empty(&worker->scheduled)) {
2186 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002188 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Tejun Heo4690c4a2010-06-29 10:07:10 +02002192/**
2193 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002194 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002195 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002196 * The worker thread function. All workers belong to a worker_pool -
2197 * either a per-cpu one or dynamic unbound one. These workers process all
2198 * work items regardless of their specific target workqueue. The only
2199 * exception is work items which belong to workqueues with a rescuer which
2200 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002201 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002202static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Tejun Heoc34056a2010-06-29 10:07:11 +02002204 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002205 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Tejun Heoe22bee72010-06-29 10:07:14 +02002207 /* tell the scheduler that this is a workqueue worker */
2208 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002209woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002210 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Tejun Heoa9ab7752013-03-19 13:45:21 -07002212 /* am I supposed to die? */
2213 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002214 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002215 WARN_ON_ONCE(!list_empty(&worker->entry));
2216 worker->task->flags &= ~PF_WQ_WORKER;
2217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Tejun Heoc8e55f32010-06-29 10:07:12 +02002220 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002221recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002222 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002223 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002224 goto sleep;
2225
2226 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002227 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002228 goto recheck;
2229
Tejun Heoc8e55f32010-06-29 10:07:12 +02002230 /*
2231 * ->scheduled list can only be filled while a worker is
2232 * preparing to process a work or actually processing it.
2233 * Make sure nobody diddled with it while I was sleeping.
2234 */
Tejun Heo6183c002013-03-12 11:29:57 -07002235 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002236
Tejun Heoe22bee72010-06-29 10:07:14 +02002237 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002238 * Finish PREP stage. We're guaranteed to have at least one idle
2239 * worker or that someone else has already assumed the manager
2240 * role. This is where @worker starts participating in concurrency
2241 * management if applicable and concurrency management is restored
2242 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002243 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002244 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002245
2246 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002247 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002248 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002249 struct work_struct, entry);
2250
2251 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2252 /* optimization path, not strictly necessary */
2253 process_one_work(worker, work);
2254 if (unlikely(!list_empty(&worker->scheduled)))
2255 process_scheduled_works(worker);
2256 } else {
2257 move_linked_works(work, &worker->scheduled, NULL);
2258 process_scheduled_works(worker);
2259 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002260 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002261
Tejun Heoe22bee72010-06-29 10:07:14 +02002262 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002263sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002264 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002265 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002266
Tejun Heoc8e55f32010-06-29 10:07:12 +02002267 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002268 * pool->lock is held and there's no work to process and no need to
2269 * manage, sleep. Workers are woken up only while holding
2270 * pool->lock or from local cpu, so setting the current state
2271 * before releasing pool->lock is enough to prevent losing any
2272 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002273 */
2274 worker_enter_idle(worker);
2275 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002276 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002277 schedule();
2278 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Tejun Heoe22bee72010-06-29 10:07:14 +02002281/**
2282 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002283 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002284 *
2285 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002286 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002287 *
Tejun Heo706026c2013-01-24 11:01:34 -08002288 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002289 * worker which uses GFP_KERNEL allocation which has slight chance of
2290 * developing into deadlock if some works currently on the same queue
2291 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2292 * the problem rescuer solves.
2293 *
Tejun Heo706026c2013-01-24 11:01:34 -08002294 * When such condition is possible, the pool summons rescuers of all
2295 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002296 * those works so that forward progress can be guaranteed.
2297 *
2298 * This should happen rarely.
2299 */
Tejun Heo111c2252013-01-17 17:16:24 -08002300static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002301{
Tejun Heo111c2252013-01-17 17:16:24 -08002302 struct worker *rescuer = __rescuer;
2303 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002304 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002305
2306 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002307
2308 /*
2309 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2310 * doesn't participate in concurrency management.
2311 */
2312 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002313repeat:
2314 set_current_state(TASK_INTERRUPTIBLE);
2315
Mike Galbraith412d32e2012-11-28 07:17:18 +01002316 if (kthread_should_stop()) {
2317 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002318 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002320 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002321
Tejun Heo493a1722013-03-12 11:29:59 -07002322 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002323 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002324
2325 while (!list_empty(&wq->maydays)) {
2326 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2327 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002328 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002329 struct work_struct *work, *n;
2330
2331 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002332 list_del_init(&pwq->mayday_node);
2333
Tejun Heo2e109a22013-03-13 19:47:40 -07002334 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002335
2336 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002337 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002338 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002339
2340 /*
2341 * Slurp in all works issued via this workqueue and
2342 * process'em.
2343 */
Tejun Heo6183c002013-03-12 11:29:57 -07002344 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002345 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002346 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002347 move_linked_works(work, scheduled, &n);
2348
2349 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002350
2351 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002352 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002353 * regular worker; otherwise, we end up with 0 concurrency
2354 * and stalling the execution.
2355 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002356 if (keep_working(pool))
2357 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002358
Lai Jiangshanb3104102013-02-19 12:17:02 -08002359 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002360 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002361 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002362 }
2363
Tejun Heo2e109a22013-03-13 19:47:40 -07002364 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002365
Tejun Heo111c2252013-01-17 17:16:24 -08002366 /* rescuers should never participate in concurrency management */
2367 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002368 schedule();
2369 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002372struct wq_barrier {
2373 struct work_struct work;
2374 struct completion done;
2375};
2376
2377static void wq_barrier_func(struct work_struct *work)
2378{
2379 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2380 complete(&barr->done);
2381}
2382
Tejun Heo4690c4a2010-06-29 10:07:10 +02002383/**
2384 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002385 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002386 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002387 * @target: target work to attach @barr to
2388 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002389 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002390 * @barr is linked to @target such that @barr is completed only after
2391 * @target finishes execution. Please note that the ordering
2392 * guarantee is observed only with respect to @target and on the local
2393 * cpu.
2394 *
2395 * Currently, a queued barrier can't be canceled. This is because
2396 * try_to_grab_pending() can't determine whether the work to be
2397 * grabbed is at the head of the queue and thus can't clear LINKED
2398 * flag of the previous work while there must be a valid next work
2399 * after a work with LINKED flag set.
2400 *
2401 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002402 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002403 *
2404 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002405 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002406 */
Tejun Heo112202d2013-02-13 19:29:12 -08002407static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002408 struct wq_barrier *barr,
2409 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002410{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002411 struct list_head *head;
2412 unsigned int linked = 0;
2413
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002414 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002415 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002416 * as we know for sure that this will not trigger any of the
2417 * checks and call back into the fixup functions where we
2418 * might deadlock.
2419 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002420 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002421 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002422 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002423
Tejun Heoaffee4b2010-06-29 10:07:12 +02002424 /*
2425 * If @target is currently being executed, schedule the
2426 * barrier to the worker; otherwise, put it after @target.
2427 */
2428 if (worker)
2429 head = worker->scheduled.next;
2430 else {
2431 unsigned long *bits = work_data_bits(target);
2432
2433 head = target->entry.next;
2434 /* there can already be other linked works, inherit and set */
2435 linked = *bits & WORK_STRUCT_LINKED;
2436 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2437 }
2438
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002439 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002440 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002441 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002442}
2443
Tejun Heo73f53c42010-06-29 10:07:11 +02002444/**
Tejun Heo112202d2013-02-13 19:29:12 -08002445 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002446 * @wq: workqueue being flushed
2447 * @flush_color: new flush color, < 0 for no-op
2448 * @work_color: new work color, < 0 for no-op
2449 *
Tejun Heo112202d2013-02-13 19:29:12 -08002450 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002451 *
Tejun Heo112202d2013-02-13 19:29:12 -08002452 * If @flush_color is non-negative, flush_color on all pwqs should be
2453 * -1. If no pwq has in-flight commands at the specified color, all
2454 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2455 * has in flight commands, its pwq->flush_color is set to
2456 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002457 * wakeup logic is armed and %true is returned.
2458 *
2459 * The caller should have initialized @wq->first_flusher prior to
2460 * calling this function with non-negative @flush_color. If
2461 * @flush_color is negative, no flush color update is done and %false
2462 * is returned.
2463 *
Tejun Heo112202d2013-02-13 19:29:12 -08002464 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002465 * work_color which is previous to @work_color and all will be
2466 * advanced to @work_color.
2467 *
2468 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002469 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002470 *
2471 * RETURNS:
2472 * %true if @flush_color >= 0 and there's something to flush. %false
2473 * otherwise.
2474 */
Tejun Heo112202d2013-02-13 19:29:12 -08002475static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002476 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Tejun Heo73f53c42010-06-29 10:07:11 +02002478 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002479 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002480
Tejun Heo73f53c42010-06-29 10:07:11 +02002481 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002482 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002483 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002484 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002485
Tejun Heo49e3cf42013-03-12 11:29:58 -07002486 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002487 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002488
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002489 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002490
2491 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002492 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002493
Tejun Heo112202d2013-02-13 19:29:12 -08002494 if (pwq->nr_in_flight[flush_color]) {
2495 pwq->flush_color = flush_color;
2496 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002497 wait = true;
2498 }
2499 }
2500
2501 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002502 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002503 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002504 }
2505
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002506 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002507 }
2508
Tejun Heo112202d2013-02-13 19:29:12 -08002509 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 complete(&wq->first_flusher->done);
2511
2512 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002515/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002517 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002519 * This function sleeps until all work items which were queued on entry
2520 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002522void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Tejun Heo73f53c42010-06-29 10:07:11 +02002524 struct wq_flusher this_flusher = {
2525 .list = LIST_HEAD_INIT(this_flusher.list),
2526 .flush_color = -1,
2527 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2528 };
2529 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002530
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002531 lock_map_acquire(&wq->lockdep_map);
2532 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002533
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002534 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002535
2536 /*
2537 * Start-to-wait phase
2538 */
2539 next_color = work_next_color(wq->work_color);
2540
2541 if (next_color != wq->flush_color) {
2542 /*
2543 * Color space is not full. The current work_color
2544 * becomes our flush_color and work_color is advanced
2545 * by one.
2546 */
Tejun Heo6183c002013-03-12 11:29:57 -07002547 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002548 this_flusher.flush_color = wq->work_color;
2549 wq->work_color = next_color;
2550
2551 if (!wq->first_flusher) {
2552 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002553 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002554
2555 wq->first_flusher = &this_flusher;
2556
Tejun Heo112202d2013-02-13 19:29:12 -08002557 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002558 wq->work_color)) {
2559 /* nothing to flush, done */
2560 wq->flush_color = next_color;
2561 wq->first_flusher = NULL;
2562 goto out_unlock;
2563 }
2564 } else {
2565 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002566 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002567 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002568 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002569 }
2570 } else {
2571 /*
2572 * Oops, color space is full, wait on overflow queue.
2573 * The next flush completion will assign us
2574 * flush_color and transfer to flusher_queue.
2575 */
2576 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2577 }
2578
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002579 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002580
2581 wait_for_completion(&this_flusher.done);
2582
2583 /*
2584 * Wake-up-and-cascade phase
2585 *
2586 * First flushers are responsible for cascading flushes and
2587 * handling overflow. Non-first flushers can simply return.
2588 */
2589 if (wq->first_flusher != &this_flusher)
2590 return;
2591
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002592 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002593
Tejun Heo4ce48b32010-07-02 10:03:51 +02002594 /* we might have raced, check again with mutex held */
2595 if (wq->first_flusher != &this_flusher)
2596 goto out_unlock;
2597
Tejun Heo73f53c42010-06-29 10:07:11 +02002598 wq->first_flusher = NULL;
2599
Tejun Heo6183c002013-03-12 11:29:57 -07002600 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2601 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002602
2603 while (true) {
2604 struct wq_flusher *next, *tmp;
2605
2606 /* complete all the flushers sharing the current flush color */
2607 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2608 if (next->flush_color != wq->flush_color)
2609 break;
2610 list_del_init(&next->list);
2611 complete(&next->done);
2612 }
2613
Tejun Heo6183c002013-03-12 11:29:57 -07002614 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2615 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002616
2617 /* this flush_color is finished, advance by one */
2618 wq->flush_color = work_next_color(wq->flush_color);
2619
2620 /* one color has been freed, handle overflow queue */
2621 if (!list_empty(&wq->flusher_overflow)) {
2622 /*
2623 * Assign the same color to all overflowed
2624 * flushers, advance work_color and append to
2625 * flusher_queue. This is the start-to-wait
2626 * phase for these overflowed flushers.
2627 */
2628 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2629 tmp->flush_color = wq->work_color;
2630
2631 wq->work_color = work_next_color(wq->work_color);
2632
2633 list_splice_tail_init(&wq->flusher_overflow,
2634 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002635 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002636 }
2637
2638 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002639 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002640 break;
2641 }
2642
2643 /*
2644 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002645 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002646 */
Tejun Heo6183c002013-03-12 11:29:57 -07002647 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2648 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002649
2650 list_del_init(&next->list);
2651 wq->first_flusher = next;
2652
Tejun Heo112202d2013-02-13 19:29:12 -08002653 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002654 break;
2655
2656 /*
2657 * Meh... this color is already done, clear first
2658 * flusher and repeat cascading.
2659 */
2660 wq->first_flusher = NULL;
2661 }
2662
2663out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002664 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
Dave Jonesae90dd52006-06-30 01:40:45 -04002666EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002668/**
2669 * drain_workqueue - drain a workqueue
2670 * @wq: workqueue to drain
2671 *
2672 * Wait until the workqueue becomes empty. While draining is in progress,
2673 * only chain queueing is allowed. IOW, only currently pending or running
2674 * work items on @wq can queue further work items on it. @wq is flushed
2675 * repeatedly until it becomes empty. The number of flushing is detemined
2676 * by the depth of chaining and should be relatively short. Whine if it
2677 * takes too long.
2678 */
2679void drain_workqueue(struct workqueue_struct *wq)
2680{
2681 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002682 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002683
2684 /*
2685 * __queue_work() needs to test whether there are drainers, is much
2686 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002687 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002688 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002689 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002690 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002691 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002692 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002693reflush:
2694 flush_workqueue(wq);
2695
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002696 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002697
Tejun Heo49e3cf42013-03-12 11:29:58 -07002698 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002699 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002700
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002701 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002702 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002703 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002704
2705 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002706 continue;
2707
2708 if (++flush_cnt == 10 ||
2709 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002710 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002711 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002712
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002713 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002714 goto reflush;
2715 }
2716
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002717 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002718 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002719 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720}
2721EXPORT_SYMBOL_GPL(drain_workqueue);
2722
Tejun Heo606a5022012-08-20 14:51:23 -07002723static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002724{
2725 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002726 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002727 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002728
2729 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002730
Tejun Heofa1b54e2013-03-12 11:30:00 -07002731 local_irq_disable();
2732 pool = get_work_pool(work);
2733 if (!pool) {
2734 local_irq_enable();
2735 return false;
2736 }
2737
2738 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002739 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002740 pwq = get_work_pwq(work);
2741 if (pwq) {
2742 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002743 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002744 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002745 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002746 if (!worker)
2747 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002748 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002749 }
Tejun Heobaf59022010-09-16 10:42:16 +02002750
Tejun Heo112202d2013-02-13 19:29:12 -08002751 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002752 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002753
Tejun Heoe1594892011-01-09 23:32:15 +01002754 /*
2755 * If @max_active is 1 or rescuer is in use, flushing another work
2756 * item on the same workqueue may lead to deadlock. Make sure the
2757 * flusher is not running on the same workqueue by verifying write
2758 * access.
2759 */
Tejun Heo493008a2013-03-12 11:30:03 -07002760 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002761 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002762 else
Tejun Heo112202d2013-02-13 19:29:12 -08002763 lock_map_acquire_read(&pwq->wq->lockdep_map);
2764 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002765
Tejun Heobaf59022010-09-16 10:42:16 +02002766 return true;
2767already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002768 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002769 return false;
2770}
2771
Oleg Nesterovdb700892008-07-25 01:47:49 -07002772/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002773 * flush_work - wait for a work to finish executing the last queueing instance
2774 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002775 *
Tejun Heo606a5022012-08-20 14:51:23 -07002776 * Wait until @work has finished execution. @work is guaranteed to be idle
2777 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002778 *
2779 * RETURNS:
2780 * %true if flush_work() waited for the work to finish execution,
2781 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002782 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002783bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002784{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002785 struct wq_barrier barr;
2786
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002787 lock_map_acquire(&work->lockdep_map);
2788 lock_map_release(&work->lockdep_map);
2789
Tejun Heo606a5022012-08-20 14:51:23 -07002790 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002791 wait_for_completion(&barr.done);
2792 destroy_work_on_stack(&barr.work);
2793 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002794 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002795 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002796 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002797}
2798EXPORT_SYMBOL_GPL(flush_work);
2799
Tejun Heo36e227d2012-08-03 10:30:46 -07002800static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002801{
Tejun Heobbb68df2012-08-03 10:30:46 -07002802 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002803 int ret;
2804
2805 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002806 ret = try_to_grab_pending(work, is_dwork, &flags);
2807 /*
2808 * If someone else is canceling, wait for the same event it
2809 * would be waiting for before retrying.
2810 */
2811 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002812 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002813 } while (unlikely(ret < 0));
2814
Tejun Heobbb68df2012-08-03 10:30:46 -07002815 /* tell other tasks trying to grab @work to back off */
2816 mark_work_canceling(work);
2817 local_irq_restore(flags);
2818
Tejun Heo606a5022012-08-20 14:51:23 -07002819 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002820 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002821 return ret;
2822}
2823
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002824/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002825 * cancel_work_sync - cancel a work and wait for it to finish
2826 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002828 * Cancel @work and wait for its execution to finish. This function
2829 * can be used even if the work re-queues itself or migrates to
2830 * another workqueue. On return from this function, @work is
2831 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002832 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002833 * cancel_work_sync(&delayed_work->work) must not be used for
2834 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002837 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002838 *
2839 * RETURNS:
2840 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002841 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002842bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002843{
Tejun Heo36e227d2012-08-03 10:30:46 -07002844 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002845}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002846EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002847
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002848/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002849 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2850 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002851 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002852 * Delayed timer is cancelled and the pending work is queued for
2853 * immediate execution. Like flush_work(), this function only
2854 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002855 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002856 * RETURNS:
2857 * %true if flush_work() waited for the work to finish execution,
2858 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002859 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002860bool flush_delayed_work(struct delayed_work *dwork)
2861{
Tejun Heo8930cab2012-08-03 10:30:45 -07002862 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002863 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002864 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002865 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002866 return flush_work(&dwork->work);
2867}
2868EXPORT_SYMBOL(flush_delayed_work);
2869
2870/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002871 * cancel_delayed_work - cancel a delayed work
2872 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002873 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002874 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2875 * and canceled; %false if wasn't pending. Note that the work callback
2876 * function may still be running on return, unless it returns %true and the
2877 * work doesn't re-arm itself. Explicitly flush or use
2878 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002879 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002880 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002881 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002882bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002883{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002884 unsigned long flags;
2885 int ret;
2886
2887 do {
2888 ret = try_to_grab_pending(&dwork->work, true, &flags);
2889 } while (unlikely(ret == -EAGAIN));
2890
2891 if (unlikely(ret < 0))
2892 return false;
2893
Tejun Heo7c3eed52013-01-24 11:01:33 -08002894 set_work_pool_and_clear_pending(&dwork->work,
2895 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002896 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002897 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002898}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002899EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002900
2901/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002902 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2903 * @dwork: the delayed work cancel
2904 *
2905 * This is cancel_work_sync() for delayed works.
2906 *
2907 * RETURNS:
2908 * %true if @dwork was pending, %false otherwise.
2909 */
2910bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911{
Tejun Heo36e227d2012-08-03 10:30:46 -07002912 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002913}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002914EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002916/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002917 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002918 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002919 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002920 * schedule_on_each_cpu() executes @func on each online CPU using the
2921 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002922 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002923 *
2924 * RETURNS:
2925 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002926 */
David Howells65f27f32006-11-22 14:55:48 +00002927int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002928{
2929 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002930 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002931
Andrew Mortonb6136772006-06-25 05:47:49 -07002932 works = alloc_percpu(struct work_struct);
2933 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002934 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002935
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002936 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002937
Christoph Lameter15316ba2006-01-08 01:00:43 -08002938 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002939 struct work_struct *work = per_cpu_ptr(works, cpu);
2940
2941 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002942 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002943 }
Tejun Heo93981802009-11-17 14:06:20 -08002944
2945 for_each_online_cpu(cpu)
2946 flush_work(per_cpu_ptr(works, cpu));
2947
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002948 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002949 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002950 return 0;
2951}
2952
Alan Sterneef6a7d2010-02-12 17:39:21 +09002953/**
2954 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2955 *
2956 * Forces execution of the kernel-global workqueue and blocks until its
2957 * completion.
2958 *
2959 * Think twice before calling this function! It's very easy to get into
2960 * trouble if you don't take great care. Either of the following situations
2961 * will lead to deadlock:
2962 *
2963 * One of the work items currently on the workqueue needs to acquire
2964 * a lock held by your code or its caller.
2965 *
2966 * Your code is running in the context of a work routine.
2967 *
2968 * They will be detected by lockdep when they occur, but the first might not
2969 * occur very often. It depends on what work items are on the workqueue and
2970 * what locks they need, which you have no control over.
2971 *
2972 * In most situations flushing the entire workqueue is overkill; you merely
2973 * need to know that a particular work item isn't queued and isn't running.
2974 * In such cases you should use cancel_delayed_work_sync() or
2975 * cancel_work_sync() instead.
2976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977void flush_scheduled_work(void)
2978{
Tejun Heod320c032010-06-29 10:07:14 +02002979 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
Dave Jonesae90dd52006-06-30 01:40:45 -04002981EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002984 * execute_in_process_context - reliably execute the routine with user context
2985 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002986 * @ew: guaranteed storage for the execute work structure (must
2987 * be available when the work executes)
2988 *
2989 * Executes the function immediately if process context is available,
2990 * otherwise schedules the function for delayed execution.
2991 *
2992 * Returns: 0 - function was executed
2993 * 1 - function was scheduled for execution
2994 */
David Howells65f27f32006-11-22 14:55:48 +00002995int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002996{
2997 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002998 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002999 return 0;
3000 }
3001
David Howells65f27f32006-11-22 14:55:48 +00003002 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003003 schedule_work(&ew->work);
3004
3005 return 1;
3006}
3007EXPORT_SYMBOL_GPL(execute_in_process_context);
3008
Tejun Heo226223a2013-03-12 11:30:05 -07003009#ifdef CONFIG_SYSFS
3010/*
3011 * Workqueues with WQ_SYSFS flag set is visible to userland via
3012 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3013 * following attributes.
3014 *
3015 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3016 * max_active RW int : maximum number of in-flight work items
3017 *
3018 * Unbound workqueues have the following extra attributes.
3019 *
3020 * id RO int : the associated pool ID
3021 * nice RW int : nice value of the workers
3022 * cpumask RW mask : bitmask of allowed CPUs for the workers
3023 */
3024struct wq_device {
3025 struct workqueue_struct *wq;
3026 struct device dev;
3027};
3028
3029static struct workqueue_struct *dev_to_wq(struct device *dev)
3030{
3031 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3032
3033 return wq_dev->wq;
3034}
3035
3036static ssize_t wq_per_cpu_show(struct device *dev,
3037 struct device_attribute *attr, char *buf)
3038{
3039 struct workqueue_struct *wq = dev_to_wq(dev);
3040
3041 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3042}
3043
3044static ssize_t wq_max_active_show(struct device *dev,
3045 struct device_attribute *attr, char *buf)
3046{
3047 struct workqueue_struct *wq = dev_to_wq(dev);
3048
3049 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3050}
3051
3052static ssize_t wq_max_active_store(struct device *dev,
3053 struct device_attribute *attr,
3054 const char *buf, size_t count)
3055{
3056 struct workqueue_struct *wq = dev_to_wq(dev);
3057 int val;
3058
3059 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3060 return -EINVAL;
3061
3062 workqueue_set_max_active(wq, val);
3063 return count;
3064}
3065
3066static struct device_attribute wq_sysfs_attrs[] = {
3067 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3068 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3069 __ATTR_NULL,
3070};
3071
3072static ssize_t wq_pool_id_show(struct device *dev,
3073 struct device_attribute *attr, char *buf)
3074{
3075 struct workqueue_struct *wq = dev_to_wq(dev);
3076 struct worker_pool *pool;
3077 int written;
3078
3079 rcu_read_lock_sched();
3080 pool = first_pwq(wq)->pool;
3081 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3082 rcu_read_unlock_sched();
3083
3084 return written;
3085}
3086
3087static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3088 char *buf)
3089{
3090 struct workqueue_struct *wq = dev_to_wq(dev);
3091 int written;
3092
Tejun Heo6029a912013-04-01 11:23:34 -07003093 mutex_lock(&wq->mutex);
3094 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3095 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003096
3097 return written;
3098}
3099
3100/* prepare workqueue_attrs for sysfs store operations */
3101static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3102{
3103 struct workqueue_attrs *attrs;
3104
3105 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3106 if (!attrs)
3107 return NULL;
3108
Tejun Heo6029a912013-04-01 11:23:34 -07003109 mutex_lock(&wq->mutex);
3110 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3111 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003112 return attrs;
3113}
3114
3115static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3116 const char *buf, size_t count)
3117{
3118 struct workqueue_struct *wq = dev_to_wq(dev);
3119 struct workqueue_attrs *attrs;
3120 int ret;
3121
3122 attrs = wq_sysfs_prep_attrs(wq);
3123 if (!attrs)
3124 return -ENOMEM;
3125
3126 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3127 attrs->nice >= -20 && attrs->nice <= 19)
3128 ret = apply_workqueue_attrs(wq, attrs);
3129 else
3130 ret = -EINVAL;
3131
3132 free_workqueue_attrs(attrs);
3133 return ret ?: count;
3134}
3135
3136static ssize_t wq_cpumask_show(struct device *dev,
3137 struct device_attribute *attr, char *buf)
3138{
3139 struct workqueue_struct *wq = dev_to_wq(dev);
3140 int written;
3141
Tejun Heo6029a912013-04-01 11:23:34 -07003142 mutex_lock(&wq->mutex);
3143 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3144 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003145
3146 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3147 return written;
3148}
3149
3150static ssize_t wq_cpumask_store(struct device *dev,
3151 struct device_attribute *attr,
3152 const char *buf, size_t count)
3153{
3154 struct workqueue_struct *wq = dev_to_wq(dev);
3155 struct workqueue_attrs *attrs;
3156 int ret;
3157
3158 attrs = wq_sysfs_prep_attrs(wq);
3159 if (!attrs)
3160 return -ENOMEM;
3161
3162 ret = cpumask_parse(buf, attrs->cpumask);
3163 if (!ret)
3164 ret = apply_workqueue_attrs(wq, attrs);
3165
3166 free_workqueue_attrs(attrs);
3167 return ret ?: count;
3168}
3169
3170static struct device_attribute wq_sysfs_unbound_attrs[] = {
3171 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3172 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3173 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3174 __ATTR_NULL,
3175};
3176
3177static struct bus_type wq_subsys = {
3178 .name = "workqueue",
3179 .dev_attrs = wq_sysfs_attrs,
3180};
3181
3182static int __init wq_sysfs_init(void)
3183{
3184 return subsys_virtual_register(&wq_subsys, NULL);
3185}
3186core_initcall(wq_sysfs_init);
3187
3188static void wq_device_release(struct device *dev)
3189{
3190 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3191
3192 kfree(wq_dev);
3193}
3194
3195/**
3196 * workqueue_sysfs_register - make a workqueue visible in sysfs
3197 * @wq: the workqueue to register
3198 *
3199 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3200 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3201 * which is the preferred method.
3202 *
3203 * Workqueue user should use this function directly iff it wants to apply
3204 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3205 * apply_workqueue_attrs() may race against userland updating the
3206 * attributes.
3207 *
3208 * Returns 0 on success, -errno on failure.
3209 */
3210int workqueue_sysfs_register(struct workqueue_struct *wq)
3211{
3212 struct wq_device *wq_dev;
3213 int ret;
3214
3215 /*
3216 * Adjusting max_active or creating new pwqs by applyting
3217 * attributes breaks ordering guarantee. Disallow exposing ordered
3218 * workqueues.
3219 */
3220 if (WARN_ON(wq->flags & __WQ_ORDERED))
3221 return -EINVAL;
3222
3223 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3224 if (!wq_dev)
3225 return -ENOMEM;
3226
3227 wq_dev->wq = wq;
3228 wq_dev->dev.bus = &wq_subsys;
3229 wq_dev->dev.init_name = wq->name;
3230 wq_dev->dev.release = wq_device_release;
3231
3232 /*
3233 * unbound_attrs are created separately. Suppress uevent until
3234 * everything is ready.
3235 */
3236 dev_set_uevent_suppress(&wq_dev->dev, true);
3237
3238 ret = device_register(&wq_dev->dev);
3239 if (ret) {
3240 kfree(wq_dev);
3241 wq->wq_dev = NULL;
3242 return ret;
3243 }
3244
3245 if (wq->flags & WQ_UNBOUND) {
3246 struct device_attribute *attr;
3247
3248 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3249 ret = device_create_file(&wq_dev->dev, attr);
3250 if (ret) {
3251 device_unregister(&wq_dev->dev);
3252 wq->wq_dev = NULL;
3253 return ret;
3254 }
3255 }
3256 }
3257
3258 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3259 return 0;
3260}
3261
3262/**
3263 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3264 * @wq: the workqueue to unregister
3265 *
3266 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3267 */
3268static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3269{
3270 struct wq_device *wq_dev = wq->wq_dev;
3271
3272 if (!wq->wq_dev)
3273 return;
3274
3275 wq->wq_dev = NULL;
3276 device_unregister(&wq_dev->dev);
3277}
3278#else /* CONFIG_SYSFS */
3279static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3280#endif /* CONFIG_SYSFS */
3281
Tejun Heo7a4e3442013-03-12 11:30:00 -07003282/**
3283 * free_workqueue_attrs - free a workqueue_attrs
3284 * @attrs: workqueue_attrs to free
3285 *
3286 * Undo alloc_workqueue_attrs().
3287 */
3288void free_workqueue_attrs(struct workqueue_attrs *attrs)
3289{
3290 if (attrs) {
3291 free_cpumask_var(attrs->cpumask);
3292 kfree(attrs);
3293 }
3294}
3295
3296/**
3297 * alloc_workqueue_attrs - allocate a workqueue_attrs
3298 * @gfp_mask: allocation mask to use
3299 *
3300 * Allocate a new workqueue_attrs, initialize with default settings and
3301 * return it. Returns NULL on failure.
3302 */
3303struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3304{
3305 struct workqueue_attrs *attrs;
3306
3307 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3308 if (!attrs)
3309 goto fail;
3310 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3311 goto fail;
3312
Tejun Heo13e2e552013-04-01 11:23:31 -07003313 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003314 return attrs;
3315fail:
3316 free_workqueue_attrs(attrs);
3317 return NULL;
3318}
3319
Tejun Heo29c91e92013-03-12 11:30:03 -07003320static void copy_workqueue_attrs(struct workqueue_attrs *to,
3321 const struct workqueue_attrs *from)
3322{
3323 to->nice = from->nice;
3324 cpumask_copy(to->cpumask, from->cpumask);
3325}
3326
Tejun Heo29c91e92013-03-12 11:30:03 -07003327/* hash value of the content of @attr */
3328static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3329{
3330 u32 hash = 0;
3331
3332 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003333 hash = jhash(cpumask_bits(attrs->cpumask),
3334 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003335 return hash;
3336}
3337
3338/* content equality test */
3339static bool wqattrs_equal(const struct workqueue_attrs *a,
3340 const struct workqueue_attrs *b)
3341{
3342 if (a->nice != b->nice)
3343 return false;
3344 if (!cpumask_equal(a->cpumask, b->cpumask))
3345 return false;
3346 return true;
3347}
3348
Tejun Heo7a4e3442013-03-12 11:30:00 -07003349/**
3350 * init_worker_pool - initialize a newly zalloc'd worker_pool
3351 * @pool: worker_pool to initialize
3352 *
3353 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003354 * Returns 0 on success, -errno on failure. Even on failure, all fields
3355 * inside @pool proper are initialized and put_unbound_pool() can be called
3356 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003357 */
3358static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003359{
3360 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003361 pool->id = -1;
3362 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003363 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003364 pool->flags |= POOL_DISASSOCIATED;
3365 INIT_LIST_HEAD(&pool->worklist);
3366 INIT_LIST_HEAD(&pool->idle_list);
3367 hash_init(pool->busy_hash);
3368
3369 init_timer_deferrable(&pool->idle_timer);
3370 pool->idle_timer.function = idle_worker_timeout;
3371 pool->idle_timer.data = (unsigned long)pool;
3372
3373 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3374 (unsigned long)pool);
3375
3376 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003377 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003378 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003379
Tejun Heo29c91e92013-03-12 11:30:03 -07003380 INIT_HLIST_NODE(&pool->hash_node);
3381 pool->refcnt = 1;
3382
3383 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003384 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3385 if (!pool->attrs)
3386 return -ENOMEM;
3387 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003388}
3389
Tejun Heo29c91e92013-03-12 11:30:03 -07003390static void rcu_free_pool(struct rcu_head *rcu)
3391{
3392 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3393
Tejun Heo822d8402013-03-19 13:45:21 -07003394 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003395 free_workqueue_attrs(pool->attrs);
3396 kfree(pool);
3397}
3398
3399/**
3400 * put_unbound_pool - put a worker_pool
3401 * @pool: worker_pool to put
3402 *
3403 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003404 * safe manner. get_unbound_pool() calls this function on its failure path
3405 * and this function should be able to release pools which went through,
3406 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003407 *
3408 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003409 */
3410static void put_unbound_pool(struct worker_pool *pool)
3411{
3412 struct worker *worker;
3413
Tejun Heoa892cac2013-04-01 11:23:32 -07003414 lockdep_assert_held(&wq_pool_mutex);
3415
3416 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003417 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003418
3419 /* sanity checks */
3420 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003421 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003422 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003423
3424 /* release id and unhash */
3425 if (pool->id >= 0)
3426 idr_remove(&worker_pool_idr, pool->id);
3427 hash_del(&pool->hash_node);
3428
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003429 /*
3430 * Become the manager and destroy all workers. Grabbing
3431 * manager_arb prevents @pool's workers from blocking on
3432 * manager_mutex.
3433 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003434 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003435 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003436 spin_lock_irq(&pool->lock);
3437
3438 while ((worker = first_worker(pool)))
3439 destroy_worker(worker);
3440 WARN_ON(pool->nr_workers || pool->nr_idle);
3441
3442 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003443 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003444 mutex_unlock(&pool->manager_arb);
3445
3446 /* shut down the timers */
3447 del_timer_sync(&pool->idle_timer);
3448 del_timer_sync(&pool->mayday_timer);
3449
3450 /* sched-RCU protected to allow dereferences from get_work_pool() */
3451 call_rcu_sched(&pool->rcu, rcu_free_pool);
3452}
3453
3454/**
3455 * get_unbound_pool - get a worker_pool with the specified attributes
3456 * @attrs: the attributes of the worker_pool to get
3457 *
3458 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3459 * reference count and return it. If there already is a matching
3460 * worker_pool, it will be used; otherwise, this function attempts to
3461 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003462 *
3463 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003464 */
3465static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3466{
Tejun Heo29c91e92013-03-12 11:30:03 -07003467 u32 hash = wqattrs_hash(attrs);
3468 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003469 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003470
Tejun Heoa892cac2013-04-01 11:23:32 -07003471 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003472
3473 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003474 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3475 if (wqattrs_equal(pool->attrs, attrs)) {
3476 pool->refcnt++;
3477 goto out_unlock;
3478 }
3479 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003480
3481 /* nope, create a new one */
3482 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3483 if (!pool || init_worker_pool(pool) < 0)
3484 goto fail;
3485
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003486 if (workqueue_freezing)
3487 pool->flags |= POOL_FREEZING;
3488
Tejun Heo8864b4e2013-03-12 11:30:04 -07003489 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003490 copy_workqueue_attrs(pool->attrs, attrs);
3491
Tejun Heof3f90ad2013-04-01 11:23:34 -07003492 /* if cpumask is contained inside a NUMA node, we belong to that node */
3493 if (wq_numa_enabled) {
3494 for_each_node(node) {
3495 if (cpumask_subset(pool->attrs->cpumask,
3496 wq_numa_possible_cpumask[node])) {
3497 pool->node = node;
3498 break;
3499 }
3500 }
3501 }
3502
Tejun Heo29c91e92013-03-12 11:30:03 -07003503 if (worker_pool_assign_id(pool) < 0)
3504 goto fail;
3505
3506 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003507 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003508 goto fail;
3509
Tejun Heo29c91e92013-03-12 11:30:03 -07003510 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003511 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3512out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003513 return pool;
3514fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003515 if (pool)
3516 put_unbound_pool(pool);
3517 return NULL;
3518}
3519
Tejun Heo8864b4e2013-03-12 11:30:04 -07003520static void rcu_free_pwq(struct rcu_head *rcu)
3521{
3522 kmem_cache_free(pwq_cache,
3523 container_of(rcu, struct pool_workqueue, rcu));
3524}
3525
3526/*
3527 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3528 * and needs to be destroyed.
3529 */
3530static void pwq_unbound_release_workfn(struct work_struct *work)
3531{
3532 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3533 unbound_release_work);
3534 struct workqueue_struct *wq = pwq->wq;
3535 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003536 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003537
3538 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3539 return;
3540
Tejun Heo75ccf592013-03-12 11:30:04 -07003541 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003542 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003543 * necessary on release but do it anyway. It's easier to verify
3544 * and consistent with the linking path.
3545 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003546 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003547 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003548 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003549 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003550
Tejun Heoa892cac2013-04-01 11:23:32 -07003551 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003552 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003553 mutex_unlock(&wq_pool_mutex);
3554
Tejun Heo8864b4e2013-03-12 11:30:04 -07003555 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3556
3557 /*
3558 * If we're the last pwq going away, @wq is already dead and no one
3559 * is gonna access it anymore. Free it.
3560 */
Tejun Heo6029a912013-04-01 11:23:34 -07003561 if (is_last) {
3562 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003563 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003564 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003565}
3566
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003567/**
Tejun Heo699ce092013-03-13 16:51:35 -07003568 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003569 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003570 *
Tejun Heo699ce092013-03-13 16:51:35 -07003571 * If @pwq isn't freezing, set @pwq->max_active to the associated
3572 * workqueue's saved_max_active and activate delayed work items
3573 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003574 */
Tejun Heo699ce092013-03-13 16:51:35 -07003575static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003576{
Tejun Heo699ce092013-03-13 16:51:35 -07003577 struct workqueue_struct *wq = pwq->wq;
3578 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003579
Tejun Heo699ce092013-03-13 16:51:35 -07003580 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003581 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003582
3583 /* fast exit for non-freezable wqs */
3584 if (!freezable && pwq->max_active == wq->saved_max_active)
3585 return;
3586
Lai Jiangshana357fc02013-03-25 16:57:19 -07003587 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003588
3589 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3590 pwq->max_active = wq->saved_max_active;
3591
3592 while (!list_empty(&pwq->delayed_works) &&
3593 pwq->nr_active < pwq->max_active)
3594 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003595
3596 /*
3597 * Need to kick a worker after thawed or an unbound wq's
3598 * max_active is bumped. It's a slow path. Do it always.
3599 */
3600 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003601 } else {
3602 pwq->max_active = 0;
3603 }
3604
Lai Jiangshana357fc02013-03-25 16:57:19 -07003605 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003606}
3607
Tejun Heod2c1d402013-03-12 11:30:04 -07003608static void init_and_link_pwq(struct pool_workqueue *pwq,
3609 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003610 struct worker_pool *pool,
3611 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003612{
3613 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3614
3615 pwq->pool = pool;
3616 pwq->wq = wq;
3617 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003618 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003619 INIT_LIST_HEAD(&pwq->delayed_works);
3620 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003621 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003622
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003623 mutex_lock(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003624
Tejun Heo983ca252013-03-13 16:51:35 -07003625 /*
3626 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003627 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003628 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003629 if (p_last_pwq)
3630 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003631 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003632
3633 /* sync max_active to the current setting */
3634 pwq_adjust_max_active(pwq);
3635
3636 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003637 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003638
Tejun Heo6029a912013-04-01 11:23:34 -07003639 if (wq->flags & WQ_UNBOUND)
3640 copy_workqueue_attrs(wq->unbound_attrs, pool->attrs);
3641
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003642 mutex_unlock(&wq->mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003643}
3644
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003645/**
3646 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3647 * @wq: the target workqueue
3648 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3649 *
3650 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3651 * current attributes, a new pwq is created and made the first pwq which
3652 * will serve all new work items. Older pwqs are released as in-flight
3653 * work items finish. Note that a work item which repeatedly requeues
3654 * itself back-to-back will stay on its current pwq.
3655 *
3656 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3657 * failure.
3658 */
3659int apply_workqueue_attrs(struct workqueue_struct *wq,
3660 const struct workqueue_attrs *attrs)
3661{
Tejun Heo13e2e552013-04-01 11:23:31 -07003662 struct workqueue_attrs *new_attrs;
3663 struct pool_workqueue *pwq = NULL, *last_pwq;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003664 struct worker_pool *pool;
Tejun Heo48621252013-04-01 11:23:31 -07003665 int ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003666
Tejun Heo8719dce2013-03-12 11:30:04 -07003667 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003668 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3669 return -EINVAL;
3670
Tejun Heo8719dce2013-03-12 11:30:04 -07003671 /* creating multiple pwqs breaks ordering guarantee */
3672 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3673 return -EINVAL;
3674
Tejun Heo13e2e552013-04-01 11:23:31 -07003675 /* make a copy of @attrs and sanitize it */
3676 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3677 if (!new_attrs)
3678 goto enomem;
3679
3680 copy_workqueue_attrs(new_attrs, attrs);
3681 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3682
Tejun Heoa892cac2013-04-01 11:23:32 -07003683 mutex_lock(&wq_pool_mutex);
3684
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003685 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
Tejun Heoa892cac2013-04-01 11:23:32 -07003686 if (!pwq) {
3687 mutex_unlock(&wq_pool_mutex);
Tejun Heo13e2e552013-04-01 11:23:31 -07003688 goto enomem;
Tejun Heoa892cac2013-04-01 11:23:32 -07003689 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003690
Tejun Heo13e2e552013-04-01 11:23:31 -07003691 pool = get_unbound_pool(new_attrs);
Tejun Heoa892cac2013-04-01 11:23:32 -07003692 if (!pool) {
3693 mutex_unlock(&wq_pool_mutex);
Tejun Heo13e2e552013-04-01 11:23:31 -07003694 goto enomem;
Tejun Heoa892cac2013-04-01 11:23:32 -07003695 }
3696
3697 mutex_unlock(&wq_pool_mutex);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003698
3699 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3700 if (last_pwq) {
3701 spin_lock_irq(&last_pwq->pool->lock);
3702 put_pwq(last_pwq);
3703 spin_unlock_irq(&last_pwq->pool->lock);
3704 }
3705
Tejun Heo48621252013-04-01 11:23:31 -07003706 ret = 0;
3707 /* fall through */
3708out_free:
3709 free_workqueue_attrs(new_attrs);
3710 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003711
3712enomem:
3713 kmem_cache_free(pwq_cache, pwq);
Tejun Heo48621252013-04-01 11:23:31 -07003714 ret = -ENOMEM;
3715 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003716}
3717
Tejun Heo30cdf242013-03-12 11:29:57 -07003718static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003720 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003721 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003722
Tejun Heo30cdf242013-03-12 11:29:57 -07003723 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003724 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3725 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003726 return -ENOMEM;
3727
3728 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003729 struct pool_workqueue *pwq =
3730 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003731 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003732 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003733
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003734 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003735 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003736 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003737 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003738 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003739 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003740}
3741
Tejun Heof3421792010-07-02 10:03:51 +02003742static int wq_clamp_max_active(int max_active, unsigned int flags,
3743 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003744{
Tejun Heof3421792010-07-02 10:03:51 +02003745 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3746
3747 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003748 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3749 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003750
Tejun Heof3421792010-07-02 10:03:51 +02003751 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003752}
3753
Tejun Heob196be82012-01-10 15:11:35 -08003754struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003755 unsigned int flags,
3756 int max_active,
3757 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003758 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003759{
Tejun Heob196be82012-01-10 15:11:35 -08003760 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003761 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003762 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003763 size_t namelen;
3764
3765 /* determine namelen, allocate wq and format name */
3766 va_start(args, lock_name);
3767 va_copy(args1, args);
3768 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3769
3770 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3771 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003772 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003773
Tejun Heo6029a912013-04-01 11:23:34 -07003774 if (flags & WQ_UNBOUND) {
3775 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3776 if (!wq->unbound_attrs)
3777 goto err_free_wq;
3778 }
3779
Tejun Heob196be82012-01-10 15:11:35 -08003780 vsnprintf(wq->name, namelen, fmt, args1);
3781 va_end(args);
3782 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003783
Tejun Heod320c032010-06-29 10:07:14 +02003784 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003785 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003786
Tejun Heob196be82012-01-10 15:11:35 -08003787 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003788 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003790 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003791 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003792 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003793 INIT_LIST_HEAD(&wq->flusher_queue);
3794 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003795 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003796
Johannes Bergeb13ba82008-01-16 09:51:58 +01003797 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003798 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003799
Tejun Heo30cdf242013-03-12 11:29:57 -07003800 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003801 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003802
Tejun Heo493008a2013-03-12 11:30:03 -07003803 /*
3804 * Workqueues which may be used during memory reclaim should
3805 * have a rescuer to guarantee forward progress.
3806 */
3807 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003808 struct worker *rescuer;
3809
Tejun Heod2c1d402013-03-12 11:30:04 -07003810 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003811 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003812 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003813
Tejun Heo111c2252013-01-17 17:16:24 -08003814 rescuer->rescue_wq = wq;
3815 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003816 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003817 if (IS_ERR(rescuer->task)) {
3818 kfree(rescuer);
3819 goto err_destroy;
3820 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003821
Tejun Heod2c1d402013-03-12 11:30:04 -07003822 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003823 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003824 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003825 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003826
Tejun Heo226223a2013-03-12 11:30:05 -07003827 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3828 goto err_destroy;
3829
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003830 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003831 * wq_pool_mutex protects global freeze state and workqueues list.
3832 * Grab it, adjust max_active and add the new @wq to workqueues
3833 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003834 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003835 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003836
Lai Jiangshana357fc02013-03-25 16:57:19 -07003837 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003838 for_each_pwq(pwq, wq)
3839 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003840 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003841
Tejun Heo15376632010-06-29 10:07:11 +02003842 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003843
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003844 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003845
Oleg Nesterov3af244332007-05-09 02:34:09 -07003846 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003847
3848err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07003849 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07003850 kfree(wq);
3851 return NULL;
3852err_destroy:
3853 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003854 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003855}
Tejun Heod320c032010-06-29 10:07:14 +02003856EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003857
3858/**
3859 * destroy_workqueue - safely terminate a workqueue
3860 * @wq: target workqueue
3861 *
3862 * Safely destroy a workqueue. All work currently pending will be done first.
3863 */
3864void destroy_workqueue(struct workqueue_struct *wq)
3865{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003866 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003867
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003868 /* drain it before proceeding with destruction */
3869 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003870
Tejun Heo6183c002013-03-12 11:29:57 -07003871 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003872 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003873 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003874 int i;
3875
Tejun Heo76af4d92013-03-12 11:30:00 -07003876 for (i = 0; i < WORK_NR_COLORS; i++) {
3877 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003878 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003879 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003880 }
3881 }
3882
Tejun Heo8864b4e2013-03-12 11:30:04 -07003883 if (WARN_ON(pwq->refcnt > 1) ||
3884 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003885 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003886 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003887 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003888 }
Tejun Heo6183c002013-03-12 11:29:57 -07003889 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003890 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003891
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003892 /*
3893 * wq list is used to freeze wq, remove from list after
3894 * flushing is complete in case freeze races us.
3895 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003896 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003897 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003898 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003899
Tejun Heo226223a2013-03-12 11:30:05 -07003900 workqueue_sysfs_unregister(wq);
3901
Tejun Heo493008a2013-03-12 11:30:03 -07003902 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003903 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003904 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003905 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003906 }
3907
Tejun Heo8864b4e2013-03-12 11:30:04 -07003908 if (!(wq->flags & WQ_UNBOUND)) {
3909 /*
3910 * The base ref is never dropped on per-cpu pwqs. Directly
3911 * free the pwqs and wq.
3912 */
3913 free_percpu(wq->cpu_pwqs);
3914 kfree(wq);
3915 } else {
3916 /*
3917 * We're the sole accessor of @wq at this point. Directly
3918 * access the first pwq and put the base ref. As both pwqs
3919 * and pools are sched-RCU protected, the lock operations
3920 * are safe. @wq will be freed when the last pwq is
3921 * released.
3922 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003923 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3924 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003925 spin_lock_irq(&pwq->pool->lock);
3926 put_pwq(pwq);
3927 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003928 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003929}
3930EXPORT_SYMBOL_GPL(destroy_workqueue);
3931
Tejun Heodcd989c2010-06-29 10:07:14 +02003932/**
3933 * workqueue_set_max_active - adjust max_active of a workqueue
3934 * @wq: target workqueue
3935 * @max_active: new max_active value.
3936 *
3937 * Set max_active of @wq to @max_active.
3938 *
3939 * CONTEXT:
3940 * Don't call from IRQ context.
3941 */
3942void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3943{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003944 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003945
Tejun Heo8719dce2013-03-12 11:30:04 -07003946 /* disallow meddling with max_active for ordered workqueues */
3947 if (WARN_ON(wq->flags & __WQ_ORDERED))
3948 return;
3949
Tejun Heof3421792010-07-02 10:03:51 +02003950 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003951
Lai Jiangshana357fc02013-03-25 16:57:19 -07003952 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003953
3954 wq->saved_max_active = max_active;
3955
Tejun Heo699ce092013-03-13 16:51:35 -07003956 for_each_pwq(pwq, wq)
3957 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003958
Lai Jiangshana357fc02013-03-25 16:57:19 -07003959 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003960}
3961EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3962
3963/**
Tejun Heoe6267612013-03-12 17:41:37 -07003964 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3965 *
3966 * Determine whether %current is a workqueue rescuer. Can be used from
3967 * work functions to determine whether it's being run off the rescuer task.
3968 */
3969bool current_is_workqueue_rescuer(void)
3970{
3971 struct worker *worker = current_wq_worker();
3972
Lai Jiangshan6a092df2013-03-20 03:28:03 +08003973 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07003974}
3975
3976/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003977 * workqueue_congested - test whether a workqueue is congested
3978 * @cpu: CPU in question
3979 * @wq: target workqueue
3980 *
3981 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3982 * no synchronization around this function and the test result is
3983 * unreliable and only useful as advisory hints or for debugging.
3984 *
3985 * RETURNS:
3986 * %true if congested, %false otherwise.
3987 */
Tejun Heod84ff052013-03-12 11:29:59 -07003988bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003989{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003990 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003991 bool ret;
3992
Lai Jiangshan88109452013-03-20 03:28:10 +08003993 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003994
3995 if (!(wq->flags & WQ_UNBOUND))
3996 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3997 else
3998 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003999
Tejun Heo76af4d92013-03-12 11:30:00 -07004000 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004001 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004002
4003 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004004}
4005EXPORT_SYMBOL_GPL(workqueue_congested);
4006
4007/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004008 * work_busy - test whether a work is currently pending or running
4009 * @work: the work to be tested
4010 *
4011 * Test whether @work is currently pending or running. There is no
4012 * synchronization around this function and the test result is
4013 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004014 *
4015 * RETURNS:
4016 * OR'd bitmask of WORK_BUSY_* bits.
4017 */
4018unsigned int work_busy(struct work_struct *work)
4019{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004020 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004021 unsigned long flags;
4022 unsigned int ret = 0;
4023
Tejun Heodcd989c2010-06-29 10:07:14 +02004024 if (work_pending(work))
4025 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004026
Tejun Heofa1b54e2013-03-12 11:30:00 -07004027 local_irq_save(flags);
4028 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004029 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004030 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004031 if (find_worker_executing_work(pool, work))
4032 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004033 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004034 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004035 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004036
4037 return ret;
4038}
4039EXPORT_SYMBOL_GPL(work_busy);
4040
Tejun Heodb7bccf2010-06-29 10:07:12 +02004041/*
4042 * CPU hotplug.
4043 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004044 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004045 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004046 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004047 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004048 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004049 * blocked draining impractical.
4050 *
Tejun Heo24647572013-01-24 11:01:33 -08004051 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004052 * running as an unbound one and allowing it to be reattached later if the
4053 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004054 */
4055
Tejun Heo706026c2013-01-24 11:01:34 -08004056static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004057{
Tejun Heo38db41d2013-01-24 11:01:34 -08004058 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004059 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004060 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004061 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004062
Tejun Heof02ae732013-03-12 11:30:03 -07004063 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004064 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004065
Tejun Heobc3a1af2013-03-13 19:47:39 -07004066 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004067 spin_lock_irq(&pool->lock);
4068
4069 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004070 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004071 * unbound and set DISASSOCIATED. Before this, all workers
4072 * except for the ones which are still executing works from
4073 * before the last CPU down must be on the cpu. After
4074 * this, they may become diasporas.
4075 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004076 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004077 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004078
Tejun Heo24647572013-01-24 11:01:33 -08004079 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004080
Tejun Heo94cf58b2013-01-24 11:01:33 -08004081 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004082 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004083 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004084
4085 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004086 * Call schedule() so that we cross rq->lock and thus can guarantee
4087 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4088 * as scheduler callbacks may be invoked from other cpus.
4089 */
4090 schedule();
4091
4092 /*
4093 * Sched callbacks are disabled now. Zap nr_running. After this,
4094 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004095 * are always true as long as the worklist is not empty. Pools on
4096 * @cpu now behave as unbound (in terms of concurrency management)
4097 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004098 *
4099 * On return from this function, the current worker would trigger
4100 * unbound chain execution of pending work items if other workers
4101 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004102 */
Tejun Heof02ae732013-03-12 11:30:03 -07004103 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004104 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004105}
4106
Tejun Heobd7c0892013-03-19 13:45:21 -07004107/**
4108 * rebind_workers - rebind all workers of a pool to the associated CPU
4109 * @pool: pool of interest
4110 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004111 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004112 */
4113static void rebind_workers(struct worker_pool *pool)
4114{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004115 struct worker *worker;
4116 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004117
4118 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004119
Tejun Heoa9ab7752013-03-19 13:45:21 -07004120 /*
4121 * Restore CPU affinity of all workers. As all idle workers should
4122 * be on the run-queue of the associated CPU before any local
4123 * wake-ups for concurrency management happen, restore CPU affinty
4124 * of all workers first and then clear UNBOUND. As we're called
4125 * from CPU_ONLINE, the following shouldn't fail.
4126 */
4127 for_each_pool_worker(worker, wi, pool)
4128 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4129 pool->attrs->cpumask) < 0);
4130
4131 spin_lock_irq(&pool->lock);
4132
4133 for_each_pool_worker(worker, wi, pool) {
4134 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004135
4136 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004137 * A bound idle worker should actually be on the runqueue
4138 * of the associated CPU for local wake-ups targeting it to
4139 * work. Kick all idle workers so that they migrate to the
4140 * associated CPU. Doing this in the same loop as
4141 * replacing UNBOUND with REBOUND is safe as no worker will
4142 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004143 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004144 if (worker_flags & WORKER_IDLE)
4145 wake_up_process(worker->task);
4146
4147 /*
4148 * We want to clear UNBOUND but can't directly call
4149 * worker_clr_flags() or adjust nr_running. Atomically
4150 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4151 * @worker will clear REBOUND using worker_clr_flags() when
4152 * it initiates the next execution cycle thus restoring
4153 * concurrency management. Note that when or whether
4154 * @worker clears REBOUND doesn't affect correctness.
4155 *
4156 * ACCESS_ONCE() is necessary because @worker->flags may be
4157 * tested without holding any lock in
4158 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4159 * fail incorrectly leading to premature concurrency
4160 * management operations.
4161 */
4162 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4163 worker_flags |= WORKER_REBOUND;
4164 worker_flags &= ~WORKER_UNBOUND;
4165 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004166 }
4167
Tejun Heoa9ab7752013-03-19 13:45:21 -07004168 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004169}
4170
Tejun Heo7dbc7252013-03-19 13:45:21 -07004171/**
4172 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4173 * @pool: unbound pool of interest
4174 * @cpu: the CPU which is coming up
4175 *
4176 * An unbound pool may end up with a cpumask which doesn't have any online
4177 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4178 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4179 * online CPU before, cpus_allowed of all its workers should be restored.
4180 */
4181static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4182{
4183 static cpumask_t cpumask;
4184 struct worker *worker;
4185 int wi;
4186
4187 lockdep_assert_held(&pool->manager_mutex);
4188
4189 /* is @cpu allowed for @pool? */
4190 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4191 return;
4192
4193 /* is @cpu the only online CPU? */
4194 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4195 if (cpumask_weight(&cpumask) != 1)
4196 return;
4197
4198 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4199 for_each_pool_worker(worker, wi, pool)
4200 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4201 pool->attrs->cpumask) < 0);
4202}
4203
Tejun Heo8db25e72012-07-17 12:39:28 -07004204/*
4205 * Workqueues should be brought up before normal priority CPU notifiers.
4206 * This will be registered high priority CPU notifier.
4207 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004208static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004209 unsigned long action,
4210 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004211{
Tejun Heod84ff052013-03-12 11:29:59 -07004212 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004213 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004214 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Tejun Heo8db25e72012-07-17 12:39:28 -07004216 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004218 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004219 if (pool->nr_workers)
4220 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004221 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004222 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004224 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004225
Tejun Heo65758202012-07-17 12:39:26 -07004226 case CPU_DOWN_FAILED:
4227 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004228 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004229
4230 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004231 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004232
Tejun Heo7dbc7252013-03-19 13:45:21 -07004233 if (pool->cpu == cpu) {
4234 spin_lock_irq(&pool->lock);
4235 pool->flags &= ~POOL_DISASSOCIATED;
4236 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004237
Tejun Heo7dbc7252013-03-19 13:45:21 -07004238 rebind_workers(pool);
4239 } else if (pool->cpu < 0) {
4240 restore_unbound_workers_cpumask(pool, cpu);
4241 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004242
Tejun Heobc3a1af2013-03-13 19:47:39 -07004243 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004244 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004245
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004246 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004247 break;
Tejun Heo65758202012-07-17 12:39:26 -07004248 }
4249 return NOTIFY_OK;
4250}
4251
4252/*
4253 * Workqueues should be brought down after normal priority CPU notifiers.
4254 * This will be registered as low priority CPU notifier.
4255 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004256static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004257 unsigned long action,
4258 void *hcpu)
4259{
Tejun Heod84ff052013-03-12 11:29:59 -07004260 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004261 struct work_struct unbind_work;
4262
Tejun Heo65758202012-07-17 12:39:26 -07004263 switch (action & ~CPU_TASKS_FROZEN) {
4264 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004265 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004266 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004267 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004268 flush_work(&unbind_work);
4269 break;
Tejun Heo65758202012-07-17 12:39:26 -07004270 }
4271 return NOTIFY_OK;
4272}
4273
Rusty Russell2d3854a2008-11-05 13:39:10 +11004274#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004275
Rusty Russell2d3854a2008-11-05 13:39:10 +11004276struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004277 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004278 long (*fn)(void *);
4279 void *arg;
4280 long ret;
4281};
4282
Tejun Heoed48ece2012-09-18 12:48:43 -07004283static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004284{
Tejun Heoed48ece2012-09-18 12:48:43 -07004285 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4286
Rusty Russell2d3854a2008-11-05 13:39:10 +11004287 wfc->ret = wfc->fn(wfc->arg);
4288}
4289
4290/**
4291 * work_on_cpu - run a function in user context on a particular cpu
4292 * @cpu: the cpu to run on
4293 * @fn: the function to run
4294 * @arg: the function arg
4295 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004296 * This will return the value @fn returns.
4297 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004298 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004299 */
Tejun Heod84ff052013-03-12 11:29:59 -07004300long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004301{
Tejun Heoed48ece2012-09-18 12:48:43 -07004302 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004303
Tejun Heoed48ece2012-09-18 12:48:43 -07004304 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4305 schedule_work_on(cpu, &wfc.work);
4306 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004307 return wfc.ret;
4308}
4309EXPORT_SYMBOL_GPL(work_on_cpu);
4310#endif /* CONFIG_SMP */
4311
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004312#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304313
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004314/**
4315 * freeze_workqueues_begin - begin freezing workqueues
4316 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004317 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004318 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004319 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004320 *
4321 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004322 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004323 */
4324void freeze_workqueues_begin(void)
4325{
Tejun Heo17116962013-03-12 11:29:58 -07004326 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004327 struct workqueue_struct *wq;
4328 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004329 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004330
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004331 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004332
Tejun Heo6183c002013-03-12 11:29:57 -07004333 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004334 workqueue_freezing = true;
4335
Tejun Heo24b8a842013-03-12 11:29:58 -07004336 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004337 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004338 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004339 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4340 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004341 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004342 }
4343
Tejun Heo24b8a842013-03-12 11:29:58 -07004344 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004345 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004346 for_each_pwq(pwq, wq)
4347 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004348 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004349 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004350
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004351 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004353
4354/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004355 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004356 *
4357 * Check whether freezing is complete. This function must be called
4358 * between freeze_workqueues_begin() and thaw_workqueues().
4359 *
4360 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004361 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004362 *
4363 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004364 * %true if some freezable workqueues are still busy. %false if freezing
4365 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004366 */
4367bool freeze_workqueues_busy(void)
4368{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004369 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004370 struct workqueue_struct *wq;
4371 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004372
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004373 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004374
Tejun Heo6183c002013-03-12 11:29:57 -07004375 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004376
Tejun Heo24b8a842013-03-12 11:29:58 -07004377 list_for_each_entry(wq, &workqueues, list) {
4378 if (!(wq->flags & WQ_FREEZABLE))
4379 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004380 /*
4381 * nr_active is monotonically decreasing. It's safe
4382 * to peek without lock.
4383 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004384 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004385 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004386 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004387 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004388 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004389 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004390 goto out_unlock;
4391 }
4392 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004393 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004394 }
4395out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004396 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004397 return busy;
4398}
4399
4400/**
4401 * thaw_workqueues - thaw workqueues
4402 *
4403 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004404 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004405 *
4406 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004407 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004408 */
4409void thaw_workqueues(void)
4410{
Tejun Heo24b8a842013-03-12 11:29:58 -07004411 struct workqueue_struct *wq;
4412 struct pool_workqueue *pwq;
4413 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004414 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004415
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004416 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004417
4418 if (!workqueue_freezing)
4419 goto out_unlock;
4420
Tejun Heo24b8a842013-03-12 11:29:58 -07004421 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004422 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004423 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004424 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4425 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004426 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004427 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004428
Tejun Heo24b8a842013-03-12 11:29:58 -07004429 /* restore max_active and repopulate worklist */
4430 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004431 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004432 for_each_pwq(pwq, wq)
4433 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004434 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004435 }
4436
4437 workqueue_freezing = false;
4438out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004439 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004440}
4441#endif /* CONFIG_FREEZER */
4442
Tejun Heobce90382013-04-01 11:23:32 -07004443static void __init wq_numa_init(void)
4444{
4445 cpumask_var_t *tbl;
4446 int node, cpu;
4447
4448 /* determine NUMA pwq table len - highest node id + 1 */
4449 for_each_node(node)
4450 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4451
4452 if (num_possible_nodes() <= 1)
4453 return;
4454
4455 /*
4456 * We want masks of possible CPUs of each node which isn't readily
4457 * available. Build one from cpu_to_node() which should have been
4458 * fully initialized by now.
4459 */
4460 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4461 BUG_ON(!tbl);
4462
4463 for_each_node(node)
4464 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4465
4466 for_each_possible_cpu(cpu) {
4467 node = cpu_to_node(cpu);
4468 if (WARN_ON(node == NUMA_NO_NODE)) {
4469 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4470 /* happens iff arch is bonkers, let's just proceed */
4471 return;
4472 }
4473 cpumask_set_cpu(cpu, tbl[node]);
4474 }
4475
4476 wq_numa_possible_cpumask = tbl;
4477 wq_numa_enabled = true;
4478}
4479
Suresh Siddha6ee05782010-07-30 14:57:37 -07004480static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004482 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4483 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004484
Tejun Heo7c3eed52013-01-24 11:01:33 -08004485 /* make sure we have enough bits for OFFQ pool ID */
4486 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004487 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004488
Tejun Heoe904e6c2013-03-12 11:29:57 -07004489 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4490
4491 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4492
Tejun Heo65758202012-07-17 12:39:26 -07004493 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004494 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004495
Tejun Heobce90382013-04-01 11:23:32 -07004496 wq_numa_init();
4497
Tejun Heo706026c2013-01-24 11:01:34 -08004498 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004499 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004500 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004501
Tejun Heo7a4e3442013-03-12 11:30:00 -07004502 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004503 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004504 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004505 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004506 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004507 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004508 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004509
Tejun Heo9daf9e62013-01-24 11:01:33 -08004510 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004511 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004512 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004513 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004514 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004515 }
4516
Tejun Heoe22bee72010-06-29 10:07:14 +02004517 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004518 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004519 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004520
Tejun Heof02ae732013-03-12 11:30:03 -07004521 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004522 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004523 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004524 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004525 }
4526
Tejun Heo29c91e92013-03-12 11:30:03 -07004527 /* create default unbound wq attrs */
4528 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4529 struct workqueue_attrs *attrs;
4530
4531 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004532 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004533 unbound_std_wq_attrs[i] = attrs;
4534 }
4535
Tejun Heod320c032010-06-29 10:07:14 +02004536 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004537 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004538 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004539 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4540 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004541 system_freezable_wq = alloc_workqueue("events_freezable",
4542 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004543 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004544 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004547early_initcall(init_workqueues);