blob: 248d18aa2a5d68ea31f600721ffa920af595c240 [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 Heo9daf9e62013-01-24 11:01:33 -0800141 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700142 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700143
144 struct list_head worklist; /* L: list of pending works */
145 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700146
147 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700148 int nr_idle; /* L: currently idle ones */
149
150 struct list_head idle_list; /* X: list of idle workers */
151 struct timer_list idle_timer; /* L: worker idle timeout */
152 struct timer_list mayday_timer; /* L: SOS timer for workers */
153
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700154 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800155 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
156 /* L: hash of busy workers */
157
Tejun Heobc3a1af2013-03-13 19:47:39 -0700158 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700159 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700160 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700161 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800162
Tejun Heo7a4e3442013-03-12 11:30:00 -0700163 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700164 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
165 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700166
Tejun Heoe19e3972013-01-24 11:39:44 -0800167 /*
168 * The current concurrency level. As it's likely to be accessed
169 * from other CPUs during try_to_wake_up(), put it in a separate
170 * cacheline.
171 */
172 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700173
174 /*
175 * Destruction of pool is sched-RCU protected to allow dereferences
176 * from get_work_pool().
177 */
178 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200179} ____cacheline_aligned_in_smp;
180
181/*
Tejun Heo112202d2013-02-13 19:29:12 -0800182 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
183 * of work_struct->data are used for flags and the remaining high bits
184 * point to the pwq; thus, pwqs need to be aligned at two's power of the
185 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Tejun Heo112202d2013-02-13 19:29:12 -0800187struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700188 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200189 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200190 int work_color; /* L: current color */
191 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700192 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200193 int nr_in_flight[WORK_NR_COLORS];
194 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200195 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200196 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700198 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700199 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700200
201 /*
202 * Release of unbound pwq is punted to system_wq. See put_pwq()
203 * and pwq_unbound_release_workfn() for details. pool_workqueue
204 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700205 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700206 */
207 struct work_struct unbound_release_work;
208 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700209} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200212 * Structure used to wait for workqueue flush.
213 */
214struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700215 struct list_head list; /* WQ: list of flushers */
216 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200217 struct completion done; /* flush completion */
218};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Tejun Heo226223a2013-03-12 11:30:05 -0700220struct wq_device;
221
Tejun Heo73f53c42010-06-29 10:07:11 +0200222/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700223 * The externally visible workqueue. It relays the issued work items to
224 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
226struct workqueue_struct {
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700227 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700228 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700229 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700230 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200231
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700232 struct mutex mutex; /* protects this wq */
233 int work_color; /* WQ: current work color */
234 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800235 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700236 struct wq_flusher *first_flusher; /* WQ: first flusher */
237 struct list_head flusher_queue; /* WQ: flush waiters */
238 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200239
Tejun Heo2e109a22013-03-13 19:47:40 -0700240 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200241 struct worker *rescuer; /* I: rescue worker */
242
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700243 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700244 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700245
246#ifdef CONFIG_SYSFS
247 struct wq_device *wq_dev; /* I: for sysfs interface */
248#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700249#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200250 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700251#endif
Tejun Heob196be82012-01-10 15:11:35 -0800252 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Tejun Heoe904e6c2013-03-12 11:29:57 -0700255static struct kmem_cache *pwq_cache;
256
Tejun Heobce90382013-04-01 11:23:32 -0700257static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
258static cpumask_var_t *wq_numa_possible_cpumask;
259 /* possible CPUs of each node */
260
261static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
262
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700263static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700264static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700265
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700266static LIST_HEAD(workqueues); /* PL: list of all workqueues */
267static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700268
269/* the per-cpu worker pools */
270static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
271 cpu_worker_pools);
272
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700273static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700274
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700275/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700276static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
277
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700278/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700279static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
280
Tejun Heod320c032010-06-29 10:07:14 +0200281struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200282EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300283struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900284EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300285struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200286EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300287struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200288EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300289struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100290EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200291
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700292static int worker_thread(void *__worker);
293static void copy_workqueue_attrs(struct workqueue_attrs *to,
294 const struct workqueue_attrs *from);
295
Tejun Heo97bd2342010-10-05 10:41:14 +0200296#define CREATE_TRACE_POINTS
297#include <trace/events/workqueue.h>
298
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700299#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700300 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700301 lockdep_is_held(&wq_pool_mutex), \
302 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700303
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700304#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700305 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700306 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700307 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700308
Tejun Heo822d8402013-03-19 13:45:21 -0700309#ifdef CONFIG_LOCKDEP
310#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800311 WARN_ONCE(debug_locks && \
312 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700313 !lockdep_is_held(&(pool)->lock), \
314 "pool->manager_mutex or ->lock should be held")
315#else
316#define assert_manager_or_pool_lock(pool) do { } while (0)
317#endif
318
Tejun Heof02ae732013-03-12 11:30:03 -0700319#define for_each_cpu_worker_pool(pool, cpu) \
320 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
321 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700322 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700323
Tejun Heo49e3cf42013-03-12 11:29:58 -0700324/**
Tejun Heo17116962013-03-12 11:29:58 -0700325 * for_each_pool - iterate through all worker_pools in the system
326 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700327 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700328 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700329 * This must be called either with wq_pool_mutex held or sched RCU read
330 * locked. If the pool needs to be used beyond the locking in effect, the
331 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700332 *
333 * The if/else clause exists only for the lockdep assertion and can be
334 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700335 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700336#define for_each_pool(pool, pi) \
337 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700338 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700339 else
Tejun Heo17116962013-03-12 11:29:58 -0700340
341/**
Tejun Heo822d8402013-03-19 13:45:21 -0700342 * for_each_pool_worker - iterate through all workers of a worker_pool
343 * @worker: iteration cursor
344 * @wi: integer used for iteration
345 * @pool: worker_pool to iterate workers of
346 *
347 * This must be called with either @pool->manager_mutex or ->lock held.
348 *
349 * The if/else clause exists only for the lockdep assertion and can be
350 * ignored.
351 */
352#define for_each_pool_worker(worker, wi, pool) \
353 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
354 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
355 else
356
357/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700358 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
359 * @pwq: iteration cursor
360 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700361 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700362 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700363 * If the pwq needs to be used beyond the locking in effect, the caller is
364 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700365 *
366 * The if/else clause exists only for the lockdep assertion and can be
367 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700368 */
369#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700370 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700371 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700372 else
Tejun Heof3421792010-07-02 10:03:51 +0200373
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900374#ifdef CONFIG_DEBUG_OBJECTS_WORK
375
376static struct debug_obj_descr work_debug_descr;
377
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100378static void *work_debug_hint(void *addr)
379{
380 return ((struct work_struct *) addr)->func;
381}
382
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900383/*
384 * fixup_init is called when:
385 * - an active object is initialized
386 */
387static int work_fixup_init(void *addr, enum debug_obj_state state)
388{
389 struct work_struct *work = addr;
390
391 switch (state) {
392 case ODEBUG_STATE_ACTIVE:
393 cancel_work_sync(work);
394 debug_object_init(work, &work_debug_descr);
395 return 1;
396 default:
397 return 0;
398 }
399}
400
401/*
402 * fixup_activate is called when:
403 * - an active object is activated
404 * - an unknown object is activated (might be a statically initialized object)
405 */
406static int work_fixup_activate(void *addr, enum debug_obj_state state)
407{
408 struct work_struct *work = addr;
409
410 switch (state) {
411
412 case ODEBUG_STATE_NOTAVAILABLE:
413 /*
414 * This is not really a fixup. The work struct was
415 * statically initialized. We just make sure that it
416 * is tracked in the object tracker.
417 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200418 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900419 debug_object_init(work, &work_debug_descr);
420 debug_object_activate(work, &work_debug_descr);
421 return 0;
422 }
423 WARN_ON_ONCE(1);
424 return 0;
425
426 case ODEBUG_STATE_ACTIVE:
427 WARN_ON(1);
428
429 default:
430 return 0;
431 }
432}
433
434/*
435 * fixup_free is called when:
436 * - an active object is freed
437 */
438static int work_fixup_free(void *addr, enum debug_obj_state state)
439{
440 struct work_struct *work = addr;
441
442 switch (state) {
443 case ODEBUG_STATE_ACTIVE:
444 cancel_work_sync(work);
445 debug_object_free(work, &work_debug_descr);
446 return 1;
447 default:
448 return 0;
449 }
450}
451
452static struct debug_obj_descr work_debug_descr = {
453 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100454 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900455 .fixup_init = work_fixup_init,
456 .fixup_activate = work_fixup_activate,
457 .fixup_free = work_fixup_free,
458};
459
460static inline void debug_work_activate(struct work_struct *work)
461{
462 debug_object_activate(work, &work_debug_descr);
463}
464
465static inline void debug_work_deactivate(struct work_struct *work)
466{
467 debug_object_deactivate(work, &work_debug_descr);
468}
469
470void __init_work(struct work_struct *work, int onstack)
471{
472 if (onstack)
473 debug_object_init_on_stack(work, &work_debug_descr);
474 else
475 debug_object_init(work, &work_debug_descr);
476}
477EXPORT_SYMBOL_GPL(__init_work);
478
479void destroy_work_on_stack(struct work_struct *work)
480{
481 debug_object_free(work, &work_debug_descr);
482}
483EXPORT_SYMBOL_GPL(destroy_work_on_stack);
484
485#else
486static inline void debug_work_activate(struct work_struct *work) { }
487static inline void debug_work_deactivate(struct work_struct *work) { }
488#endif
489
Tejun Heo9daf9e62013-01-24 11:01:33 -0800490/* allocate ID and assign it to @pool */
491static int worker_pool_assign_id(struct worker_pool *pool)
492{
493 int ret;
494
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700495 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700496
Tejun Heofa1b54e2013-03-12 11:30:00 -0700497 do {
498 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
499 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700500 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700501 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800502
503 return ret;
504}
505
Tejun Heo76af4d92013-03-12 11:30:00 -0700506/**
507 * first_pwq - return the first pool_workqueue of the specified workqueue
508 * @wq: the target workqueue
509 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700510 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700511 * If the pwq needs to be used beyond the locking in effect, the caller is
512 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700513 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700514static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700515{
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700516 assert_rcu_or_wq_mutex(wq);
Tejun Heo76af4d92013-03-12 11:30:00 -0700517 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
518 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700519}
520
Tejun Heo73f53c42010-06-29 10:07:11 +0200521static unsigned int work_color_to_flags(int color)
522{
523 return color << WORK_STRUCT_COLOR_SHIFT;
524}
525
526static int get_work_color(struct work_struct *work)
527{
528 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
529 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
530}
531
532static int work_next_color(int color)
533{
534 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700535}
536
David Howells4594bf12006-12-07 11:33:26 +0000537/*
Tejun Heo112202d2013-02-13 19:29:12 -0800538 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
539 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800540 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200541 *
Tejun Heo112202d2013-02-13 19:29:12 -0800542 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
543 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700544 * work->data. These functions should only be called while the work is
545 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546 *
Tejun Heo112202d2013-02-13 19:29:12 -0800547 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800548 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800549 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800550 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700551 *
552 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
553 * canceled. While being canceled, a work item may have its PENDING set
554 * but stay off timer and worklist for arbitrarily long and nobody should
555 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000556 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557static inline void set_work_data(struct work_struct *work, unsigned long data,
558 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000559{
Tejun Heo6183c002013-03-12 11:29:57 -0700560 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000562}
David Howells365970a2006-11-22 14:54:49 +0000563
Tejun Heo112202d2013-02-13 19:29:12 -0800564static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566{
Tejun Heo112202d2013-02-13 19:29:12 -0800567 set_work_data(work, (unsigned long)pwq,
568 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200569}
570
Lai Jiangshan4468a002013-02-06 18:04:53 -0800571static void set_work_pool_and_keep_pending(struct work_struct *work,
572 int pool_id)
573{
574 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
575 WORK_STRUCT_PENDING);
576}
577
Tejun Heo7c3eed52013-01-24 11:01:33 -0800578static void set_work_pool_and_clear_pending(struct work_struct *work,
579 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000580{
Tejun Heo23657bb2012-08-13 17:08:19 -0700581 /*
582 * The following wmb is paired with the implied mb in
583 * test_and_set_bit(PENDING) and ensures all updates to @work made
584 * here are visible to and precede any updates by the next PENDING
585 * owner.
586 */
587 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800588 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589}
590
591static void clear_work_data(struct work_struct *work)
592{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800593 smp_wmb(); /* see set_work_pool_and_clear_pending() */
594 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595}
596
Tejun Heo112202d2013-02-13 19:29:12 -0800597static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598{
Tejun Heoe1201532010-07-22 14:14:25 +0200599 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200600
Tejun Heo112202d2013-02-13 19:29:12 -0800601 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200602 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
603 else
604 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605}
606
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607/**
608 * get_work_pool - return the worker_pool a given work was associated with
609 * @work: the work item of interest
610 *
611 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700612 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700613 * Pools are created and destroyed under wq_pool_mutex, and allows read
614 * access under sched-RCU read lock. As such, this function should be
615 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700616 *
617 * All fields of the returned pool are accessible as long as the above
618 * mentioned locking is in effect. If the returned pool needs to be used
619 * beyond the critical section, the caller is responsible for ensuring the
620 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800621 */
622static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200623{
Tejun Heoe1201532010-07-22 14:14:25 +0200624 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800625 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200626
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700627 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700628
Tejun Heo112202d2013-02-13 19:29:12 -0800629 if (data & WORK_STRUCT_PWQ)
630 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200632
Tejun Heo7c3eed52013-01-24 11:01:33 -0800633 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
634 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200635 return NULL;
636
Tejun Heofa1b54e2013-03-12 11:30:00 -0700637 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800638}
639
640/**
641 * get_work_pool_id - return the worker pool ID a given work is associated with
642 * @work: the work item of interest
643 *
644 * Return the worker_pool ID @work was last associated with.
645 * %WORK_OFFQ_POOL_NONE if none.
646 */
647static int get_work_pool_id(struct work_struct *work)
648{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800649 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800650
Tejun Heo112202d2013-02-13 19:29:12 -0800651 if (data & WORK_STRUCT_PWQ)
652 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800653 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
654
655 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800656}
657
Tejun Heobbb68df2012-08-03 10:30:46 -0700658static void mark_work_canceling(struct work_struct *work)
659{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800660 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700661
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662 pool_id <<= WORK_OFFQ_POOL_SHIFT;
663 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700664}
665
666static bool work_is_canceling(struct work_struct *work)
667{
668 unsigned long data = atomic_long_read(&work->data);
669
Tejun Heo112202d2013-02-13 19:29:12 -0800670 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700671}
672
David Howells365970a2006-11-22 14:54:49 +0000673/*
Tejun Heo32704762012-07-13 22:16:45 -0700674 * Policy functions. These define the policies on how the global worker
675 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800676 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000677 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200678
Tejun Heo63d95a92012-07-12 14:46:37 -0700679static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000680{
Tejun Heoe19e3972013-01-24 11:39:44 -0800681 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000682}
683
Tejun Heoe22bee72010-06-29 10:07:14 +0200684/*
685 * Need to wake up a worker? Called from anything but currently
686 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700687 *
688 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800689 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700690 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200691 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700692static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000693{
Tejun Heo63d95a92012-07-12 14:46:37 -0700694 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000695}
696
Tejun Heoe22bee72010-06-29 10:07:14 +0200697/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700698static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200699{
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200701}
702
703/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700704static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200705{
Tejun Heoe19e3972013-01-24 11:39:44 -0800706 return !list_empty(&pool->worklist) &&
707 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200708}
709
710/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700711static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200712{
Tejun Heo63d95a92012-07-12 14:46:37 -0700713 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200714}
715
716/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700717static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200718{
Tejun Heo63d95a92012-07-12 14:46:37 -0700719 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700720 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200721}
722
723/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700724static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200725{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700726 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700727 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
728 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200729
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700730 /*
731 * nr_idle and idle_list may disagree if idle rebinding is in
732 * progress. Never return %true if idle_list is empty.
733 */
734 if (list_empty(&pool->idle_list))
735 return false;
736
Tejun Heoe22bee72010-06-29 10:07:14 +0200737 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
738}
739
740/*
741 * Wake up functions.
742 */
743
Tejun Heo7e116292010-06-29 10:07:13 +0200744/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700745static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200746{
Tejun Heo63d95a92012-07-12 14:46:37 -0700747 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200748 return NULL;
749
Tejun Heo63d95a92012-07-12 14:46:37 -0700750 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200751}
752
753/**
754 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700755 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200756 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700757 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200758 *
759 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800760 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200761 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700762static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200763{
Tejun Heo63d95a92012-07-12 14:46:37 -0700764 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200765
766 if (likely(worker))
767 wake_up_process(worker->task);
768}
769
Tejun Heo4690c4a2010-06-29 10:07:10 +0200770/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200771 * wq_worker_waking_up - a worker is waking up
772 * @task: task waking up
773 * @cpu: CPU @task is waking up to
774 *
775 * This function is called during try_to_wake_up() when a worker is
776 * being awoken.
777 *
778 * CONTEXT:
779 * spin_lock_irq(rq->lock)
780 */
Tejun Heod84ff052013-03-12 11:29:59 -0700781void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200782{
783 struct worker *worker = kthread_data(task);
784
Joonsoo Kim36576002012-10-26 23:03:49 +0900785 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800786 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800787 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900788 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200789}
790
791/**
792 * wq_worker_sleeping - a worker is going to sleep
793 * @task: task going to sleep
794 * @cpu: CPU in question, must be the current CPU number
795 *
796 * This function is called during schedule() when a busy worker is
797 * going to sleep. Worker on the same cpu can be woken up by
798 * returning pointer to its task.
799 *
800 * CONTEXT:
801 * spin_lock_irq(rq->lock)
802 *
803 * RETURNS:
804 * Worker task on @cpu to wake up, %NULL if none.
805 */
Tejun Heod84ff052013-03-12 11:29:59 -0700806struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200807{
808 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800809 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200810
Tejun Heo111c2252013-01-17 17:16:24 -0800811 /*
812 * Rescuers, which may not have all the fields set up like normal
813 * workers, also reach here, let's not access anything before
814 * checking NOT_RUNNING.
815 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500816 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 return NULL;
818
Tejun Heo111c2252013-01-17 17:16:24 -0800819 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800820
Tejun Heoe22bee72010-06-29 10:07:14 +0200821 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700822 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
823 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200824
825 /*
826 * The counterpart of the following dec_and_test, implied mb,
827 * worklist not empty test sequence is in insert_work().
828 * Please read comment there.
829 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700830 * NOT_RUNNING is clear. This means that we're bound to and
831 * running on the local cpu w/ rq lock held and preemption
832 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800833 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700834 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200835 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800836 if (atomic_dec_and_test(&pool->nr_running) &&
837 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700838 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200839 return to_wakeup ? to_wakeup->task : NULL;
840}
841
842/**
843 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200844 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200845 * @flags: flags to set
846 * @wakeup: wakeup an idle worker if necessary
847 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200848 * Set @flags in @worker->flags and adjust nr_running accordingly. If
849 * nr_running becomes zero and @wakeup is %true, an idle worker is
850 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200851 *
Tejun Heocb444762010-07-02 10:03:50 +0200852 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800853 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200854 */
855static inline void worker_set_flags(struct worker *worker, unsigned int flags,
856 bool wakeup)
857{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700858 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200859
Tejun Heocb444762010-07-02 10:03:50 +0200860 WARN_ON_ONCE(worker->task != current);
861
Tejun Heoe22bee72010-06-29 10:07:14 +0200862 /*
863 * If transitioning into NOT_RUNNING, adjust nr_running and
864 * wake up an idle worker as necessary if requested by
865 * @wakeup.
866 */
867 if ((flags & WORKER_NOT_RUNNING) &&
868 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800870 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700871 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700872 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200873 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800874 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200875 }
876
Tejun Heod302f012010-06-29 10:07:13 +0200877 worker->flags |= flags;
878}
879
880/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200881 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200882 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200883 * @flags: flags to clear
884 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200885 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200886 *
Tejun Heocb444762010-07-02 10:03:50 +0200887 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800888 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200889 */
890static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
891{
Tejun Heo63d95a92012-07-12 14:46:37 -0700892 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200893 unsigned int oflags = worker->flags;
894
Tejun Heocb444762010-07-02 10:03:50 +0200895 WARN_ON_ONCE(worker->task != current);
896
Tejun Heod302f012010-06-29 10:07:13 +0200897 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200898
Tejun Heo42c025f2011-01-11 15:58:49 +0100899 /*
900 * If transitioning out of NOT_RUNNING, increment nr_running. Note
901 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
902 * of multiple flags, not a single flag.
903 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200904 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
905 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800906 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200907}
908
909/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200910 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800911 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200912 * @work: work to find worker for
913 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800914 * Find a worker which is executing @work on @pool by searching
915 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800916 * to match, its current execution should match the address of @work and
917 * its work function. This is to avoid unwanted dependency between
918 * unrelated work executions through a work item being recycled while still
919 * being executed.
920 *
921 * This is a bit tricky. A work item may be freed once its execution
922 * starts and nothing prevents the freed area from being recycled for
923 * another work item. If the same work item address ends up being reused
924 * before the original execution finishes, workqueue will identify the
925 * recycled work item as currently executing and make it wait until the
926 * current execution finishes, introducing an unwanted dependency.
927 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700928 * This function checks the work item address and work function to avoid
929 * false positives. Note that this isn't complete as one may construct a
930 * work function which can introduce dependency onto itself through a
931 * recycled work item. Well, if somebody wants to shoot oneself in the
932 * foot that badly, there's only so much we can do, and if such deadlock
933 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200934 *
935 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800936 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200937 *
938 * RETURNS:
939 * Pointer to worker which is executing @work if found, NULL
940 * otherwise.
941 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800942static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200943 struct work_struct *work)
944{
Sasha Levin42f85702012-12-17 10:01:23 -0500945 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500946
Sasha Levinb67bfe02013-02-27 17:06:00 -0800947 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800948 (unsigned long)work)
949 if (worker->current_work == work &&
950 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500951 return worker;
952
953 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200954}
955
956/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700957 * move_linked_works - move linked works to a list
958 * @work: start of series of works to be scheduled
959 * @head: target list to append @work to
960 * @nextp: out paramter for nested worklist walking
961 *
962 * Schedule linked works starting from @work to @head. Work series to
963 * be scheduled starts at @work and includes any consecutive work with
964 * WORK_STRUCT_LINKED set in its predecessor.
965 *
966 * If @nextp is not NULL, it's updated to point to the next work of
967 * the last scheduled work. This allows move_linked_works() to be
968 * nested inside outer list_for_each_entry_safe().
969 *
970 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800971 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700972 */
973static void move_linked_works(struct work_struct *work, struct list_head *head,
974 struct work_struct **nextp)
975{
976 struct work_struct *n;
977
978 /*
979 * Linked worklist will always end before the end of the list,
980 * use NULL for list head.
981 */
982 list_for_each_entry_safe_from(work, n, NULL, entry) {
983 list_move_tail(&work->entry, head);
984 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
985 break;
986 }
987
988 /*
989 * If we're already inside safe list traversal and have moved
990 * multiple works to the scheduled queue, the next position
991 * needs to be updated.
992 */
993 if (nextp)
994 *nextp = n;
995}
996
Tejun Heo8864b4e2013-03-12 11:30:04 -0700997/**
998 * get_pwq - get an extra reference on the specified pool_workqueue
999 * @pwq: pool_workqueue to get
1000 *
1001 * Obtain an extra reference on @pwq. The caller should guarantee that
1002 * @pwq has positive refcnt and be holding the matching pool->lock.
1003 */
1004static void get_pwq(struct pool_workqueue *pwq)
1005{
1006 lockdep_assert_held(&pwq->pool->lock);
1007 WARN_ON_ONCE(pwq->refcnt <= 0);
1008 pwq->refcnt++;
1009}
1010
1011/**
1012 * put_pwq - put a pool_workqueue reference
1013 * @pwq: pool_workqueue to put
1014 *
1015 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1016 * destruction. The caller should be holding the matching pool->lock.
1017 */
1018static void put_pwq(struct pool_workqueue *pwq)
1019{
1020 lockdep_assert_held(&pwq->pool->lock);
1021 if (likely(--pwq->refcnt))
1022 return;
1023 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1024 return;
1025 /*
1026 * @pwq can't be released under pool->lock, bounce to
1027 * pwq_unbound_release_workfn(). This never recurses on the same
1028 * pool->lock as this path is taken only for unbound workqueues and
1029 * the release work item is scheduled on a per-cpu workqueue. To
1030 * avoid lockdep warning, unbound pool->locks are given lockdep
1031 * subclass of 1 in get_unbound_pool().
1032 */
1033 schedule_work(&pwq->unbound_release_work);
1034}
1035
Tejun Heo112202d2013-02-13 19:29:12 -08001036static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001037{
Tejun Heo112202d2013-02-13 19:29:12 -08001038 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001039
1040 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001041 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001042 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001043 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001044}
1045
Tejun Heo112202d2013-02-13 19:29:12 -08001046static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001047{
Tejun Heo112202d2013-02-13 19:29:12 -08001048 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001049 struct work_struct, entry);
1050
Tejun Heo112202d2013-02-13 19:29:12 -08001051 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001052}
1053
Tejun Heobf4ede02012-08-03 10:30:46 -07001054/**
Tejun Heo112202d2013-02-13 19:29:12 -08001055 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1056 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001057 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001058 *
1059 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001060 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001061 *
1062 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001063 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001064 */
Tejun Heo112202d2013-02-13 19:29:12 -08001065static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001066{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001067 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001068 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001069 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001070
Tejun Heo112202d2013-02-13 19:29:12 -08001071 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001072
Tejun Heo112202d2013-02-13 19:29:12 -08001073 pwq->nr_active--;
1074 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001075 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001076 if (pwq->nr_active < pwq->max_active)
1077 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001078 }
1079
1080 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001081 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001082 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001083
1084 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001085 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001086 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001087
Tejun Heo112202d2013-02-13 19:29:12 -08001088 /* this pwq is done, clear flush_color */
1089 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001090
1091 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001092 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001093 * will handle the rest.
1094 */
Tejun Heo112202d2013-02-13 19:29:12 -08001095 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1096 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001097out_put:
1098 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001099}
1100
Tejun Heo36e227d2012-08-03 10:30:46 -07001101/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001102 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001103 * @work: work item to steal
1104 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001105 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001106 *
1107 * Try to grab PENDING bit of @work. This function can handle @work in any
1108 * stable state - idle, on timer or on worklist. Return values are
1109 *
1110 * 1 if @work was pending and we successfully stole PENDING
1111 * 0 if @work was idle and we claimed PENDING
1112 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001113 * -ENOENT if someone else is canceling @work, this state may persist
1114 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001115 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001116 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001117 * interrupted while holding PENDING and @work off queue, irq must be
1118 * disabled on entry. This, combined with delayed_work->timer being
1119 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001120 *
1121 * On successful return, >= 0, irq is disabled and the caller is
1122 * responsible for releasing it using local_irq_restore(*@flags).
1123 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001124 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001125 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001126static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1127 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001128{
Tejun Heod565ed62013-01-24 11:01:33 -08001129 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001130 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001131
Tejun Heobbb68df2012-08-03 10:30:46 -07001132 local_irq_save(*flags);
1133
Tejun Heo36e227d2012-08-03 10:30:46 -07001134 /* try to steal the timer if it exists */
1135 if (is_dwork) {
1136 struct delayed_work *dwork = to_delayed_work(work);
1137
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001138 /*
1139 * dwork->timer is irqsafe. If del_timer() fails, it's
1140 * guaranteed that the timer is not queued anywhere and not
1141 * running on the local CPU.
1142 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001143 if (likely(del_timer(&dwork->timer)))
1144 return 1;
1145 }
1146
1147 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001148 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1149 return 0;
1150
1151 /*
1152 * The queueing is in progress, or it is already queued. Try to
1153 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1154 */
Tejun Heod565ed62013-01-24 11:01:33 -08001155 pool = get_work_pool(work);
1156 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001157 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001158
Tejun Heod565ed62013-01-24 11:01:33 -08001159 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001160 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001161 * work->data is guaranteed to point to pwq only while the work
1162 * item is queued on pwq->wq, and both updating work->data to point
1163 * to pwq on queueing and to pool on dequeueing are done under
1164 * pwq->pool->lock. This in turn guarantees that, if work->data
1165 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001166 * item is currently queued on that pool.
1167 */
Tejun Heo112202d2013-02-13 19:29:12 -08001168 pwq = get_work_pwq(work);
1169 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001170 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001171
Tejun Heo16062832013-02-06 18:04:53 -08001172 /*
1173 * A delayed work item cannot be grabbed directly because
1174 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001175 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001176 * management later on and cause stall. Make sure the work
1177 * item is activated before grabbing.
1178 */
1179 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001180 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001181
Tejun Heo16062832013-02-06 18:04:53 -08001182 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001183 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001184
Tejun Heo112202d2013-02-13 19:29:12 -08001185 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001186 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001187
Tejun Heo16062832013-02-06 18:04:53 -08001188 spin_unlock(&pool->lock);
1189 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001190 }
Tejun Heod565ed62013-01-24 11:01:33 -08001191 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001192fail:
1193 local_irq_restore(*flags);
1194 if (work_is_canceling(work))
1195 return -ENOENT;
1196 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001197 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001198}
1199
1200/**
Tejun Heo706026c2013-01-24 11:01:34 -08001201 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001202 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001203 * @work: work to insert
1204 * @head: insertion point
1205 * @extra_flags: extra WORK_STRUCT_* flags to set
1206 *
Tejun Heo112202d2013-02-13 19:29:12 -08001207 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001208 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001209 *
1210 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001211 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001212 */
Tejun Heo112202d2013-02-13 19:29:12 -08001213static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1214 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001215{
Tejun Heo112202d2013-02-13 19:29:12 -08001216 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001217
Tejun Heo4690c4a2010-06-29 10:07:10 +02001218 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001219 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001220 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001221 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001222
1223 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001224 * Ensure either wq_worker_sleeping() sees the above
1225 * list_add_tail() or we see zero nr_running to avoid workers lying
1226 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001227 */
1228 smp_mb();
1229
Tejun Heo63d95a92012-07-12 14:46:37 -07001230 if (__need_more_worker(pool))
1231 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001232}
1233
Tejun Heoc8efcc22010-12-20 19:32:04 +01001234/*
1235 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001236 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001237 */
1238static bool is_chained_work(struct workqueue_struct *wq)
1239{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001240 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001241
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001242 worker = current_wq_worker();
1243 /*
1244 * Return %true iff I'm a worker execuing a work item on @wq. If
1245 * I'm @worker, it's safe to dereference it without locking.
1246 */
Tejun Heo112202d2013-02-13 19:29:12 -08001247 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001248}
1249
Tejun Heod84ff052013-03-12 11:29:59 -07001250static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct work_struct *work)
1252{
Tejun Heo112202d2013-02-13 19:29:12 -08001253 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001254 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001255 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001256 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001257 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001258
1259 /*
1260 * While a work item is PENDING && off queue, a task trying to
1261 * steal the PENDING will busy-loop waiting for it to either get
1262 * queued or lose PENDING. Grabbing PENDING and queueing should
1263 * happen with IRQ disabled.
1264 */
1265 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001267 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001268
Tejun Heoc8efcc22010-12-20 19:32:04 +01001269 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001270 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001271 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001272 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001273retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001274 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001275 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001276 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001277 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001278 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001279 } else {
1280 pwq = first_pwq(wq);
1281 }
Tejun Heodbf25762012-08-20 14:51:23 -07001282
Tejun Heoc9178082013-03-12 11:30:04 -07001283 /*
1284 * If @work was previously on a different pool, it might still be
1285 * running there, in which case the work needs to be queued on that
1286 * pool to guarantee non-reentrancy.
1287 */
1288 last_pool = get_work_pool(work);
1289 if (last_pool && last_pool != pwq->pool) {
1290 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001291
Tejun Heoc9178082013-03-12 11:30:04 -07001292 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001293
Tejun Heoc9178082013-03-12 11:30:04 -07001294 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001295
Tejun Heoc9178082013-03-12 11:30:04 -07001296 if (worker && worker->current_pwq->wq == wq) {
1297 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001298 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001299 /* meh... not running there, queue here */
1300 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001301 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001302 }
Tejun Heof3421792010-07-02 10:03:51 +02001303 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001304 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001305 }
1306
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001307 /*
1308 * pwq is determined and locked. For unbound pools, we could have
1309 * raced with pwq release and it could already be dead. If its
1310 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1311 * without another pwq replacing it as the first pwq or while a
1312 * work item is executing on it, so the retying is guaranteed to
1313 * make forward-progress.
1314 */
1315 if (unlikely(!pwq->refcnt)) {
1316 if (wq->flags & WQ_UNBOUND) {
1317 spin_unlock(&pwq->pool->lock);
1318 cpu_relax();
1319 goto retry;
1320 }
1321 /* oops */
1322 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1323 wq->name, cpu);
1324 }
1325
Tejun Heo112202d2013-02-13 19:29:12 -08001326 /* pwq determined, queue */
1327 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001328
Dan Carpenterf5b25522012-04-13 22:06:58 +03001329 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001330 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001331 return;
1332 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001333
Tejun Heo112202d2013-02-13 19:29:12 -08001334 pwq->nr_in_flight[pwq->work_color]++;
1335 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001336
Tejun Heo112202d2013-02-13 19:29:12 -08001337 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001338 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001339 pwq->nr_active++;
1340 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001341 } else {
1342 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001343 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001344 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001345
Tejun Heo112202d2013-02-13 19:29:12 -08001346 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001347
Tejun Heo112202d2013-02-13 19:29:12 -08001348 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001351/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001352 * queue_work_on - queue work on specific cpu
1353 * @cpu: CPU number to execute work on
1354 * @wq: workqueue to use
1355 * @work: work to queue
1356 *
Tejun Heod4283e92012-08-03 10:30:44 -07001357 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001358 *
1359 * We queue the work to a specific CPU, the caller must ensure it
1360 * can't go away.
1361 */
Tejun Heod4283e92012-08-03 10:30:44 -07001362bool queue_work_on(int cpu, struct workqueue_struct *wq,
1363 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001364{
Tejun Heod4283e92012-08-03 10:30:44 -07001365 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001366 unsigned long flags;
1367
1368 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001369
Tejun Heo22df02b2010-06-29 10:07:10 +02001370 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001371 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001372 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001373 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001374
1375 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001376 return ret;
1377}
1378EXPORT_SYMBOL_GPL(queue_work_on);
1379
Tejun Heod8e794d2012-08-03 10:30:45 -07001380void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
David Howells52bad642006-11-22 14:54:01 +00001382 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001384 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001385 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001387EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001389static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1390 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001392 struct timer_list *timer = &dwork->timer;
1393 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001395 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1396 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001397 WARN_ON_ONCE(timer_pending(timer));
1398 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001399
Tejun Heo8852aac2012-12-01 16:23:42 -08001400 /*
1401 * If @delay is 0, queue @dwork->work immediately. This is for
1402 * both optimization and correctness. The earliest @timer can
1403 * expire is on the closest next tick and delayed_work users depend
1404 * on that there's no such delay when @delay is 0.
1405 */
1406 if (!delay) {
1407 __queue_work(cpu, wq, &dwork->work);
1408 return;
1409 }
1410
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001411 timer_stats_timer_set_start_info(&dwork->timer);
1412
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001413 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001414 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001415 timer->expires = jiffies + delay;
1416
1417 if (unlikely(cpu != WORK_CPU_UNBOUND))
1418 add_timer_on(timer, cpu);
1419 else
1420 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001423/**
1424 * queue_delayed_work_on - queue work on specific CPU after delay
1425 * @cpu: CPU number to execute work on
1426 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001427 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001428 * @delay: number of jiffies to wait before queueing
1429 *
Tejun Heo715f1302012-08-03 10:30:46 -07001430 * Returns %false if @work was already on a queue, %true otherwise. If
1431 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1432 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001433 */
Tejun Heod4283e92012-08-03 10:30:44 -07001434bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1435 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001436{
David Howells52bad642006-11-22 14:54:01 +00001437 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001438 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001439 unsigned long flags;
1440
1441 /* read the comment in __queue_work() */
1442 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001443
Tejun Heo22df02b2010-06-29 10:07:10 +02001444 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001445 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001446 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001447 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001448
1449 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001450 return ret;
1451}
Dave Jonesae90dd52006-06-30 01:40:45 -04001452EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Tejun Heoc8e55f32010-06-29 10:07:12 +02001454/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001455 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1456 * @cpu: CPU number to execute work on
1457 * @wq: workqueue to use
1458 * @dwork: work to queue
1459 * @delay: number of jiffies to wait before queueing
1460 *
1461 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1462 * modify @dwork's timer so that it expires after @delay. If @delay is
1463 * zero, @work is guaranteed to be scheduled immediately regardless of its
1464 * current state.
1465 *
1466 * Returns %false if @dwork was idle and queued, %true if @dwork was
1467 * pending and its timer was modified.
1468 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001469 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001470 * See try_to_grab_pending() for details.
1471 */
1472bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1473 struct delayed_work *dwork, unsigned long delay)
1474{
1475 unsigned long flags;
1476 int ret;
1477
1478 do {
1479 ret = try_to_grab_pending(&dwork->work, true, &flags);
1480 } while (unlikely(ret == -EAGAIN));
1481
1482 if (likely(ret >= 0)) {
1483 __queue_delayed_work(cpu, wq, dwork, delay);
1484 local_irq_restore(flags);
1485 }
1486
1487 /* -ENOENT from try_to_grab_pending() becomes %true */
1488 return ret;
1489}
1490EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1491
1492/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001493 * worker_enter_idle - enter idle state
1494 * @worker: worker which is entering idle state
1495 *
1496 * @worker is entering idle state. Update stats and idle timer if
1497 * necessary.
1498 *
1499 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001500 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001501 */
1502static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001504 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Tejun Heo6183c002013-03-12 11:29:57 -07001506 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1507 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1508 (worker->hentry.next || worker->hentry.pprev)))
1509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Tejun Heocb444762010-07-02 10:03:50 +02001511 /* can't use worker_set_flags(), also called from start_worker() */
1512 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001513 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001514 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001515
Tejun Heoc8e55f32010-06-29 10:07:12 +02001516 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001517 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001518
Tejun Heo628c78e2012-07-17 12:39:27 -07001519 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1520 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001521
Tejun Heo544ecf32012-05-14 15:04:50 -07001522 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001523 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001524 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001525 * nr_running, the warning may trigger spuriously. Check iff
1526 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001527 */
Tejun Heo24647572013-01-24 11:01:33 -08001528 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001529 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001530 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Tejun Heoc8e55f32010-06-29 10:07:12 +02001533/**
1534 * worker_leave_idle - leave idle state
1535 * @worker: worker which is leaving idle state
1536 *
1537 * @worker is leaving idle state. Update stats.
1538 *
1539 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001540 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001541 */
1542static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001544 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Tejun Heo6183c002013-03-12 11:29:57 -07001546 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1547 return;
Tejun Heod302f012010-06-29 10:07:13 +02001548 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001549 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001550 list_del_init(&worker->entry);
1551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Tejun Heoe22bee72010-06-29 10:07:14 +02001553/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001554 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1555 * @pool: target worker_pool
1556 *
1557 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001558 *
1559 * Works which are scheduled while the cpu is online must at least be
1560 * scheduled to a worker which is bound to the cpu so that if they are
1561 * flushed from cpu callbacks while cpu is going down, they are
1562 * guaranteed to execute on the cpu.
1563 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001564 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 * themselves to the target cpu and may race with cpu going down or
1566 * coming online. kthread_bind() can't be used because it may put the
1567 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001568 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 * [dis]associated in the meantime.
1570 *
Tejun Heo706026c2013-01-24 11:01:34 -08001571 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001572 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001573 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1574 * enters idle state or fetches works without dropping lock, it can
1575 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 *
1577 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001578 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 * held.
1580 *
1581 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001582 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 * bound), %false if offline.
1584 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001585static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001586__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001587{
Tejun Heoe22bee72010-06-29 10:07:14 +02001588 while (true) {
1589 /*
1590 * The following call may fail, succeed or succeed
1591 * without actually migrating the task to the cpu if
1592 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001593 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 */
Tejun Heo24647572013-01-24 11:01:33 -08001595 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001596 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001597
Tejun Heod565ed62013-01-24 11:01:33 -08001598 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001599 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001601 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001602 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001603 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001604 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001605
Tejun Heo5035b202011-04-29 18:08:37 +02001606 /*
1607 * We've raced with CPU hot[un]plug. Give it a breather
1608 * and retry migration. cond_resched() is required here;
1609 * otherwise, we might deadlock against cpu_stop trying to
1610 * bring down the CPU on non-preemptive kernel.
1611 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001613 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 }
1615}
1616
Tejun Heoc34056a2010-06-29 10:07:11 +02001617static struct worker *alloc_worker(void)
1618{
1619 struct worker *worker;
1620
1621 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001622 if (worker) {
1623 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001624 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 /* on creation a worker is in !idle && prep state */
1626 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001627 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001628 return worker;
1629}
1630
1631/**
1632 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001633 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001634 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001635 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001636 * can be started by calling start_worker() or destroyed using
1637 * destroy_worker().
1638 *
1639 * CONTEXT:
1640 * Might sleep. Does GFP_KERNEL allocations.
1641 *
1642 * RETURNS:
1643 * Pointer to the newly created worker.
1644 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001645static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001646{
Tejun Heoc34056a2010-06-29 10:07:11 +02001647 struct worker *worker = NULL;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001648 int node = pool->cpu >= 0 ? cpu_to_node(pool->cpu) : NUMA_NO_NODE;
Tejun Heof3421792010-07-02 10:03:51 +02001649 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001650 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001651
Tejun Heocd549682013-03-13 19:47:39 -07001652 lockdep_assert_held(&pool->manager_mutex);
1653
Tejun Heo822d8402013-03-19 13:45:21 -07001654 /*
1655 * ID is needed to determine kthread name. Allocate ID first
1656 * without installing the pointer.
1657 */
1658 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001659 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001660
1661 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1662
Tejun Heod565ed62013-01-24 11:01:33 -08001663 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001664 idr_preload_end();
1665 if (id < 0)
1666 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001667
1668 worker = alloc_worker();
1669 if (!worker)
1670 goto fail;
1671
Tejun Heobd7bdd42012-07-12 14:46:37 -07001672 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001673 worker->id = id;
1674
Tejun Heo29c91e92013-03-12 11:30:03 -07001675 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001676 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1677 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001678 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001679 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1680
1681 worker->task = kthread_create_on_node(worker_thread, worker, node,
1682 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001683 if (IS_ERR(worker->task))
1684 goto fail;
1685
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001686 /*
1687 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1688 * online CPUs. It'll be re-applied when any of the CPUs come up.
1689 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001690 set_user_nice(worker->task, pool->attrs->nice);
1691 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001692
Tejun Heo14a40ff2013-03-19 13:45:20 -07001693 /* prevent userland from meddling with cpumask of workqueue workers */
1694 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001695
1696 /*
1697 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1698 * remains stable across this function. See the comments above the
1699 * flag definition for details.
1700 */
1701 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001702 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001703
Tejun Heo822d8402013-03-19 13:45:21 -07001704 /* successful, commit the pointer to idr */
1705 spin_lock_irq(&pool->lock);
1706 idr_replace(&pool->worker_idr, worker, worker->id);
1707 spin_unlock_irq(&pool->lock);
1708
Tejun Heoc34056a2010-06-29 10:07:11 +02001709 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001710
Tejun Heoc34056a2010-06-29 10:07:11 +02001711fail:
1712 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001713 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001714 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001715 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001716 }
1717 kfree(worker);
1718 return NULL;
1719}
1720
1721/**
1722 * start_worker - start a newly created worker
1723 * @worker: worker to start
1724 *
Tejun Heo706026c2013-01-24 11:01:34 -08001725 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001726 *
1727 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001728 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 */
1730static void start_worker(struct worker *worker)
1731{
Tejun Heocb444762010-07-02 10:03:50 +02001732 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001733 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001734 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001735 wake_up_process(worker->task);
1736}
1737
1738/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001739 * create_and_start_worker - create and start a worker for a pool
1740 * @pool: the target pool
1741 *
Tejun Heocd549682013-03-13 19:47:39 -07001742 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001743 */
1744static int create_and_start_worker(struct worker_pool *pool)
1745{
1746 struct worker *worker;
1747
Tejun Heocd549682013-03-13 19:47:39 -07001748 mutex_lock(&pool->manager_mutex);
1749
Tejun Heoebf44d12013-03-13 19:47:39 -07001750 worker = create_worker(pool);
1751 if (worker) {
1752 spin_lock_irq(&pool->lock);
1753 start_worker(worker);
1754 spin_unlock_irq(&pool->lock);
1755 }
1756
Tejun Heocd549682013-03-13 19:47:39 -07001757 mutex_unlock(&pool->manager_mutex);
1758
Tejun Heoebf44d12013-03-13 19:47:39 -07001759 return worker ? 0 : -ENOMEM;
1760}
1761
1762/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 * destroy_worker - destroy a workqueue worker
1764 * @worker: worker to be destroyed
1765 *
Tejun Heo706026c2013-01-24 11:01:34 -08001766 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001767 *
1768 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001769 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001770 */
1771static void destroy_worker(struct worker *worker)
1772{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001773 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001774
Tejun Heocd549682013-03-13 19:47:39 -07001775 lockdep_assert_held(&pool->manager_mutex);
1776 lockdep_assert_held(&pool->lock);
1777
Tejun Heoc34056a2010-06-29 10:07:11 +02001778 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001779 if (WARN_ON(worker->current_work) ||
1780 WARN_ON(!list_empty(&worker->scheduled)))
1781 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001782
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001784 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001785 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001786 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001787
1788 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001789 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001790
Tejun Heo822d8402013-03-19 13:45:21 -07001791 idr_remove(&pool->worker_idr, worker->id);
1792
Tejun Heod565ed62013-01-24 11:01:33 -08001793 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001794
Tejun Heoc34056a2010-06-29 10:07:11 +02001795 kthread_stop(worker->task);
1796 kfree(worker);
1797
Tejun Heod565ed62013-01-24 11:01:33 -08001798 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001799}
1800
Tejun Heo63d95a92012-07-12 14:46:37 -07001801static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001802{
Tejun Heo63d95a92012-07-12 14:46:37 -07001803 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001804
Tejun Heod565ed62013-01-24 11:01:33 -08001805 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001806
Tejun Heo63d95a92012-07-12 14:46:37 -07001807 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001808 struct worker *worker;
1809 unsigned long expires;
1810
1811 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001812 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001813 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1814
1815 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001816 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001817 else {
1818 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001819 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001820 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001821 }
1822 }
1823
Tejun Heod565ed62013-01-24 11:01:33 -08001824 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001825}
1826
Tejun Heo493a1722013-03-12 11:29:59 -07001827static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001828{
Tejun Heo112202d2013-02-13 19:29:12 -08001829 struct pool_workqueue *pwq = get_work_pwq(work);
1830 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001831
Tejun Heo2e109a22013-03-13 19:47:40 -07001832 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001833
Tejun Heo493008a2013-03-12 11:30:03 -07001834 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001835 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001836
1837 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001838 if (list_empty(&pwq->mayday_node)) {
1839 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001840 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001841 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001842}
1843
Tejun Heo706026c2013-01-24 11:01:34 -08001844static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001845{
Tejun Heo63d95a92012-07-12 14:46:37 -07001846 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001847 struct work_struct *work;
1848
Tejun Heo2e109a22013-03-13 19:47:40 -07001849 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001850 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001851
Tejun Heo63d95a92012-07-12 14:46:37 -07001852 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001853 /*
1854 * We've been trying to create a new worker but
1855 * haven't been successful. We might be hitting an
1856 * allocation deadlock. Send distress signals to
1857 * rescuers.
1858 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001859 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001860 send_mayday(work);
1861 }
1862
Tejun Heo493a1722013-03-12 11:29:59 -07001863 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001864 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001865
Tejun Heo63d95a92012-07-12 14:46:37 -07001866 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001867}
1868
1869/**
1870 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001871 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001872 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001873 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001874 * have at least one idle worker on return from this function. If
1875 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001876 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001877 * possible allocation deadlock.
1878 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001879 * On return, need_to_create_worker() is guaranteed to be %false and
1880 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 *
1882 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001883 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001884 * multiple times. Does GFP_KERNEL allocations. Called only from
1885 * manager.
1886 *
1887 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001888 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001889 * otherwise.
1890 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001891static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001892__releases(&pool->lock)
1893__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001894{
Tejun Heo63d95a92012-07-12 14:46:37 -07001895 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001896 return false;
1897restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001898 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001899
Tejun Heoe22bee72010-06-29 10:07:14 +02001900 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001901 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902
1903 while (true) {
1904 struct worker *worker;
1905
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001906 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001907 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001909 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001910 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001911 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1912 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001913 return true;
1914 }
1915
Tejun Heo63d95a92012-07-12 14:46:37 -07001916 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001917 break;
1918
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 __set_current_state(TASK_INTERRUPTIBLE);
1920 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001921
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 break;
1924 }
1925
Tejun Heo63d95a92012-07-12 14:46:37 -07001926 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001927 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001928 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 goto restart;
1930 return true;
1931}
1932
1933/**
1934 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001935 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001937 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 * IDLE_WORKER_TIMEOUT.
1939 *
1940 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001941 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 * multiple times. Called only from manager.
1943 *
1944 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001945 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 * otherwise.
1947 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001948static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001949{
1950 bool ret = false;
1951
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 struct worker *worker;
1954 unsigned long expires;
1955
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1958
1959 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 break;
1962 }
1963
1964 destroy_worker(worker);
1965 ret = true;
1966 }
1967
1968 return ret;
1969}
1970
1971/**
1972 * manage_workers - manage worker pool
1973 * @worker: self
1974 *
Tejun Heo706026c2013-01-24 11:01:34 -08001975 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001977 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 *
1979 * The caller can safely start processing works on false return. On
1980 * true return, it's guaranteed that need_to_create_worker() is false
1981 * and may_start_working() is true.
1982 *
1983 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001984 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 * multiple times. Does GFP_KERNEL allocations.
1986 *
1987 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001988 * spin_lock_irq(pool->lock) which may be released and regrabbed
1989 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 */
1991static bool manage_workers(struct worker *worker)
1992{
Tejun Heo63d95a92012-07-12 14:46:37 -07001993 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 bool ret = false;
1995
Tejun Heobc3a1af2013-03-13 19:47:39 -07001996 /*
1997 * Managership is governed by two mutexes - manager_arb and
1998 * manager_mutex. manager_arb handles arbitration of manager role.
1999 * Anyone who successfully grabs manager_arb wins the arbitration
2000 * and becomes the manager. mutex_trylock() on pool->manager_arb
2001 * failure while holding pool->lock reliably indicates that someone
2002 * else is managing the pool and the worker which failed trylock
2003 * can proceed to executing work items. This means that anyone
2004 * grabbing manager_arb is responsible for actually performing
2005 * manager duties. If manager_arb is grabbed and released without
2006 * actual management, the pool may stall indefinitely.
2007 *
2008 * manager_mutex is used for exclusion of actual management
2009 * operations. The holder of manager_mutex can be sure that none
2010 * of management operations, including creation and destruction of
2011 * workers, won't take place until the mutex is released. Because
2012 * manager_mutex doesn't interfere with manager role arbitration,
2013 * it is guaranteed that the pool's management, while may be
2014 * delayed, won't be disturbed by someone else grabbing
2015 * manager_mutex.
2016 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002017 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 return ret;
2019
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002020 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002021 * With manager arbitration won, manager_mutex would be free in
2022 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002023 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002024 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002025 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002026 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002027 ret = true;
2028 }
2029
Tejun Heo11ebea52012-07-12 14:46:37 -07002030 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002031
2032 /*
2033 * Destroy and then create so that may_start_working() is true
2034 * on return.
2035 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002036 ret |= maybe_destroy_workers(pool);
2037 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002038
Tejun Heobc3a1af2013-03-13 19:47:39 -07002039 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002040 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 return ret;
2042}
2043
Tejun Heoa62428c2010-06-29 10:07:10 +02002044/**
2045 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002046 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002047 * @work: work to process
2048 *
2049 * Process @work. This function contains all the logics necessary to
2050 * process a single work including synchronization against and
2051 * interaction with other workers on the same cpu, queueing and
2052 * flushing. As long as context requirement is met, any worker can
2053 * call this function to process a work.
2054 *
2055 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002056 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002057 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002058static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002059__releases(&pool->lock)
2060__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002061{
Tejun Heo112202d2013-02-13 19:29:12 -08002062 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002063 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002064 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002065 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002066 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002067#ifdef CONFIG_LOCKDEP
2068 /*
2069 * It is permissible to free the struct work_struct from
2070 * inside the function that is called from it, this we need to
2071 * take into account for lockdep too. To avoid bogus "held
2072 * lock freed" warnings as well as problems when looking into
2073 * work->lockdep_map, make a copy and use that here.
2074 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002075 struct lockdep_map lockdep_map;
2076
2077 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002078#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002079 /*
2080 * Ensure we're on the correct CPU. DISASSOCIATED test is
2081 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002082 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002083 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002084 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002085 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002086 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002087
Tejun Heo7e116292010-06-29 10:07:13 +02002088 /*
2089 * A single work shouldn't be executed concurrently by
2090 * multiple workers on a single cpu. Check whether anyone is
2091 * already processing the work. If so, defer the work to the
2092 * currently executing one.
2093 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002094 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002095 if (unlikely(collision)) {
2096 move_linked_works(work, &collision->scheduled, NULL);
2097 return;
2098 }
2099
Tejun Heo8930cab2012-08-03 10:30:45 -07002100 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002101 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002102 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002103 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002104 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002105 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002106 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002107
Tejun Heoa62428c2010-06-29 10:07:10 +02002108 list_del_init(&work->entry);
2109
Tejun Heo649027d2010-06-29 10:07:14 +02002110 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002111 * CPU intensive works don't participate in concurrency
2112 * management. They're the scheduler's responsibility.
2113 */
2114 if (unlikely(cpu_intensive))
2115 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2116
Tejun Heo974271c2012-07-12 14:46:37 -07002117 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002118 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002119 * executed ASAP. Wake up another worker if necessary.
2120 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002121 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2122 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002123
Tejun Heo8930cab2012-08-03 10:30:45 -07002124 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002125 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002126 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002127 * PENDING and queued state changes happen together while IRQ is
2128 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002129 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002130 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002131
Tejun Heod565ed62013-01-24 11:01:33 -08002132 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002133
Tejun Heo112202d2013-02-13 19:29:12 -08002134 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002136 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002137 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002138 /*
2139 * While we must be careful to not use "work" after this, the trace
2140 * point will only record its address.
2141 */
2142 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002143 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002144 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002145
2146 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002147 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2148 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002149 current->comm, preempt_count(), task_pid_nr(current),
2150 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002151 debug_show_held_locks(current);
2152 dump_stack();
2153 }
2154
Tejun Heod565ed62013-01-24 11:01:33 -08002155 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002156
Tejun Heofb0e7be2010-06-29 10:07:15 +02002157 /* clear cpu intensive status */
2158 if (unlikely(cpu_intensive))
2159 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2160
Tejun Heoa62428c2010-06-29 10:07:10 +02002161 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002162 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002163 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002164 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002165 worker->current_pwq = NULL;
2166 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002167}
2168
Tejun Heoaffee4b2010-06-29 10:07:12 +02002169/**
2170 * process_scheduled_works - process scheduled works
2171 * @worker: self
2172 *
2173 * Process all scheduled works. Please note that the scheduled list
2174 * may change while processing a work, so this function repeatedly
2175 * fetches a work from the top and executes it.
2176 *
2177 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002178 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002179 * multiple times.
2180 */
2181static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002183 while (!list_empty(&worker->scheduled)) {
2184 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002186 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Tejun Heo4690c4a2010-06-29 10:07:10 +02002190/**
2191 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002192 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002193 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002194 * The worker thread function. All workers belong to a worker_pool -
2195 * either a per-cpu one or dynamic unbound one. These workers process all
2196 * work items regardless of their specific target workqueue. The only
2197 * exception is work items which belong to workqueues with a rescuer which
2198 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002199 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002200static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Tejun Heoc34056a2010-06-29 10:07:11 +02002202 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002203 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Tejun Heoe22bee72010-06-29 10:07:14 +02002205 /* tell the scheduler that this is a workqueue worker */
2206 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002207woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002208 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Tejun Heoa9ab7752013-03-19 13:45:21 -07002210 /* am I supposed to die? */
2211 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002212 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002213 WARN_ON_ONCE(!list_empty(&worker->entry));
2214 worker->task->flags &= ~PF_WQ_WORKER;
2215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217
Tejun Heoc8e55f32010-06-29 10:07:12 +02002218 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002219recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002220 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002221 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002222 goto sleep;
2223
2224 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002225 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002226 goto recheck;
2227
Tejun Heoc8e55f32010-06-29 10:07:12 +02002228 /*
2229 * ->scheduled list can only be filled while a worker is
2230 * preparing to process a work or actually processing it.
2231 * Make sure nobody diddled with it while I was sleeping.
2232 */
Tejun Heo6183c002013-03-12 11:29:57 -07002233 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002234
Tejun Heoe22bee72010-06-29 10:07:14 +02002235 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002236 * Finish PREP stage. We're guaranteed to have at least one idle
2237 * worker or that someone else has already assumed the manager
2238 * role. This is where @worker starts participating in concurrency
2239 * management if applicable and concurrency management is restored
2240 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002241 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002242 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002243
2244 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002245 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002246 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002247 struct work_struct, entry);
2248
2249 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2250 /* optimization path, not strictly necessary */
2251 process_one_work(worker, work);
2252 if (unlikely(!list_empty(&worker->scheduled)))
2253 process_scheduled_works(worker);
2254 } else {
2255 move_linked_works(work, &worker->scheduled, NULL);
2256 process_scheduled_works(worker);
2257 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002258 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002259
Tejun Heoe22bee72010-06-29 10:07:14 +02002260 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002261sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002262 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002263 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002264
Tejun Heoc8e55f32010-06-29 10:07:12 +02002265 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002266 * pool->lock is held and there's no work to process and no need to
2267 * manage, sleep. Workers are woken up only while holding
2268 * pool->lock or from local cpu, so setting the current state
2269 * before releasing pool->lock is enough to prevent losing any
2270 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002271 */
2272 worker_enter_idle(worker);
2273 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002274 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002275 schedule();
2276 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Tejun Heoe22bee72010-06-29 10:07:14 +02002279/**
2280 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002281 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002282 *
2283 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002284 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002285 *
Tejun Heo706026c2013-01-24 11:01:34 -08002286 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002287 * worker which uses GFP_KERNEL allocation which has slight chance of
2288 * developing into deadlock if some works currently on the same queue
2289 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2290 * the problem rescuer solves.
2291 *
Tejun Heo706026c2013-01-24 11:01:34 -08002292 * When such condition is possible, the pool summons rescuers of all
2293 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002294 * those works so that forward progress can be guaranteed.
2295 *
2296 * This should happen rarely.
2297 */
Tejun Heo111c2252013-01-17 17:16:24 -08002298static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002299{
Tejun Heo111c2252013-01-17 17:16:24 -08002300 struct worker *rescuer = __rescuer;
2301 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002302 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002303
2304 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002305
2306 /*
2307 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2308 * doesn't participate in concurrency management.
2309 */
2310 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002311repeat:
2312 set_current_state(TASK_INTERRUPTIBLE);
2313
Mike Galbraith412d32e2012-11-28 07:17:18 +01002314 if (kthread_should_stop()) {
2315 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002316 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002317 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002318 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002319
Tejun Heo493a1722013-03-12 11:29:59 -07002320 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002321 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002322
2323 while (!list_empty(&wq->maydays)) {
2324 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2325 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002326 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002327 struct work_struct *work, *n;
2328
2329 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002330 list_del_init(&pwq->mayday_node);
2331
Tejun Heo2e109a22013-03-13 19:47:40 -07002332 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002333
2334 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002335 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002336 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002337
2338 /*
2339 * Slurp in all works issued via this workqueue and
2340 * process'em.
2341 */
Tejun Heo6183c002013-03-12 11:29:57 -07002342 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002343 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002344 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002345 move_linked_works(work, scheduled, &n);
2346
2347 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002348
2349 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002350 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002351 * regular worker; otherwise, we end up with 0 concurrency
2352 * and stalling the execution.
2353 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002354 if (keep_working(pool))
2355 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002356
Lai Jiangshanb3104102013-02-19 12:17:02 -08002357 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002358 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002359 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002360 }
2361
Tejun Heo2e109a22013-03-13 19:47:40 -07002362 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002363
Tejun Heo111c2252013-01-17 17:16:24 -08002364 /* rescuers should never participate in concurrency management */
2365 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002366 schedule();
2367 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002370struct wq_barrier {
2371 struct work_struct work;
2372 struct completion done;
2373};
2374
2375static void wq_barrier_func(struct work_struct *work)
2376{
2377 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2378 complete(&barr->done);
2379}
2380
Tejun Heo4690c4a2010-06-29 10:07:10 +02002381/**
2382 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002383 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002384 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002385 * @target: target work to attach @barr to
2386 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002387 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002388 * @barr is linked to @target such that @barr is completed only after
2389 * @target finishes execution. Please note that the ordering
2390 * guarantee is observed only with respect to @target and on the local
2391 * cpu.
2392 *
2393 * Currently, a queued barrier can't be canceled. This is because
2394 * try_to_grab_pending() can't determine whether the work to be
2395 * grabbed is at the head of the queue and thus can't clear LINKED
2396 * flag of the previous work while there must be a valid next work
2397 * after a work with LINKED flag set.
2398 *
2399 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002400 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002401 *
2402 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002403 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002404 */
Tejun Heo112202d2013-02-13 19:29:12 -08002405static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002406 struct wq_barrier *barr,
2407 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002408{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002409 struct list_head *head;
2410 unsigned int linked = 0;
2411
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002412 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002413 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002414 * as we know for sure that this will not trigger any of the
2415 * checks and call back into the fixup functions where we
2416 * might deadlock.
2417 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002418 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002419 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002420 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002421
Tejun Heoaffee4b2010-06-29 10:07:12 +02002422 /*
2423 * If @target is currently being executed, schedule the
2424 * barrier to the worker; otherwise, put it after @target.
2425 */
2426 if (worker)
2427 head = worker->scheduled.next;
2428 else {
2429 unsigned long *bits = work_data_bits(target);
2430
2431 head = target->entry.next;
2432 /* there can already be other linked works, inherit and set */
2433 linked = *bits & WORK_STRUCT_LINKED;
2434 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2435 }
2436
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002437 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002438 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002439 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002440}
2441
Tejun Heo73f53c42010-06-29 10:07:11 +02002442/**
Tejun Heo112202d2013-02-13 19:29:12 -08002443 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002444 * @wq: workqueue being flushed
2445 * @flush_color: new flush color, < 0 for no-op
2446 * @work_color: new work color, < 0 for no-op
2447 *
Tejun Heo112202d2013-02-13 19:29:12 -08002448 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002449 *
Tejun Heo112202d2013-02-13 19:29:12 -08002450 * If @flush_color is non-negative, flush_color on all pwqs should be
2451 * -1. If no pwq has in-flight commands at the specified color, all
2452 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2453 * has in flight commands, its pwq->flush_color is set to
2454 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002455 * wakeup logic is armed and %true is returned.
2456 *
2457 * The caller should have initialized @wq->first_flusher prior to
2458 * calling this function with non-negative @flush_color. If
2459 * @flush_color is negative, no flush color update is done and %false
2460 * is returned.
2461 *
Tejun Heo112202d2013-02-13 19:29:12 -08002462 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002463 * work_color which is previous to @work_color and all will be
2464 * advanced to @work_color.
2465 *
2466 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002467 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002468 *
2469 * RETURNS:
2470 * %true if @flush_color >= 0 and there's something to flush. %false
2471 * otherwise.
2472 */
Tejun Heo112202d2013-02-13 19:29:12 -08002473static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002474 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Tejun Heo73f53c42010-06-29 10:07:11 +02002476 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002477 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002478
Tejun Heo73f53c42010-06-29 10:07:11 +02002479 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002480 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002481 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002482 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002483
Tejun Heo49e3cf42013-03-12 11:29:58 -07002484 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002485 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002486
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002487 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002488
2489 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002490 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002491
Tejun Heo112202d2013-02-13 19:29:12 -08002492 if (pwq->nr_in_flight[flush_color]) {
2493 pwq->flush_color = flush_color;
2494 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002495 wait = true;
2496 }
2497 }
2498
2499 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002500 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002501 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002502 }
2503
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002504 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 }
2506
Tejun Heo112202d2013-02-13 19:29:12 -08002507 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 complete(&wq->first_flusher->done);
2509
2510 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002513/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002515 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002517 * This function sleeps until all work items which were queued on entry
2518 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002520void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 struct wq_flusher this_flusher = {
2523 .list = LIST_HEAD_INIT(this_flusher.list),
2524 .flush_color = -1,
2525 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2526 };
2527 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002528
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002529 lock_map_acquire(&wq->lockdep_map);
2530 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002531
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002532 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002533
2534 /*
2535 * Start-to-wait phase
2536 */
2537 next_color = work_next_color(wq->work_color);
2538
2539 if (next_color != wq->flush_color) {
2540 /*
2541 * Color space is not full. The current work_color
2542 * becomes our flush_color and work_color is advanced
2543 * by one.
2544 */
Tejun Heo6183c002013-03-12 11:29:57 -07002545 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002546 this_flusher.flush_color = wq->work_color;
2547 wq->work_color = next_color;
2548
2549 if (!wq->first_flusher) {
2550 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002551 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002552
2553 wq->first_flusher = &this_flusher;
2554
Tejun Heo112202d2013-02-13 19:29:12 -08002555 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 wq->work_color)) {
2557 /* nothing to flush, done */
2558 wq->flush_color = next_color;
2559 wq->first_flusher = NULL;
2560 goto out_unlock;
2561 }
2562 } else {
2563 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002564 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002565 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002566 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002567 }
2568 } else {
2569 /*
2570 * Oops, color space is full, wait on overflow queue.
2571 * The next flush completion will assign us
2572 * flush_color and transfer to flusher_queue.
2573 */
2574 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2575 }
2576
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002577 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578
2579 wait_for_completion(&this_flusher.done);
2580
2581 /*
2582 * Wake-up-and-cascade phase
2583 *
2584 * First flushers are responsible for cascading flushes and
2585 * handling overflow. Non-first flushers can simply return.
2586 */
2587 if (wq->first_flusher != &this_flusher)
2588 return;
2589
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002590 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002591
Tejun Heo4ce48b32010-07-02 10:03:51 +02002592 /* we might have raced, check again with mutex held */
2593 if (wq->first_flusher != &this_flusher)
2594 goto out_unlock;
2595
Tejun Heo73f53c42010-06-29 10:07:11 +02002596 wq->first_flusher = NULL;
2597
Tejun Heo6183c002013-03-12 11:29:57 -07002598 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2599 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002600
2601 while (true) {
2602 struct wq_flusher *next, *tmp;
2603
2604 /* complete all the flushers sharing the current flush color */
2605 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2606 if (next->flush_color != wq->flush_color)
2607 break;
2608 list_del_init(&next->list);
2609 complete(&next->done);
2610 }
2611
Tejun Heo6183c002013-03-12 11:29:57 -07002612 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2613 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002614
2615 /* this flush_color is finished, advance by one */
2616 wq->flush_color = work_next_color(wq->flush_color);
2617
2618 /* one color has been freed, handle overflow queue */
2619 if (!list_empty(&wq->flusher_overflow)) {
2620 /*
2621 * Assign the same color to all overflowed
2622 * flushers, advance work_color and append to
2623 * flusher_queue. This is the start-to-wait
2624 * phase for these overflowed flushers.
2625 */
2626 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2627 tmp->flush_color = wq->work_color;
2628
2629 wq->work_color = work_next_color(wq->work_color);
2630
2631 list_splice_tail_init(&wq->flusher_overflow,
2632 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002633 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002634 }
2635
2636 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002637 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002638 break;
2639 }
2640
2641 /*
2642 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002643 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002644 */
Tejun Heo6183c002013-03-12 11:29:57 -07002645 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2646 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002647
2648 list_del_init(&next->list);
2649 wq->first_flusher = next;
2650
Tejun Heo112202d2013-02-13 19:29:12 -08002651 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002652 break;
2653
2654 /*
2655 * Meh... this color is already done, clear first
2656 * flusher and repeat cascading.
2657 */
2658 wq->first_flusher = NULL;
2659 }
2660
2661out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002662 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
Dave Jonesae90dd52006-06-30 01:40:45 -04002664EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002666/**
2667 * drain_workqueue - drain a workqueue
2668 * @wq: workqueue to drain
2669 *
2670 * Wait until the workqueue becomes empty. While draining is in progress,
2671 * only chain queueing is allowed. IOW, only currently pending or running
2672 * work items on @wq can queue further work items on it. @wq is flushed
2673 * repeatedly until it becomes empty. The number of flushing is detemined
2674 * by the depth of chaining and should be relatively short. Whine if it
2675 * takes too long.
2676 */
2677void drain_workqueue(struct workqueue_struct *wq)
2678{
2679 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002680 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002681
2682 /*
2683 * __queue_work() needs to test whether there are drainers, is much
2684 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002685 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002686 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002687 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002688 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002689 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002690 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002691reflush:
2692 flush_workqueue(wq);
2693
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002694 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002695
Tejun Heo49e3cf42013-03-12 11:29:58 -07002696 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002697 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002698
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002699 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002700 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002701 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002702
2703 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002704 continue;
2705
2706 if (++flush_cnt == 10 ||
2707 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002708 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002709 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002710
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002711 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002712 goto reflush;
2713 }
2714
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002715 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002716 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002717 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002718}
2719EXPORT_SYMBOL_GPL(drain_workqueue);
2720
Tejun Heo606a5022012-08-20 14:51:23 -07002721static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002722{
2723 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002724 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002725 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002726
2727 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002728
Tejun Heofa1b54e2013-03-12 11:30:00 -07002729 local_irq_disable();
2730 pool = get_work_pool(work);
2731 if (!pool) {
2732 local_irq_enable();
2733 return false;
2734 }
2735
2736 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002737 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002738 pwq = get_work_pwq(work);
2739 if (pwq) {
2740 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002741 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002742 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002743 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002744 if (!worker)
2745 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002746 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002747 }
Tejun Heobaf59022010-09-16 10:42:16 +02002748
Tejun Heo112202d2013-02-13 19:29:12 -08002749 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002750 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002751
Tejun Heoe1594892011-01-09 23:32:15 +01002752 /*
2753 * If @max_active is 1 or rescuer is in use, flushing another work
2754 * item on the same workqueue may lead to deadlock. Make sure the
2755 * flusher is not running on the same workqueue by verifying write
2756 * access.
2757 */
Tejun Heo493008a2013-03-12 11:30:03 -07002758 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002759 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002760 else
Tejun Heo112202d2013-02-13 19:29:12 -08002761 lock_map_acquire_read(&pwq->wq->lockdep_map);
2762 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002763
Tejun Heobaf59022010-09-16 10:42:16 +02002764 return true;
2765already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002766 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002767 return false;
2768}
2769
Oleg Nesterovdb700892008-07-25 01:47:49 -07002770/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002771 * flush_work - wait for a work to finish executing the last queueing instance
2772 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002773 *
Tejun Heo606a5022012-08-20 14:51:23 -07002774 * Wait until @work has finished execution. @work is guaranteed to be idle
2775 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002776 *
2777 * RETURNS:
2778 * %true if flush_work() waited for the work to finish execution,
2779 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002780 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002781bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002782{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002783 struct wq_barrier barr;
2784
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002785 lock_map_acquire(&work->lockdep_map);
2786 lock_map_release(&work->lockdep_map);
2787
Tejun Heo606a5022012-08-20 14:51:23 -07002788 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002789 wait_for_completion(&barr.done);
2790 destroy_work_on_stack(&barr.work);
2791 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002792 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002793 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002794 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002795}
2796EXPORT_SYMBOL_GPL(flush_work);
2797
Tejun Heo36e227d2012-08-03 10:30:46 -07002798static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002799{
Tejun Heobbb68df2012-08-03 10:30:46 -07002800 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002801 int ret;
2802
2803 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002804 ret = try_to_grab_pending(work, is_dwork, &flags);
2805 /*
2806 * If someone else is canceling, wait for the same event it
2807 * would be waiting for before retrying.
2808 */
2809 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002810 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002811 } while (unlikely(ret < 0));
2812
Tejun Heobbb68df2012-08-03 10:30:46 -07002813 /* tell other tasks trying to grab @work to back off */
2814 mark_work_canceling(work);
2815 local_irq_restore(flags);
2816
Tejun Heo606a5022012-08-20 14:51:23 -07002817 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002818 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002819 return ret;
2820}
2821
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002822/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002823 * cancel_work_sync - cancel a work and wait for it to finish
2824 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002825 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002826 * Cancel @work and wait for its execution to finish. This function
2827 * can be used even if the work re-queues itself or migrates to
2828 * another workqueue. On return from this function, @work is
2829 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002830 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002831 * cancel_work_sync(&delayed_work->work) must not be used for
2832 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002833 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002834 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 *
2837 * RETURNS:
2838 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002839 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002840bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002841{
Tejun Heo36e227d2012-08-03 10:30:46 -07002842 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002843}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002844EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002845
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002846/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002847 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2848 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002849 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002850 * Delayed timer is cancelled and the pending work is queued for
2851 * immediate execution. Like flush_work(), this function only
2852 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002853 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002854 * RETURNS:
2855 * %true if flush_work() waited for the work to finish execution,
2856 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002857 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002858bool flush_delayed_work(struct delayed_work *dwork)
2859{
Tejun Heo8930cab2012-08-03 10:30:45 -07002860 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002861 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002862 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002863 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 return flush_work(&dwork->work);
2865}
2866EXPORT_SYMBOL(flush_delayed_work);
2867
2868/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002869 * cancel_delayed_work - cancel a delayed work
2870 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002871 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002872 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2873 * and canceled; %false if wasn't pending. Note that the work callback
2874 * function may still be running on return, unless it returns %true and the
2875 * work doesn't re-arm itself. Explicitly flush or use
2876 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002877 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002878 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002879 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002880bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002881{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002882 unsigned long flags;
2883 int ret;
2884
2885 do {
2886 ret = try_to_grab_pending(&dwork->work, true, &flags);
2887 } while (unlikely(ret == -EAGAIN));
2888
2889 if (unlikely(ret < 0))
2890 return false;
2891
Tejun Heo7c3eed52013-01-24 11:01:33 -08002892 set_work_pool_and_clear_pending(&dwork->work,
2893 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002894 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002895 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002896}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002897EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002898
2899/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002900 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2901 * @dwork: the delayed work cancel
2902 *
2903 * This is cancel_work_sync() for delayed works.
2904 *
2905 * RETURNS:
2906 * %true if @dwork was pending, %false otherwise.
2907 */
2908bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002909{
Tejun Heo36e227d2012-08-03 10:30:46 -07002910 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002912EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002914/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002915 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002916 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002917 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002918 * schedule_on_each_cpu() executes @func on each online CPU using the
2919 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002920 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002921 *
2922 * RETURNS:
2923 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002924 */
David Howells65f27f32006-11-22 14:55:48 +00002925int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002926{
2927 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002928 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002929
Andrew Mortonb6136772006-06-25 05:47:49 -07002930 works = alloc_percpu(struct work_struct);
2931 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002932 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002933
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002934 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002935
Christoph Lameter15316ba2006-01-08 01:00:43 -08002936 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002937 struct work_struct *work = per_cpu_ptr(works, cpu);
2938
2939 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002940 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002941 }
Tejun Heo93981802009-11-17 14:06:20 -08002942
2943 for_each_online_cpu(cpu)
2944 flush_work(per_cpu_ptr(works, cpu));
2945
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002946 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002947 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002948 return 0;
2949}
2950
Alan Sterneef6a7d2010-02-12 17:39:21 +09002951/**
2952 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2953 *
2954 * Forces execution of the kernel-global workqueue and blocks until its
2955 * completion.
2956 *
2957 * Think twice before calling this function! It's very easy to get into
2958 * trouble if you don't take great care. Either of the following situations
2959 * will lead to deadlock:
2960 *
2961 * One of the work items currently on the workqueue needs to acquire
2962 * a lock held by your code or its caller.
2963 *
2964 * Your code is running in the context of a work routine.
2965 *
2966 * They will be detected by lockdep when they occur, but the first might not
2967 * occur very often. It depends on what work items are on the workqueue and
2968 * what locks they need, which you have no control over.
2969 *
2970 * In most situations flushing the entire workqueue is overkill; you merely
2971 * need to know that a particular work item isn't queued and isn't running.
2972 * In such cases you should use cancel_delayed_work_sync() or
2973 * cancel_work_sync() instead.
2974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975void flush_scheduled_work(void)
2976{
Tejun Heod320c032010-06-29 10:07:14 +02002977 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
Dave Jonesae90dd52006-06-30 01:40:45 -04002979EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002982 * execute_in_process_context - reliably execute the routine with user context
2983 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002984 * @ew: guaranteed storage for the execute work structure (must
2985 * be available when the work executes)
2986 *
2987 * Executes the function immediately if process context is available,
2988 * otherwise schedules the function for delayed execution.
2989 *
2990 * Returns: 0 - function was executed
2991 * 1 - function was scheduled for execution
2992 */
David Howells65f27f32006-11-22 14:55:48 +00002993int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002994{
2995 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002996 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002997 return 0;
2998 }
2999
David Howells65f27f32006-11-22 14:55:48 +00003000 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003001 schedule_work(&ew->work);
3002
3003 return 1;
3004}
3005EXPORT_SYMBOL_GPL(execute_in_process_context);
3006
Tejun Heo226223a2013-03-12 11:30:05 -07003007#ifdef CONFIG_SYSFS
3008/*
3009 * Workqueues with WQ_SYSFS flag set is visible to userland via
3010 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3011 * following attributes.
3012 *
3013 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3014 * max_active RW int : maximum number of in-flight work items
3015 *
3016 * Unbound workqueues have the following extra attributes.
3017 *
3018 * id RO int : the associated pool ID
3019 * nice RW int : nice value of the workers
3020 * cpumask RW mask : bitmask of allowed CPUs for the workers
3021 */
3022struct wq_device {
3023 struct workqueue_struct *wq;
3024 struct device dev;
3025};
3026
3027static struct workqueue_struct *dev_to_wq(struct device *dev)
3028{
3029 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3030
3031 return wq_dev->wq;
3032}
3033
3034static ssize_t wq_per_cpu_show(struct device *dev,
3035 struct device_attribute *attr, char *buf)
3036{
3037 struct workqueue_struct *wq = dev_to_wq(dev);
3038
3039 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3040}
3041
3042static ssize_t wq_max_active_show(struct device *dev,
3043 struct device_attribute *attr, char *buf)
3044{
3045 struct workqueue_struct *wq = dev_to_wq(dev);
3046
3047 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3048}
3049
3050static ssize_t wq_max_active_store(struct device *dev,
3051 struct device_attribute *attr,
3052 const char *buf, size_t count)
3053{
3054 struct workqueue_struct *wq = dev_to_wq(dev);
3055 int val;
3056
3057 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3058 return -EINVAL;
3059
3060 workqueue_set_max_active(wq, val);
3061 return count;
3062}
3063
3064static struct device_attribute wq_sysfs_attrs[] = {
3065 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3066 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3067 __ATTR_NULL,
3068};
3069
3070static ssize_t wq_pool_id_show(struct device *dev,
3071 struct device_attribute *attr, char *buf)
3072{
3073 struct workqueue_struct *wq = dev_to_wq(dev);
3074 struct worker_pool *pool;
3075 int written;
3076
3077 rcu_read_lock_sched();
3078 pool = first_pwq(wq)->pool;
3079 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3080 rcu_read_unlock_sched();
3081
3082 return written;
3083}
3084
3085static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3086 char *buf)
3087{
3088 struct workqueue_struct *wq = dev_to_wq(dev);
3089 int written;
3090
3091 rcu_read_lock_sched();
3092 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3093 first_pwq(wq)->pool->attrs->nice);
3094 rcu_read_unlock_sched();
3095
3096 return written;
3097}
3098
3099/* prepare workqueue_attrs for sysfs store operations */
3100static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3101{
3102 struct workqueue_attrs *attrs;
3103
3104 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3105 if (!attrs)
3106 return NULL;
3107
3108 rcu_read_lock_sched();
3109 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3110 rcu_read_unlock_sched();
3111 return attrs;
3112}
3113
3114static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3115 const char *buf, size_t count)
3116{
3117 struct workqueue_struct *wq = dev_to_wq(dev);
3118 struct workqueue_attrs *attrs;
3119 int ret;
3120
3121 attrs = wq_sysfs_prep_attrs(wq);
3122 if (!attrs)
3123 return -ENOMEM;
3124
3125 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3126 attrs->nice >= -20 && attrs->nice <= 19)
3127 ret = apply_workqueue_attrs(wq, attrs);
3128 else
3129 ret = -EINVAL;
3130
3131 free_workqueue_attrs(attrs);
3132 return ret ?: count;
3133}
3134
3135static ssize_t wq_cpumask_show(struct device *dev,
3136 struct device_attribute *attr, char *buf)
3137{
3138 struct workqueue_struct *wq = dev_to_wq(dev);
3139 int written;
3140
3141 rcu_read_lock_sched();
3142 written = cpumask_scnprintf(buf, PAGE_SIZE,
3143 first_pwq(wq)->pool->attrs->cpumask);
3144 rcu_read_unlock_sched();
3145
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 Heo4e1a1f92013-03-12 11:30:00 -07003363 pool->flags |= POOL_DISASSOCIATED;
3364 INIT_LIST_HEAD(&pool->worklist);
3365 INIT_LIST_HEAD(&pool->idle_list);
3366 hash_init(pool->busy_hash);
3367
3368 init_timer_deferrable(&pool->idle_timer);
3369 pool->idle_timer.function = idle_worker_timeout;
3370 pool->idle_timer.data = (unsigned long)pool;
3371
3372 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3373 (unsigned long)pool);
3374
3375 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003376 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003377 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003378
Tejun Heo29c91e92013-03-12 11:30:03 -07003379 INIT_HLIST_NODE(&pool->hash_node);
3380 pool->refcnt = 1;
3381
3382 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003383 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3384 if (!pool->attrs)
3385 return -ENOMEM;
3386 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003387}
3388
Tejun Heo29c91e92013-03-12 11:30:03 -07003389static void rcu_free_pool(struct rcu_head *rcu)
3390{
3391 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3392
Tejun Heo822d8402013-03-19 13:45:21 -07003393 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003394 free_workqueue_attrs(pool->attrs);
3395 kfree(pool);
3396}
3397
3398/**
3399 * put_unbound_pool - put a worker_pool
3400 * @pool: worker_pool to put
3401 *
3402 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003403 * safe manner. get_unbound_pool() calls this function on its failure path
3404 * and this function should be able to release pools which went through,
3405 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003406 *
3407 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003408 */
3409static void put_unbound_pool(struct worker_pool *pool)
3410{
3411 struct worker *worker;
3412
Tejun Heoa892cac2013-04-01 11:23:32 -07003413 lockdep_assert_held(&wq_pool_mutex);
3414
3415 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003416 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003417
3418 /* sanity checks */
3419 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003420 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003421 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003422
3423 /* release id and unhash */
3424 if (pool->id >= 0)
3425 idr_remove(&worker_pool_idr, pool->id);
3426 hash_del(&pool->hash_node);
3427
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003428 /*
3429 * Become the manager and destroy all workers. Grabbing
3430 * manager_arb prevents @pool's workers from blocking on
3431 * manager_mutex.
3432 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003433 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003434 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003435 spin_lock_irq(&pool->lock);
3436
3437 while ((worker = first_worker(pool)))
3438 destroy_worker(worker);
3439 WARN_ON(pool->nr_workers || pool->nr_idle);
3440
3441 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003442 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003443 mutex_unlock(&pool->manager_arb);
3444
3445 /* shut down the timers */
3446 del_timer_sync(&pool->idle_timer);
3447 del_timer_sync(&pool->mayday_timer);
3448
3449 /* sched-RCU protected to allow dereferences from get_work_pool() */
3450 call_rcu_sched(&pool->rcu, rcu_free_pool);
3451}
3452
3453/**
3454 * get_unbound_pool - get a worker_pool with the specified attributes
3455 * @attrs: the attributes of the worker_pool to get
3456 *
3457 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3458 * reference count and return it. If there already is a matching
3459 * worker_pool, it will be used; otherwise, this function attempts to
3460 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003461 *
3462 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003463 */
3464static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3465{
Tejun Heo29c91e92013-03-12 11:30:03 -07003466 u32 hash = wqattrs_hash(attrs);
3467 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003468
Tejun Heoa892cac2013-04-01 11:23:32 -07003469 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003470
3471 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003472 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3473 if (wqattrs_equal(pool->attrs, attrs)) {
3474 pool->refcnt++;
3475 goto out_unlock;
3476 }
3477 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003478
3479 /* nope, create a new one */
3480 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3481 if (!pool || init_worker_pool(pool) < 0)
3482 goto fail;
3483
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003484 if (workqueue_freezing)
3485 pool->flags |= POOL_FREEZING;
3486
Tejun Heo8864b4e2013-03-12 11:30:04 -07003487 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 copy_workqueue_attrs(pool->attrs, attrs);
3489
3490 if (worker_pool_assign_id(pool) < 0)
3491 goto fail;
3492
3493 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003494 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003495 goto fail;
3496
Tejun Heo29c91e92013-03-12 11:30:03 -07003497 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003498 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3499out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003500 return pool;
3501fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003502 if (pool)
3503 put_unbound_pool(pool);
3504 return NULL;
3505}
3506
Tejun Heo8864b4e2013-03-12 11:30:04 -07003507static void rcu_free_pwq(struct rcu_head *rcu)
3508{
3509 kmem_cache_free(pwq_cache,
3510 container_of(rcu, struct pool_workqueue, rcu));
3511}
3512
3513/*
3514 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3515 * and needs to be destroyed.
3516 */
3517static void pwq_unbound_release_workfn(struct work_struct *work)
3518{
3519 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3520 unbound_release_work);
3521 struct workqueue_struct *wq = pwq->wq;
3522 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003523 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003524
3525 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3526 return;
3527
Tejun Heo75ccf592013-03-12 11:30:04 -07003528 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003529 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003530 * necessary on release but do it anyway. It's easier to verify
3531 * and consistent with the linking path.
3532 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003533 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003534 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003535 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003536 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003537
Tejun Heoa892cac2013-04-01 11:23:32 -07003538 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003539 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003540 mutex_unlock(&wq_pool_mutex);
3541
Tejun Heo8864b4e2013-03-12 11:30:04 -07003542 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3543
3544 /*
3545 * If we're the last pwq going away, @wq is already dead and no one
3546 * is gonna access it anymore. Free it.
3547 */
Tejun Heobc0caf02013-04-01 11:23:31 -07003548 if (is_last)
Tejun Heo8864b4e2013-03-12 11:30:04 -07003549 kfree(wq);
3550}
3551
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003552/**
Tejun Heo699ce092013-03-13 16:51:35 -07003553 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003554 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003555 *
Tejun Heo699ce092013-03-13 16:51:35 -07003556 * If @pwq isn't freezing, set @pwq->max_active to the associated
3557 * workqueue's saved_max_active and activate delayed work items
3558 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003559 */
Tejun Heo699ce092013-03-13 16:51:35 -07003560static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003561{
Tejun Heo699ce092013-03-13 16:51:35 -07003562 struct workqueue_struct *wq = pwq->wq;
3563 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003564
Tejun Heo699ce092013-03-13 16:51:35 -07003565 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003566 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003567
3568 /* fast exit for non-freezable wqs */
3569 if (!freezable && pwq->max_active == wq->saved_max_active)
3570 return;
3571
Lai Jiangshana357fc02013-03-25 16:57:19 -07003572 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003573
3574 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3575 pwq->max_active = wq->saved_max_active;
3576
3577 while (!list_empty(&pwq->delayed_works) &&
3578 pwq->nr_active < pwq->max_active)
3579 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003580
3581 /*
3582 * Need to kick a worker after thawed or an unbound wq's
3583 * max_active is bumped. It's a slow path. Do it always.
3584 */
3585 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003586 } else {
3587 pwq->max_active = 0;
3588 }
3589
Lai Jiangshana357fc02013-03-25 16:57:19 -07003590 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003591}
3592
Tejun Heod2c1d402013-03-12 11:30:04 -07003593static void init_and_link_pwq(struct pool_workqueue *pwq,
3594 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003595 struct worker_pool *pool,
3596 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003597{
3598 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3599
3600 pwq->pool = pool;
3601 pwq->wq = wq;
3602 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003603 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003604 INIT_LIST_HEAD(&pwq->delayed_works);
3605 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003606 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003607
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003608 mutex_lock(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003609
Tejun Heo983ca252013-03-13 16:51:35 -07003610 /*
3611 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003612 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003613 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003614 if (p_last_pwq)
3615 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003616 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003617
3618 /* sync max_active to the current setting */
3619 pwq_adjust_max_active(pwq);
3620
3621 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003622 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003623
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003624 mutex_unlock(&wq->mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003625}
3626
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003627/**
3628 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3629 * @wq: the target workqueue
3630 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3631 *
3632 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3633 * current attributes, a new pwq is created and made the first pwq which
3634 * will serve all new work items. Older pwqs are released as in-flight
3635 * work items finish. Note that a work item which repeatedly requeues
3636 * itself back-to-back will stay on its current pwq.
3637 *
3638 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3639 * failure.
3640 */
3641int apply_workqueue_attrs(struct workqueue_struct *wq,
3642 const struct workqueue_attrs *attrs)
3643{
Tejun Heo13e2e552013-04-01 11:23:31 -07003644 struct workqueue_attrs *new_attrs;
3645 struct pool_workqueue *pwq = NULL, *last_pwq;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003646 struct worker_pool *pool;
Tejun Heo48621252013-04-01 11:23:31 -07003647 int ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003648
Tejun Heo8719dce2013-03-12 11:30:04 -07003649 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003650 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3651 return -EINVAL;
3652
Tejun Heo8719dce2013-03-12 11:30:04 -07003653 /* creating multiple pwqs breaks ordering guarantee */
3654 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3655 return -EINVAL;
3656
Tejun Heo13e2e552013-04-01 11:23:31 -07003657 /* make a copy of @attrs and sanitize it */
3658 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3659 if (!new_attrs)
3660 goto enomem;
3661
3662 copy_workqueue_attrs(new_attrs, attrs);
3663 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3664
Tejun Heoa892cac2013-04-01 11:23:32 -07003665 mutex_lock(&wq_pool_mutex);
3666
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003667 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
Tejun Heoa892cac2013-04-01 11:23:32 -07003668 if (!pwq) {
3669 mutex_unlock(&wq_pool_mutex);
Tejun Heo13e2e552013-04-01 11:23:31 -07003670 goto enomem;
Tejun Heoa892cac2013-04-01 11:23:32 -07003671 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003672
Tejun Heo13e2e552013-04-01 11:23:31 -07003673 pool = get_unbound_pool(new_attrs);
Tejun Heoa892cac2013-04-01 11:23:32 -07003674 if (!pool) {
3675 mutex_unlock(&wq_pool_mutex);
Tejun Heo13e2e552013-04-01 11:23:31 -07003676 goto enomem;
Tejun Heoa892cac2013-04-01 11:23:32 -07003677 }
3678
3679 mutex_unlock(&wq_pool_mutex);
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003680
3681 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3682 if (last_pwq) {
3683 spin_lock_irq(&last_pwq->pool->lock);
3684 put_pwq(last_pwq);
3685 spin_unlock_irq(&last_pwq->pool->lock);
3686 }
3687
Tejun Heo48621252013-04-01 11:23:31 -07003688 ret = 0;
3689 /* fall through */
3690out_free:
3691 free_workqueue_attrs(new_attrs);
3692 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003693
3694enomem:
3695 kmem_cache_free(pwq_cache, pwq);
Tejun Heo48621252013-04-01 11:23:31 -07003696 ret = -ENOMEM;
3697 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003698}
3699
Tejun Heo30cdf242013-03-12 11:29:57 -07003700static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003702 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003703 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003704
Tejun Heo30cdf242013-03-12 11:29:57 -07003705 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003706 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3707 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003708 return -ENOMEM;
3709
3710 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003711 struct pool_workqueue *pwq =
3712 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003713 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003714 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003715
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003716 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003717 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003718 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003719 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003720 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003721 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003722}
3723
Tejun Heof3421792010-07-02 10:03:51 +02003724static int wq_clamp_max_active(int max_active, unsigned int flags,
3725 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003726{
Tejun Heof3421792010-07-02 10:03:51 +02003727 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3728
3729 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003730 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3731 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003732
Tejun Heof3421792010-07-02 10:03:51 +02003733 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003734}
3735
Tejun Heob196be82012-01-10 15:11:35 -08003736struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003737 unsigned int flags,
3738 int max_active,
3739 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003740 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003741{
Tejun Heob196be82012-01-10 15:11:35 -08003742 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003743 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003744 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003745 size_t namelen;
3746
3747 /* determine namelen, allocate wq and format name */
3748 va_start(args, lock_name);
3749 va_copy(args1, args);
3750 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3751
3752 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3753 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003754 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003755
3756 vsnprintf(wq->name, namelen, fmt, args1);
3757 va_end(args);
3758 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003759
Tejun Heod320c032010-06-29 10:07:14 +02003760 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003761 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003762
Tejun Heob196be82012-01-10 15:11:35 -08003763 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003764 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003765 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003766 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003767 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003768 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003769 INIT_LIST_HEAD(&wq->flusher_queue);
3770 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003771 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003772
Johannes Bergeb13ba82008-01-16 09:51:58 +01003773 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003774 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003775
Tejun Heo30cdf242013-03-12 11:29:57 -07003776 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003777 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003778
Tejun Heo493008a2013-03-12 11:30:03 -07003779 /*
3780 * Workqueues which may be used during memory reclaim should
3781 * have a rescuer to guarantee forward progress.
3782 */
3783 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003784 struct worker *rescuer;
3785
Tejun Heod2c1d402013-03-12 11:30:04 -07003786 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003787 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003788 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003789
Tejun Heo111c2252013-01-17 17:16:24 -08003790 rescuer->rescue_wq = wq;
3791 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003792 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003793 if (IS_ERR(rescuer->task)) {
3794 kfree(rescuer);
3795 goto err_destroy;
3796 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003797
Tejun Heod2c1d402013-03-12 11:30:04 -07003798 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003799 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003800 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003801 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003802
Tejun Heo226223a2013-03-12 11:30:05 -07003803 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3804 goto err_destroy;
3805
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003806 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003807 * wq_pool_mutex protects global freeze state and workqueues list.
3808 * Grab it, adjust max_active and add the new @wq to workqueues
3809 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003810 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003811 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003812
Lai Jiangshana357fc02013-03-25 16:57:19 -07003813 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003814 for_each_pwq(pwq, wq)
3815 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003816 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817
Tejun Heo15376632010-06-29 10:07:11 +02003818 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003819
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003820 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003821
Oleg Nesterov3af244332007-05-09 02:34:09 -07003822 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003823
3824err_free_wq:
3825 kfree(wq);
3826 return NULL;
3827err_destroy:
3828 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003829 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003830}
Tejun Heod320c032010-06-29 10:07:14 +02003831EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003832
3833/**
3834 * destroy_workqueue - safely terminate a workqueue
3835 * @wq: target workqueue
3836 *
3837 * Safely destroy a workqueue. All work currently pending will be done first.
3838 */
3839void destroy_workqueue(struct workqueue_struct *wq)
3840{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003841 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003842
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003843 /* drain it before proceeding with destruction */
3844 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003845
Tejun Heo6183c002013-03-12 11:29:57 -07003846 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003847 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003848 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003849 int i;
3850
Tejun Heo76af4d92013-03-12 11:30:00 -07003851 for (i = 0; i < WORK_NR_COLORS; i++) {
3852 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003853 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003854 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003855 }
3856 }
3857
Tejun Heo8864b4e2013-03-12 11:30:04 -07003858 if (WARN_ON(pwq->refcnt > 1) ||
3859 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003860 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003861 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003862 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003863 }
Tejun Heo6183c002013-03-12 11:29:57 -07003864 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003865 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003866
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003867 /*
3868 * wq list is used to freeze wq, remove from list after
3869 * flushing is complete in case freeze races us.
3870 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003871 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003872 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003873 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003874
Tejun Heo226223a2013-03-12 11:30:05 -07003875 workqueue_sysfs_unregister(wq);
3876
Tejun Heo493008a2013-03-12 11:30:03 -07003877 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003878 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003879 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003880 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003881 }
3882
Tejun Heo8864b4e2013-03-12 11:30:04 -07003883 if (!(wq->flags & WQ_UNBOUND)) {
3884 /*
3885 * The base ref is never dropped on per-cpu pwqs. Directly
3886 * free the pwqs and wq.
3887 */
3888 free_percpu(wq->cpu_pwqs);
3889 kfree(wq);
3890 } else {
3891 /*
3892 * We're the sole accessor of @wq at this point. Directly
3893 * access the first pwq and put the base ref. As both pwqs
3894 * and pools are sched-RCU protected, the lock operations
3895 * are safe. @wq will be freed when the last pwq is
3896 * released.
3897 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003898 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3899 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003900 spin_lock_irq(&pwq->pool->lock);
3901 put_pwq(pwq);
3902 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003903 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003904}
3905EXPORT_SYMBOL_GPL(destroy_workqueue);
3906
Tejun Heodcd989c2010-06-29 10:07:14 +02003907/**
3908 * workqueue_set_max_active - adjust max_active of a workqueue
3909 * @wq: target workqueue
3910 * @max_active: new max_active value.
3911 *
3912 * Set max_active of @wq to @max_active.
3913 *
3914 * CONTEXT:
3915 * Don't call from IRQ context.
3916 */
3917void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3918{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003919 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003920
Tejun Heo8719dce2013-03-12 11:30:04 -07003921 /* disallow meddling with max_active for ordered workqueues */
3922 if (WARN_ON(wq->flags & __WQ_ORDERED))
3923 return;
3924
Tejun Heof3421792010-07-02 10:03:51 +02003925 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003926
Lai Jiangshana357fc02013-03-25 16:57:19 -07003927 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003928
3929 wq->saved_max_active = max_active;
3930
Tejun Heo699ce092013-03-13 16:51:35 -07003931 for_each_pwq(pwq, wq)
3932 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003933
Lai Jiangshana357fc02013-03-25 16:57:19 -07003934 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003935}
3936EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3937
3938/**
Tejun Heoe6267612013-03-12 17:41:37 -07003939 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3940 *
3941 * Determine whether %current is a workqueue rescuer. Can be used from
3942 * work functions to determine whether it's being run off the rescuer task.
3943 */
3944bool current_is_workqueue_rescuer(void)
3945{
3946 struct worker *worker = current_wq_worker();
3947
Lai Jiangshan6a092df2013-03-20 03:28:03 +08003948 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07003949}
3950
3951/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003952 * workqueue_congested - test whether a workqueue is congested
3953 * @cpu: CPU in question
3954 * @wq: target workqueue
3955 *
3956 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3957 * no synchronization around this function and the test result is
3958 * unreliable and only useful as advisory hints or for debugging.
3959 *
3960 * RETURNS:
3961 * %true if congested, %false otherwise.
3962 */
Tejun Heod84ff052013-03-12 11:29:59 -07003963bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003964{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003965 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003966 bool ret;
3967
Lai Jiangshan88109452013-03-20 03:28:10 +08003968 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003969
3970 if (!(wq->flags & WQ_UNBOUND))
3971 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3972 else
3973 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003974
Tejun Heo76af4d92013-03-12 11:30:00 -07003975 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08003976 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07003977
3978 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003979}
3980EXPORT_SYMBOL_GPL(workqueue_congested);
3981
3982/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003983 * work_busy - test whether a work is currently pending or running
3984 * @work: the work to be tested
3985 *
3986 * Test whether @work is currently pending or running. There is no
3987 * synchronization around this function and the test result is
3988 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003989 *
3990 * RETURNS:
3991 * OR'd bitmask of WORK_BUSY_* bits.
3992 */
3993unsigned int work_busy(struct work_struct *work)
3994{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003995 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003996 unsigned long flags;
3997 unsigned int ret = 0;
3998
Tejun Heodcd989c2010-06-29 10:07:14 +02003999 if (work_pending(work))
4000 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004001
Tejun Heofa1b54e2013-03-12 11:30:00 -07004002 local_irq_save(flags);
4003 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004004 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004005 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004006 if (find_worker_executing_work(pool, work))
4007 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004008 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004009 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004010 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004011
4012 return ret;
4013}
4014EXPORT_SYMBOL_GPL(work_busy);
4015
Tejun Heodb7bccf2010-06-29 10:07:12 +02004016/*
4017 * CPU hotplug.
4018 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004019 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004020 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004021 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004022 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004023 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004024 * blocked draining impractical.
4025 *
Tejun Heo24647572013-01-24 11:01:33 -08004026 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004027 * running as an unbound one and allowing it to be reattached later if the
4028 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004029 */
4030
Tejun Heo706026c2013-01-24 11:01:34 -08004031static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004032{
Tejun Heo38db41d2013-01-24 11:01:34 -08004033 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004034 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004035 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004036 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004037
Tejun Heof02ae732013-03-12 11:30:03 -07004038 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004039 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004040
Tejun Heobc3a1af2013-03-13 19:47:39 -07004041 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004042 spin_lock_irq(&pool->lock);
4043
4044 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004045 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004046 * unbound and set DISASSOCIATED. Before this, all workers
4047 * except for the ones which are still executing works from
4048 * before the last CPU down must be on the cpu. After
4049 * this, they may become diasporas.
4050 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004051 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004052 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004053
Tejun Heo24647572013-01-24 11:01:33 -08004054 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004055
Tejun Heo94cf58b2013-01-24 11:01:33 -08004056 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004057 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004058 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004059
4060 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004061 * Call schedule() so that we cross rq->lock and thus can guarantee
4062 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4063 * as scheduler callbacks may be invoked from other cpus.
4064 */
4065 schedule();
4066
4067 /*
4068 * Sched callbacks are disabled now. Zap nr_running. After this,
4069 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004070 * are always true as long as the worklist is not empty. Pools on
4071 * @cpu now behave as unbound (in terms of concurrency management)
4072 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004073 *
4074 * On return from this function, the current worker would trigger
4075 * unbound chain execution of pending work items if other workers
4076 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004077 */
Tejun Heof02ae732013-03-12 11:30:03 -07004078 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004079 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004080}
4081
Tejun Heobd7c0892013-03-19 13:45:21 -07004082/**
4083 * rebind_workers - rebind all workers of a pool to the associated CPU
4084 * @pool: pool of interest
4085 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004086 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004087 */
4088static void rebind_workers(struct worker_pool *pool)
4089{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004090 struct worker *worker;
4091 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004092
4093 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004094
Tejun Heoa9ab7752013-03-19 13:45:21 -07004095 /*
4096 * Restore CPU affinity of all workers. As all idle workers should
4097 * be on the run-queue of the associated CPU before any local
4098 * wake-ups for concurrency management happen, restore CPU affinty
4099 * of all workers first and then clear UNBOUND. As we're called
4100 * from CPU_ONLINE, the following shouldn't fail.
4101 */
4102 for_each_pool_worker(worker, wi, pool)
4103 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4104 pool->attrs->cpumask) < 0);
4105
4106 spin_lock_irq(&pool->lock);
4107
4108 for_each_pool_worker(worker, wi, pool) {
4109 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004110
4111 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004112 * A bound idle worker should actually be on the runqueue
4113 * of the associated CPU for local wake-ups targeting it to
4114 * work. Kick all idle workers so that they migrate to the
4115 * associated CPU. Doing this in the same loop as
4116 * replacing UNBOUND with REBOUND is safe as no worker will
4117 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004118 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004119 if (worker_flags & WORKER_IDLE)
4120 wake_up_process(worker->task);
4121
4122 /*
4123 * We want to clear UNBOUND but can't directly call
4124 * worker_clr_flags() or adjust nr_running. Atomically
4125 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4126 * @worker will clear REBOUND using worker_clr_flags() when
4127 * it initiates the next execution cycle thus restoring
4128 * concurrency management. Note that when or whether
4129 * @worker clears REBOUND doesn't affect correctness.
4130 *
4131 * ACCESS_ONCE() is necessary because @worker->flags may be
4132 * tested without holding any lock in
4133 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4134 * fail incorrectly leading to premature concurrency
4135 * management operations.
4136 */
4137 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4138 worker_flags |= WORKER_REBOUND;
4139 worker_flags &= ~WORKER_UNBOUND;
4140 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004141 }
4142
Tejun Heoa9ab7752013-03-19 13:45:21 -07004143 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004144}
4145
Tejun Heo7dbc7252013-03-19 13:45:21 -07004146/**
4147 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4148 * @pool: unbound pool of interest
4149 * @cpu: the CPU which is coming up
4150 *
4151 * An unbound pool may end up with a cpumask which doesn't have any online
4152 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4153 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4154 * online CPU before, cpus_allowed of all its workers should be restored.
4155 */
4156static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4157{
4158 static cpumask_t cpumask;
4159 struct worker *worker;
4160 int wi;
4161
4162 lockdep_assert_held(&pool->manager_mutex);
4163
4164 /* is @cpu allowed for @pool? */
4165 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4166 return;
4167
4168 /* is @cpu the only online CPU? */
4169 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4170 if (cpumask_weight(&cpumask) != 1)
4171 return;
4172
4173 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4174 for_each_pool_worker(worker, wi, pool)
4175 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4176 pool->attrs->cpumask) < 0);
4177}
4178
Tejun Heo8db25e72012-07-17 12:39:28 -07004179/*
4180 * Workqueues should be brought up before normal priority CPU notifiers.
4181 * This will be registered high priority CPU notifier.
4182 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004183static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004184 unsigned long action,
4185 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004186{
Tejun Heod84ff052013-03-12 11:29:59 -07004187 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004188 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004189 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Tejun Heo8db25e72012-07-17 12:39:28 -07004191 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004193 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004194 if (pool->nr_workers)
4195 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004196 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004197 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004199 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004200
Tejun Heo65758202012-07-17 12:39:26 -07004201 case CPU_DOWN_FAILED:
4202 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004203 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004204
4205 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004206 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004207
Tejun Heo7dbc7252013-03-19 13:45:21 -07004208 if (pool->cpu == cpu) {
4209 spin_lock_irq(&pool->lock);
4210 pool->flags &= ~POOL_DISASSOCIATED;
4211 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004212
Tejun Heo7dbc7252013-03-19 13:45:21 -07004213 rebind_workers(pool);
4214 } else if (pool->cpu < 0) {
4215 restore_unbound_workers_cpumask(pool, cpu);
4216 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004217
Tejun Heobc3a1af2013-03-13 19:47:39 -07004218 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004219 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004220
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004221 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004222 break;
Tejun Heo65758202012-07-17 12:39:26 -07004223 }
4224 return NOTIFY_OK;
4225}
4226
4227/*
4228 * Workqueues should be brought down after normal priority CPU notifiers.
4229 * This will be registered as low priority CPU notifier.
4230 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004231static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004232 unsigned long action,
4233 void *hcpu)
4234{
Tejun Heod84ff052013-03-12 11:29:59 -07004235 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004236 struct work_struct unbind_work;
4237
Tejun Heo65758202012-07-17 12:39:26 -07004238 switch (action & ~CPU_TASKS_FROZEN) {
4239 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004240 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004241 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004242 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004243 flush_work(&unbind_work);
4244 break;
Tejun Heo65758202012-07-17 12:39:26 -07004245 }
4246 return NOTIFY_OK;
4247}
4248
Rusty Russell2d3854a2008-11-05 13:39:10 +11004249#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004250
Rusty Russell2d3854a2008-11-05 13:39:10 +11004251struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004252 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004253 long (*fn)(void *);
4254 void *arg;
4255 long ret;
4256};
4257
Tejun Heoed48ece2012-09-18 12:48:43 -07004258static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004259{
Tejun Heoed48ece2012-09-18 12:48:43 -07004260 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4261
Rusty Russell2d3854a2008-11-05 13:39:10 +11004262 wfc->ret = wfc->fn(wfc->arg);
4263}
4264
4265/**
4266 * work_on_cpu - run a function in user context on a particular cpu
4267 * @cpu: the cpu to run on
4268 * @fn: the function to run
4269 * @arg: the function arg
4270 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004271 * This will return the value @fn returns.
4272 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004273 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004274 */
Tejun Heod84ff052013-03-12 11:29:59 -07004275long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004276{
Tejun Heoed48ece2012-09-18 12:48:43 -07004277 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004278
Tejun Heoed48ece2012-09-18 12:48:43 -07004279 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4280 schedule_work_on(cpu, &wfc.work);
4281 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004282 return wfc.ret;
4283}
4284EXPORT_SYMBOL_GPL(work_on_cpu);
4285#endif /* CONFIG_SMP */
4286
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004287#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304288
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004289/**
4290 * freeze_workqueues_begin - begin freezing workqueues
4291 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004292 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004293 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004294 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004295 *
4296 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004297 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004298 */
4299void freeze_workqueues_begin(void)
4300{
Tejun Heo17116962013-03-12 11:29:58 -07004301 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004302 struct workqueue_struct *wq;
4303 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004304 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004305
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004306 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004307
Tejun Heo6183c002013-03-12 11:29:57 -07004308 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004309 workqueue_freezing = true;
4310
Tejun Heo24b8a842013-03-12 11:29:58 -07004311 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004312 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004313 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004314 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4315 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004316 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004317 }
4318
Tejun Heo24b8a842013-03-12 11:29:58 -07004319 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004320 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004321 for_each_pwq(pwq, wq)
4322 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004323 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004324 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004325
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004326 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004328
4329/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004330 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004331 *
4332 * Check whether freezing is complete. This function must be called
4333 * between freeze_workqueues_begin() and thaw_workqueues().
4334 *
4335 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004336 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004337 *
4338 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004339 * %true if some freezable workqueues are still busy. %false if freezing
4340 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004341 */
4342bool freeze_workqueues_busy(void)
4343{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004344 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004345 struct workqueue_struct *wq;
4346 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004347
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004348 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004349
Tejun Heo6183c002013-03-12 11:29:57 -07004350 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004351
Tejun Heo24b8a842013-03-12 11:29:58 -07004352 list_for_each_entry(wq, &workqueues, list) {
4353 if (!(wq->flags & WQ_FREEZABLE))
4354 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004355 /*
4356 * nr_active is monotonically decreasing. It's safe
4357 * to peek without lock.
4358 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004359 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004360 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004361 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004362 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004363 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004364 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004365 goto out_unlock;
4366 }
4367 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004368 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004369 }
4370out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004371 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004372 return busy;
4373}
4374
4375/**
4376 * thaw_workqueues - thaw workqueues
4377 *
4378 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004379 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004380 *
4381 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004382 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004383 */
4384void thaw_workqueues(void)
4385{
Tejun Heo24b8a842013-03-12 11:29:58 -07004386 struct workqueue_struct *wq;
4387 struct pool_workqueue *pwq;
4388 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004389 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004390
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004391 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004392
4393 if (!workqueue_freezing)
4394 goto out_unlock;
4395
Tejun Heo24b8a842013-03-12 11:29:58 -07004396 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004397 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004398 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004399 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4400 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004401 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004402 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004403
Tejun Heo24b8a842013-03-12 11:29:58 -07004404 /* restore max_active and repopulate worklist */
4405 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004406 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004407 for_each_pwq(pwq, wq)
4408 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004409 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004410 }
4411
4412 workqueue_freezing = false;
4413out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004414 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004415}
4416#endif /* CONFIG_FREEZER */
4417
Tejun Heobce90382013-04-01 11:23:32 -07004418static void __init wq_numa_init(void)
4419{
4420 cpumask_var_t *tbl;
4421 int node, cpu;
4422
4423 /* determine NUMA pwq table len - highest node id + 1 */
4424 for_each_node(node)
4425 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4426
4427 if (num_possible_nodes() <= 1)
4428 return;
4429
4430 /*
4431 * We want masks of possible CPUs of each node which isn't readily
4432 * available. Build one from cpu_to_node() which should have been
4433 * fully initialized by now.
4434 */
4435 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4436 BUG_ON(!tbl);
4437
4438 for_each_node(node)
4439 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4440
4441 for_each_possible_cpu(cpu) {
4442 node = cpu_to_node(cpu);
4443 if (WARN_ON(node == NUMA_NO_NODE)) {
4444 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4445 /* happens iff arch is bonkers, let's just proceed */
4446 return;
4447 }
4448 cpumask_set_cpu(cpu, tbl[node]);
4449 }
4450
4451 wq_numa_possible_cpumask = tbl;
4452 wq_numa_enabled = true;
4453}
4454
Suresh Siddha6ee05782010-07-30 14:57:37 -07004455static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004457 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4458 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004459
Tejun Heo7c3eed52013-01-24 11:01:33 -08004460 /* make sure we have enough bits for OFFQ pool ID */
4461 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004462 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004463
Tejun Heoe904e6c2013-03-12 11:29:57 -07004464 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4465
4466 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4467
Tejun Heo65758202012-07-17 12:39:26 -07004468 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004469 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004470
Tejun Heobce90382013-04-01 11:23:32 -07004471 wq_numa_init();
4472
Tejun Heo706026c2013-01-24 11:01:34 -08004473 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004474 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004475 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004476
Tejun Heo7a4e3442013-03-12 11:30:00 -07004477 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004478 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004479 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004480 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004481 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004482 pool->attrs->nice = std_nice[i++];
4483
Tejun Heo9daf9e62013-01-24 11:01:33 -08004484 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004485 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004486 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004487 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004488 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004489 }
4490
Tejun Heoe22bee72010-06-29 10:07:14 +02004491 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004492 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004493 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004494
Tejun Heof02ae732013-03-12 11:30:03 -07004495 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004496 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004497 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004498 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004499 }
4500
Tejun Heo29c91e92013-03-12 11:30:03 -07004501 /* create default unbound wq attrs */
4502 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4503 struct workqueue_attrs *attrs;
4504
4505 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004506 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004507 unbound_std_wq_attrs[i] = attrs;
4508 }
4509
Tejun Heod320c032010-06-29 10:07:14 +02004510 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004511 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004512 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004513 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4514 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004515 system_freezable_wq = alloc_workqueue("events_freezable",
4516 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004517 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004518 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004521early_initcall(init_workqueues);