Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | c54fce6 | 2010-09-10 16:51:36 +0200 | [diff] [blame] | 2 | * kernel/workqueue.c - generic async execution with shared worker pool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Tejun Heo | c54fce6 | 2010-09-10 16:51:36 +0200 | [diff] [blame] | 4 | * Copyright (C) 2002 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Tejun Heo | c54fce6 | 2010-09-10 16:51:36 +0200 | [diff] [blame] | 6 | * 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 Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 11 | * |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 12 | * Made to use alloc_percpu by Christoph Lameter. |
Tejun Heo | c54fce6 | 2010-09-10 16:51:36 +0200 | [diff] [blame] | 13 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #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 Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 37 | #include <linux/hardirq.h> |
Christoph Lameter | 46934023 | 2006-10-11 01:21:26 -0700 | [diff] [blame] | 38 | #include <linux/mempolicy.h> |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 39 | #include <linux/freezer.h> |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 40 | #include <linux/kallsyms.h> |
| 41 | #include <linux/debug_locks.h> |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 42 | #include <linux/lockdep.h> |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 43 | #include <linux/idr.h> |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 44 | #include <linux/jhash.h> |
Sasha Levin | 42f8570 | 2012-12-17 10:01:23 -0500 | [diff] [blame] | 45 | #include <linux/hashtable.h> |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 46 | #include <linux/rculist.h> |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 47 | |
Tejun Heo | ea13844 | 2013-01-18 14:05:55 -0800 | [diff] [blame] | 48 | #include "workqueue_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 50 | enum { |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 51 | /* |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 52 | * worker_pool flags |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 53 | * |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 54 | * A bound pool is either associated or disassociated with its CPU. |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 55 | * While associated (!DISASSOCIATED), all workers are bound to the |
| 56 | * CPU and none has %WORKER_UNBOUND set and concurrency management |
| 57 | * is in effect. |
| 58 | * |
| 59 | * While DISASSOCIATED, the cpu may be offline and all workers have |
| 60 | * %WORKER_UNBOUND set and concurrency management disabled, and may |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 61 | * be executing on any CPU. The pool behaves as an unbound one. |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 62 | * |
| 63 | * Note that DISASSOCIATED can be flipped only while holding |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 64 | * assoc_mutex to avoid changing binding state while |
| 65 | * create_worker() is in progress. |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 66 | */ |
Tejun Heo | 11ebea5 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 67 | POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 68 | POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ |
Tejun Heo | 35b6bb6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 69 | POOL_FREEZING = 1 << 3, /* freeze in progress */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 70 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 71 | /* worker flags */ |
| 72 | WORKER_STARTED = 1 << 0, /* started */ |
| 73 | WORKER_DIE = 1 << 1, /* die die die */ |
| 74 | WORKER_IDLE = 1 << 2, /* is idle */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 75 | WORKER_PREP = 1 << 3, /* preparing to run works */ |
Tejun Heo | fb0e7be | 2010-06-29 10:07:15 +0200 | [diff] [blame] | 76 | WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 77 | WORKER_UNBOUND = 1 << 7, /* worker is unbound */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 78 | |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 79 | WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND | |
Tejun Heo | 403c821 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 80 | WORKER_CPU_INTENSIVE, |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 81 | |
Tejun Heo | e34cdddb | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 82 | NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 83 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 84 | UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 85 | BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 86 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 87 | MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ |
| 88 | IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ |
| 89 | |
Tejun Heo | 3233cdb | 2011-02-16 18:10:19 +0100 | [diff] [blame] | 90 | MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, |
| 91 | /* call for help after 10ms |
| 92 | (min two ticks) */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 93 | MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ |
| 94 | CREATE_COOLDOWN = HZ, /* time to breath after fail */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Rescue workers are used only on emergencies and shared by |
| 98 | * all cpus. Give -20. |
| 99 | */ |
| 100 | RESCUER_NICE_LEVEL = -20, |
Tejun Heo | 3270476 | 2012-07-13 22:16:45 -0700 | [diff] [blame] | 101 | HIGHPRI_NICE_LEVEL = -20, |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 102 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 105 | * Structure fields follow one of the following exclusion rules. |
| 106 | * |
Tejun Heo | e41e704 | 2010-08-24 14:22:47 +0200 | [diff] [blame] | 107 | * I: Modifiable by initialization/destruction paths and read-only for |
| 108 | * everyone else. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 109 | * |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 110 | * P: Preemption protected. Disabling preemption is enough and should |
| 111 | * only be modified and accessed from the local cpu. |
| 112 | * |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 113 | * L: pool->lock protected. Access with pool->lock held. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 114 | * |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 115 | * X: During normal operation, modification requires pool->lock and should |
| 116 | * be done only from local cpu. Either disabling preemption on local |
| 117 | * cpu or grabbing pool->lock is enough for read access. If |
| 118 | * POOL_DISASSOCIATED is set, it's identical to L. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 119 | * |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 120 | * F: wq->flush_mutex protected. |
| 121 | * |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 122 | * W: workqueue_lock protected. |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 123 | * |
| 124 | * R: workqueue_lock protected for writes. Sched-RCU protected for reads. |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 125 | * |
| 126 | * FR: wq->flush_mutex and workqueue_lock protected for writes. Sched-RCU |
| 127 | * protected for reads. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 128 | */ |
| 129 | |
Tejun Heo | 2eaebdb | 2013-01-18 14:05:55 -0800 | [diff] [blame] | 130 | /* struct worker is defined in workqueue_internal.h */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 131 | |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 132 | struct worker_pool { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 133 | spinlock_t lock; /* the pool lock */ |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 134 | int cpu; /* I: the associated cpu */ |
Tejun Heo | 9daf9e6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 135 | int id; /* I: pool ID */ |
Tejun Heo | 11ebea5 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 136 | unsigned int flags; /* X: flags */ |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 137 | |
| 138 | struct list_head worklist; /* L: list of pending works */ |
| 139 | int nr_workers; /* L: total number of workers */ |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 140 | |
| 141 | /* nr_idle includes the ones off idle_list for rebinding */ |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 142 | int nr_idle; /* L: currently idle ones */ |
| 143 | |
| 144 | struct list_head idle_list; /* X: list of idle workers */ |
| 145 | struct timer_list idle_timer; /* L: worker idle timeout */ |
| 146 | struct timer_list mayday_timer; /* L: SOS timer for workers */ |
| 147 | |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 148 | /* workers are chained either in busy_hash or idle_list */ |
| 149 | DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); |
| 150 | /* L: hash of busy workers */ |
| 151 | |
Tejun Heo | 34a06bd | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 152 | struct mutex manager_arb; /* manager arbitration */ |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 153 | struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */ |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 154 | struct ida worker_ida; /* L: for worker IDs */ |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 155 | |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 156 | struct workqueue_attrs *attrs; /* I: worker attributes */ |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 157 | struct hlist_node hash_node; /* R: unbound_pool_hash node */ |
| 158 | int refcnt; /* refcnt for unbound pools */ |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 159 | |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 160 | /* |
| 161 | * The current concurrency level. As it's likely to be accessed |
| 162 | * from other CPUs during try_to_wake_up(), put it in a separate |
| 163 | * cacheline. |
| 164 | */ |
| 165 | atomic_t nr_running ____cacheline_aligned_in_smp; |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Destruction of pool is sched-RCU protected to allow dereferences |
| 169 | * from get_work_pool(). |
| 170 | */ |
| 171 | struct rcu_head rcu; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 172 | } ____cacheline_aligned_in_smp; |
| 173 | |
| 174 | /* |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 175 | * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS |
| 176 | * of work_struct->data are used for flags and the remaining high bits |
| 177 | * point to the pwq; thus, pwqs need to be aligned at two's power of the |
| 178 | * number of flag bits. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 180 | struct pool_workqueue { |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 181 | struct worker_pool *pool; /* I: the associated pool */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 182 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 183 | int work_color; /* L: current color */ |
| 184 | int flush_color; /* L: flushing color */ |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 185 | int refcnt; /* L: reference count */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 186 | int nr_in_flight[WORK_NR_COLORS]; |
| 187 | /* L: nr of in_flight works */ |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 188 | int nr_active; /* L: nr of active works */ |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 189 | int max_active; /* L: max active works */ |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 190 | struct list_head delayed_works; /* L: delayed works */ |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 191 | struct list_head pwqs_node; /* FR: node on wq->pwqs */ |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 192 | struct list_head mayday_node; /* W: node on wq->maydays */ |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Release of unbound pwq is punted to system_wq. See put_pwq() |
| 196 | * and pwq_unbound_release_workfn() for details. pool_workqueue |
| 197 | * itself is also sched-RCU protected so that the first pwq can be |
| 198 | * determined without grabbing workqueue_lock. |
| 199 | */ |
| 200 | struct work_struct unbound_release_work; |
| 201 | struct rcu_head rcu; |
Tejun Heo | e904e6c | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 202 | } __aligned(1 << WORK_STRUCT_FLAG_BITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 205 | * Structure used to wait for workqueue flush. |
| 206 | */ |
| 207 | struct wq_flusher { |
| 208 | struct list_head list; /* F: list of flushers */ |
| 209 | int flush_color; /* F: flush color waiting for */ |
| 210 | struct completion done; /* flush completion */ |
| 211 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 213 | struct wq_device; |
| 214 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 215 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | * The externally visible workqueue abstraction is an array of |
| 217 | * per-CPU workqueues: |
| 218 | */ |
| 219 | struct workqueue_struct { |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 220 | unsigned int flags; /* W: WQ_* flags */ |
Tejun Heo | 420c0dd | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 221 | struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */ |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 222 | struct list_head pwqs; /* FR: all pwqs of this wq */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 223 | struct list_head list; /* W: list of all workqueues */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 224 | |
| 225 | struct mutex flush_mutex; /* protects wq flushing */ |
| 226 | int work_color; /* F: current work color */ |
| 227 | int flush_color; /* F: current flush color */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 228 | atomic_t nr_pwqs_to_flush; /* flush in progress */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 229 | struct wq_flusher *first_flusher; /* F: first flusher */ |
| 230 | struct list_head flusher_queue; /* F: flush waiters */ |
| 231 | struct list_head flusher_overflow; /* F: flush overflow list */ |
| 232 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 233 | struct list_head maydays; /* W: pwqs requesting rescue */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 234 | struct worker *rescuer; /* I: rescue worker */ |
| 235 | |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 236 | int nr_drainers; /* W: drain in progress */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 237 | int saved_max_active; /* W: saved pwq max_active */ |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 238 | |
| 239 | #ifdef CONFIG_SYSFS |
| 240 | struct wq_device *wq_dev; /* I: for sysfs interface */ |
| 241 | #endif |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 242 | #ifdef CONFIG_LOCKDEP |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 243 | struct lockdep_map lockdep_map; |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 244 | #endif |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 245 | char name[]; /* I: workqueue name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
Tejun Heo | e904e6c | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 248 | static struct kmem_cache *pwq_cache; |
| 249 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 250 | /* hash of all unbound pools keyed by pool->attrs */ |
| 251 | static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); |
| 252 | |
| 253 | static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; |
| 254 | |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 255 | struct workqueue_struct *system_wq __read_mostly; |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 256 | EXPORT_SYMBOL_GPL(system_wq); |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 257 | struct workqueue_struct *system_highpri_wq __read_mostly; |
Joonsoo Kim | 1aabe90 | 2012-08-15 23:25:39 +0900 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(system_highpri_wq); |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 259 | struct workqueue_struct *system_long_wq __read_mostly; |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 260 | EXPORT_SYMBOL_GPL(system_long_wq); |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 261 | struct workqueue_struct *system_unbound_wq __read_mostly; |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 263 | struct workqueue_struct *system_freezable_wq __read_mostly; |
Tejun Heo | 24d51ad | 2011-02-21 09:52:50 +0100 | [diff] [blame] | 264 | EXPORT_SYMBOL_GPL(system_freezable_wq); |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 265 | |
Tejun Heo | 97bd234 | 2010-10-05 10:41:14 +0200 | [diff] [blame] | 266 | #define CREATE_TRACE_POINTS |
| 267 | #include <trace/events/workqueue.h> |
| 268 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 269 | #define assert_rcu_or_wq_lock() \ |
| 270 | rcu_lockdep_assert(rcu_read_lock_sched_held() || \ |
| 271 | lockdep_is_held(&workqueue_lock), \ |
| 272 | "sched RCU or workqueue lock should be held") |
| 273 | |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 274 | #define for_each_cpu_worker_pool(pool, cpu) \ |
| 275 | for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ |
| 276 | (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ |
Tejun Heo | 7a62c2c | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 277 | (pool)++) |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 278 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 279 | #define for_each_busy_worker(worker, i, pool) \ |
| 280 | hash_for_each(pool->busy_hash, i, worker, hentry) |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 281 | |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 282 | /** |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 283 | * for_each_pool - iterate through all worker_pools in the system |
| 284 | * @pool: iteration cursor |
| 285 | * @id: integer used for iteration |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 286 | * |
| 287 | * This must be called either with workqueue_lock held or sched RCU read |
| 288 | * locked. If the pool needs to be used beyond the locking in effect, the |
| 289 | * caller is responsible for guaranteeing that the pool stays online. |
| 290 | * |
| 291 | * The if/else clause exists only for the lockdep assertion and can be |
| 292 | * ignored. |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 293 | */ |
| 294 | #define for_each_pool(pool, id) \ |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 295 | idr_for_each_entry(&worker_pool_idr, pool, id) \ |
| 296 | if (({ assert_rcu_or_wq_lock(); false; })) { } \ |
| 297 | else |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 298 | |
| 299 | /** |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 300 | * for_each_pwq - iterate through all pool_workqueues of the specified workqueue |
| 301 | * @pwq: iteration cursor |
| 302 | * @wq: the target workqueue |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 303 | * |
| 304 | * This must be called either with workqueue_lock held or sched RCU read |
| 305 | * locked. If the pwq needs to be used beyond the locking in effect, the |
| 306 | * caller is responsible for guaranteeing that the pwq stays online. |
| 307 | * |
| 308 | * The if/else clause exists only for the lockdep assertion and can be |
| 309 | * ignored. |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 310 | */ |
| 311 | #define for_each_pwq(pwq, wq) \ |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 312 | list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ |
| 313 | if (({ assert_rcu_or_wq_lock(); false; })) { } \ |
| 314 | else |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 315 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 316 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
| 317 | |
| 318 | static struct debug_obj_descr work_debug_descr; |
| 319 | |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 320 | static void *work_debug_hint(void *addr) |
| 321 | { |
| 322 | return ((struct work_struct *) addr)->func; |
| 323 | } |
| 324 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 325 | /* |
| 326 | * fixup_init is called when: |
| 327 | * - an active object is initialized |
| 328 | */ |
| 329 | static int work_fixup_init(void *addr, enum debug_obj_state state) |
| 330 | { |
| 331 | struct work_struct *work = addr; |
| 332 | |
| 333 | switch (state) { |
| 334 | case ODEBUG_STATE_ACTIVE: |
| 335 | cancel_work_sync(work); |
| 336 | debug_object_init(work, &work_debug_descr); |
| 337 | return 1; |
| 338 | default: |
| 339 | return 0; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * fixup_activate is called when: |
| 345 | * - an active object is activated |
| 346 | * - an unknown object is activated (might be a statically initialized object) |
| 347 | */ |
| 348 | static int work_fixup_activate(void *addr, enum debug_obj_state state) |
| 349 | { |
| 350 | struct work_struct *work = addr; |
| 351 | |
| 352 | switch (state) { |
| 353 | |
| 354 | case ODEBUG_STATE_NOTAVAILABLE: |
| 355 | /* |
| 356 | * This is not really a fixup. The work struct was |
| 357 | * statically initialized. We just make sure that it |
| 358 | * is tracked in the object tracker. |
| 359 | */ |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 360 | if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 361 | debug_object_init(work, &work_debug_descr); |
| 362 | debug_object_activate(work, &work_debug_descr); |
| 363 | return 0; |
| 364 | } |
| 365 | WARN_ON_ONCE(1); |
| 366 | return 0; |
| 367 | |
| 368 | case ODEBUG_STATE_ACTIVE: |
| 369 | WARN_ON(1); |
| 370 | |
| 371 | default: |
| 372 | return 0; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * fixup_free is called when: |
| 378 | * - an active object is freed |
| 379 | */ |
| 380 | static int work_fixup_free(void *addr, enum debug_obj_state state) |
| 381 | { |
| 382 | struct work_struct *work = addr; |
| 383 | |
| 384 | switch (state) { |
| 385 | case ODEBUG_STATE_ACTIVE: |
| 386 | cancel_work_sync(work); |
| 387 | debug_object_free(work, &work_debug_descr); |
| 388 | return 1; |
| 389 | default: |
| 390 | return 0; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | static struct debug_obj_descr work_debug_descr = { |
| 395 | .name = "work_struct", |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 396 | .debug_hint = work_debug_hint, |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 397 | .fixup_init = work_fixup_init, |
| 398 | .fixup_activate = work_fixup_activate, |
| 399 | .fixup_free = work_fixup_free, |
| 400 | }; |
| 401 | |
| 402 | static inline void debug_work_activate(struct work_struct *work) |
| 403 | { |
| 404 | debug_object_activate(work, &work_debug_descr); |
| 405 | } |
| 406 | |
| 407 | static inline void debug_work_deactivate(struct work_struct *work) |
| 408 | { |
| 409 | debug_object_deactivate(work, &work_debug_descr); |
| 410 | } |
| 411 | |
| 412 | void __init_work(struct work_struct *work, int onstack) |
| 413 | { |
| 414 | if (onstack) |
| 415 | debug_object_init_on_stack(work, &work_debug_descr); |
| 416 | else |
| 417 | debug_object_init(work, &work_debug_descr); |
| 418 | } |
| 419 | EXPORT_SYMBOL_GPL(__init_work); |
| 420 | |
| 421 | void destroy_work_on_stack(struct work_struct *work) |
| 422 | { |
| 423 | debug_object_free(work, &work_debug_descr); |
| 424 | } |
| 425 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); |
| 426 | |
| 427 | #else |
| 428 | static inline void debug_work_activate(struct work_struct *work) { } |
| 429 | static inline void debug_work_deactivate(struct work_struct *work) { } |
| 430 | #endif |
| 431 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 432 | /* Serializes the accesses to the list of workqueues. */ |
| 433 | static DEFINE_SPINLOCK(workqueue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | static LIST_HEAD(workqueues); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 435 | static bool workqueue_freezing; /* W: have wqs started freezing? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 437 | /* |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 438 | * The CPU and unbound standard worker pools. The unbound ones have |
| 439 | * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set. |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 440 | */ |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 441 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 442 | cpu_worker_pools); |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 443 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 444 | /* |
| 445 | * idr of all pools. Modifications are protected by workqueue_lock. Read |
| 446 | * accesses are protected by sched-RCU protected. |
| 447 | */ |
Tejun Heo | 9daf9e6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 448 | static DEFINE_IDR(worker_pool_idr); |
| 449 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 450 | static int worker_thread(void *__worker); |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 451 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
| 452 | const struct workqueue_attrs *from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Tejun Heo | 9daf9e6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 454 | /* allocate ID and assign it to @pool */ |
| 455 | static int worker_pool_assign_id(struct worker_pool *pool) |
| 456 | { |
| 457 | int ret; |
| 458 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 459 | do { |
| 460 | if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL)) |
| 461 | return -ENOMEM; |
| 462 | |
| 463 | spin_lock_irq(&workqueue_lock); |
| 464 | ret = idr_get_new(&worker_pool_idr, pool, &pool->id); |
| 465 | spin_unlock_irq(&workqueue_lock); |
| 466 | } while (ret == -EAGAIN); |
Tejun Heo | 9daf9e6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 471 | /** |
| 472 | * first_pwq - return the first pool_workqueue of the specified workqueue |
| 473 | * @wq: the target workqueue |
| 474 | * |
| 475 | * This must be called either with workqueue_lock held or sched RCU read |
| 476 | * locked. If the pwq needs to be used beyond the locking in effect, the |
| 477 | * caller is responsible for guaranteeing that the pwq stays online. |
| 478 | */ |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 479 | static struct pool_workqueue *first_pwq(struct workqueue_struct *wq) |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 480 | { |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 481 | assert_rcu_or_wq_lock(); |
| 482 | return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue, |
| 483 | pwqs_node); |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 486 | static unsigned int work_color_to_flags(int color) |
| 487 | { |
| 488 | return color << WORK_STRUCT_COLOR_SHIFT; |
| 489 | } |
| 490 | |
| 491 | static int get_work_color(struct work_struct *work) |
| 492 | { |
| 493 | return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & |
| 494 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); |
| 495 | } |
| 496 | |
| 497 | static int work_next_color(int color) |
| 498 | { |
| 499 | return (color + 1) % WORK_NR_COLORS; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 500 | } |
| 501 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 502 | /* |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 503 | * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data |
| 504 | * contain the pointer to the queued pwq. Once execution starts, the flag |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 505 | * is cleared and the high bits contain OFFQ flags and pool ID. |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 506 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 507 | * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() |
| 508 | * and clear_work_data() can be used to set the pwq, pool or clear |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 509 | * work->data. These functions should only be called while the work is |
| 510 | * owned - ie. while the PENDING bit is set. |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 511 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 512 | * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 513 | * corresponding to a work. Pool is available once the work has been |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 514 | * queued anywhere after initialization until it is sync canceled. pwq is |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 515 | * available only while the work item is queued. |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 516 | * |
| 517 | * %WORK_OFFQ_CANCELING is used to mark a work item which is being |
| 518 | * canceled. While being canceled, a work item may have its PENDING set |
| 519 | * but stay off timer and worklist for arbitrarily long and nobody should |
| 520 | * try to steal the PENDING bit. |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 521 | */ |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 522 | static inline void set_work_data(struct work_struct *work, unsigned long data, |
| 523 | unsigned long flags) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 524 | { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 525 | WARN_ON_ONCE(!work_pending(work)); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 526 | atomic_long_set(&work->data, data | flags | work_static(work)); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 527 | } |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 528 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 529 | static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 530 | unsigned long extra_flags) |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 531 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 532 | set_work_data(work, (unsigned long)pwq, |
| 533 | WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Lai Jiangshan | 4468a00 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 536 | static void set_work_pool_and_keep_pending(struct work_struct *work, |
| 537 | int pool_id) |
| 538 | { |
| 539 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, |
| 540 | WORK_STRUCT_PENDING); |
| 541 | } |
| 542 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 543 | static void set_work_pool_and_clear_pending(struct work_struct *work, |
| 544 | int pool_id) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 545 | { |
Tejun Heo | 23657bb | 2012-08-13 17:08:19 -0700 | [diff] [blame] | 546 | /* |
| 547 | * The following wmb is paired with the implied mb in |
| 548 | * test_and_set_bit(PENDING) and ensures all updates to @work made |
| 549 | * here are visible to and precede any updates by the next PENDING |
| 550 | * owner. |
| 551 | */ |
| 552 | smp_wmb(); |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 553 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static void clear_work_data(struct work_struct *work) |
| 557 | { |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 558 | smp_wmb(); /* see set_work_pool_and_clear_pending() */ |
| 559 | set_work_data(work, WORK_STRUCT_NO_POOL, 0); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 562 | static struct pool_workqueue *get_work_pwq(struct work_struct *work) |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 563 | { |
Tejun Heo | e120153 | 2010-07-22 14:14:25 +0200 | [diff] [blame] | 564 | unsigned long data = atomic_long_read(&work->data); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 565 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 566 | if (data & WORK_STRUCT_PWQ) |
Tejun Heo | e120153 | 2010-07-22 14:14:25 +0200 | [diff] [blame] | 567 | return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); |
| 568 | else |
| 569 | return NULL; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 572 | /** |
| 573 | * get_work_pool - return the worker_pool a given work was associated with |
| 574 | * @work: the work item of interest |
| 575 | * |
| 576 | * Return the worker_pool @work was last associated with. %NULL if none. |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 577 | * |
| 578 | * Pools are created and destroyed under workqueue_lock, and allows read |
| 579 | * access under sched-RCU read lock. As such, this function should be |
| 580 | * called under workqueue_lock or with preemption disabled. |
| 581 | * |
| 582 | * All fields of the returned pool are accessible as long as the above |
| 583 | * mentioned locking is in effect. If the returned pool needs to be used |
| 584 | * beyond the critical section, the caller is responsible for ensuring the |
| 585 | * returned pool is and stays online. |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 586 | */ |
| 587 | static struct worker_pool *get_work_pool(struct work_struct *work) |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 588 | { |
Tejun Heo | e120153 | 2010-07-22 14:14:25 +0200 | [diff] [blame] | 589 | unsigned long data = atomic_long_read(&work->data); |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 590 | int pool_id; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 591 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 592 | assert_rcu_or_wq_lock(); |
| 593 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 594 | if (data & WORK_STRUCT_PWQ) |
| 595 | return ((struct pool_workqueue *) |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 596 | (data & WORK_STRUCT_WQ_DATA_MASK))->pool; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 597 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 598 | pool_id = data >> WORK_OFFQ_POOL_SHIFT; |
| 599 | if (pool_id == WORK_OFFQ_POOL_NONE) |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 600 | return NULL; |
| 601 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 602 | return idr_find(&worker_pool_idr, pool_id); |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
| 606 | * get_work_pool_id - return the worker pool ID a given work is associated with |
| 607 | * @work: the work item of interest |
| 608 | * |
| 609 | * Return the worker_pool ID @work was last associated with. |
| 610 | * %WORK_OFFQ_POOL_NONE if none. |
| 611 | */ |
| 612 | static int get_work_pool_id(struct work_struct *work) |
| 613 | { |
Lai Jiangshan | 54d5b7d | 2013-02-07 13:14:20 -0800 | [diff] [blame] | 614 | unsigned long data = atomic_long_read(&work->data); |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 615 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 616 | if (data & WORK_STRUCT_PWQ) |
| 617 | return ((struct pool_workqueue *) |
Lai Jiangshan | 54d5b7d | 2013-02-07 13:14:20 -0800 | [diff] [blame] | 618 | (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; |
| 619 | |
| 620 | return data >> WORK_OFFQ_POOL_SHIFT; |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 623 | static void mark_work_canceling(struct work_struct *work) |
| 624 | { |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 625 | unsigned long pool_id = get_work_pool_id(work); |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 626 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 627 | pool_id <<= WORK_OFFQ_POOL_SHIFT; |
| 628 | set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static bool work_is_canceling(struct work_struct *work) |
| 632 | { |
| 633 | unsigned long data = atomic_long_read(&work->data); |
| 634 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 635 | return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 636 | } |
| 637 | |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 638 | /* |
Tejun Heo | 3270476 | 2012-07-13 22:16:45 -0700 | [diff] [blame] | 639 | * Policy functions. These define the policies on how the global worker |
| 640 | * pools are managed. Unless noted otherwise, these functions assume that |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 641 | * they're being called with pool->lock held. |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 642 | */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 643 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 644 | static bool __need_more_worker(struct worker_pool *pool) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 645 | { |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 646 | return !atomic_read(&pool->nr_running); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 649 | /* |
| 650 | * Need to wake up a worker? Called from anything but currently |
| 651 | * running workers. |
Tejun Heo | 974271c | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 652 | * |
| 653 | * Note that, because unbound workers never contribute to nr_running, this |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 654 | * function will always return %true for unbound pools as long as the |
Tejun Heo | 974271c | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 655 | * worklist isn't empty. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 656 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 657 | static bool need_more_worker(struct worker_pool *pool) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 658 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 659 | return !list_empty(&pool->worklist) && __need_more_worker(pool); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 662 | /* Can I start working? Called from busy but !running workers. */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 663 | static bool may_start_working(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 664 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 665 | return pool->nr_idle; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* Do I need to keep working? Called from currently running workers. */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 669 | static bool keep_working(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 670 | { |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 671 | return !list_empty(&pool->worklist) && |
| 672 | atomic_read(&pool->nr_running) <= 1; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Do we need a new worker? Called from manager. */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 676 | static bool need_to_create_worker(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 677 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 678 | return need_more_worker(pool) && !may_start_working(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* Do I need to be the manager? */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 682 | static bool need_to_manage_workers(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 683 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 684 | return need_to_create_worker(pool) || |
Tejun Heo | 11ebea5 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 685 | (pool->flags & POOL_MANAGE_WORKERS); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Do we have too many workers and should some go away? */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 689 | static bool too_many_workers(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 690 | { |
Tejun Heo | 34a06bd | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 691 | bool managing = mutex_is_locked(&pool->manager_arb); |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 692 | int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ |
| 693 | int nr_busy = pool->nr_workers - nr_idle; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 694 | |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 695 | /* |
| 696 | * nr_idle and idle_list may disagree if idle rebinding is in |
| 697 | * progress. Never return %true if idle_list is empty. |
| 698 | */ |
| 699 | if (list_empty(&pool->idle_list)) |
| 700 | return false; |
| 701 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 702 | return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Wake up functions. |
| 707 | */ |
| 708 | |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 709 | /* Return the first worker. Safe with preemption disabled */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 710 | static struct worker *first_worker(struct worker_pool *pool) |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 711 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 712 | if (unlikely(list_empty(&pool->idle_list))) |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 713 | return NULL; |
| 714 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 715 | return list_first_entry(&pool->idle_list, struct worker, entry); |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * wake_up_worker - wake up an idle worker |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 720 | * @pool: worker pool to wake worker from |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 721 | * |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 722 | * Wake up the first idle worker of @pool. |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 723 | * |
| 724 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 725 | * spin_lock_irq(pool->lock). |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 726 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 727 | static void wake_up_worker(struct worker_pool *pool) |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 728 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 729 | struct worker *worker = first_worker(pool); |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 730 | |
| 731 | if (likely(worker)) |
| 732 | wake_up_process(worker->task); |
| 733 | } |
| 734 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 735 | /** |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 736 | * wq_worker_waking_up - a worker is waking up |
| 737 | * @task: task waking up |
| 738 | * @cpu: CPU @task is waking up to |
| 739 | * |
| 740 | * This function is called during try_to_wake_up() when a worker is |
| 741 | * being awoken. |
| 742 | * |
| 743 | * CONTEXT: |
| 744 | * spin_lock_irq(rq->lock) |
| 745 | */ |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 746 | void wq_worker_waking_up(struct task_struct *task, int cpu) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 747 | { |
| 748 | struct worker *worker = kthread_data(task); |
| 749 | |
Joonsoo Kim | 3657600 | 2012-10-26 23:03:49 +0900 | [diff] [blame] | 750 | if (!(worker->flags & WORKER_NOT_RUNNING)) { |
Tejun Heo | ec22ca5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 751 | WARN_ON_ONCE(worker->pool->cpu != cpu); |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 752 | atomic_inc(&worker->pool->nr_running); |
Joonsoo Kim | 3657600 | 2012-10-26 23:03:49 +0900 | [diff] [blame] | 753 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| 757 | * wq_worker_sleeping - a worker is going to sleep |
| 758 | * @task: task going to sleep |
| 759 | * @cpu: CPU in question, must be the current CPU number |
| 760 | * |
| 761 | * This function is called during schedule() when a busy worker is |
| 762 | * going to sleep. Worker on the same cpu can be woken up by |
| 763 | * returning pointer to its task. |
| 764 | * |
| 765 | * CONTEXT: |
| 766 | * spin_lock_irq(rq->lock) |
| 767 | * |
| 768 | * RETURNS: |
| 769 | * Worker task on @cpu to wake up, %NULL if none. |
| 770 | */ |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 771 | struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 772 | { |
| 773 | struct worker *worker = kthread_data(task), *to_wakeup = NULL; |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 774 | struct worker_pool *pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 775 | |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 776 | /* |
| 777 | * Rescuers, which may not have all the fields set up like normal |
| 778 | * workers, also reach here, let's not access anything before |
| 779 | * checking NOT_RUNNING. |
| 780 | */ |
Steven Rostedt | 2d64672 | 2010-12-03 23:12:33 -0500 | [diff] [blame] | 781 | if (worker->flags & WORKER_NOT_RUNNING) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 782 | return NULL; |
| 783 | |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 784 | pool = worker->pool; |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 785 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 786 | /* this can only happen on the local cpu */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 787 | if (WARN_ON_ONCE(cpu != raw_smp_processor_id())) |
| 788 | return NULL; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * The counterpart of the following dec_and_test, implied mb, |
| 792 | * worklist not empty test sequence is in insert_work(). |
| 793 | * Please read comment there. |
| 794 | * |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 795 | * NOT_RUNNING is clear. This means that we're bound to and |
| 796 | * running on the local cpu w/ rq lock held and preemption |
| 797 | * disabled, which in turn means that none else could be |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 798 | * manipulating idle_list, so dereferencing idle_list without pool |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 799 | * lock is safe. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 800 | */ |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 801 | if (atomic_dec_and_test(&pool->nr_running) && |
| 802 | !list_empty(&pool->worklist)) |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 803 | to_wakeup = first_worker(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 804 | return to_wakeup ? to_wakeup->task : NULL; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * worker_set_flags - set worker flags and adjust nr_running accordingly |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 809 | * @worker: self |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 810 | * @flags: flags to set |
| 811 | * @wakeup: wakeup an idle worker if necessary |
| 812 | * |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 813 | * Set @flags in @worker->flags and adjust nr_running accordingly. If |
| 814 | * nr_running becomes zero and @wakeup is %true, an idle worker is |
| 815 | * woken up. |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 816 | * |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 817 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 818 | * spin_lock_irq(pool->lock) |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 819 | */ |
| 820 | static inline void worker_set_flags(struct worker *worker, unsigned int flags, |
| 821 | bool wakeup) |
| 822 | { |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 823 | struct worker_pool *pool = worker->pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 824 | |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 825 | WARN_ON_ONCE(worker->task != current); |
| 826 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 827 | /* |
| 828 | * If transitioning into NOT_RUNNING, adjust nr_running and |
| 829 | * wake up an idle worker as necessary if requested by |
| 830 | * @wakeup. |
| 831 | */ |
| 832 | if ((flags & WORKER_NOT_RUNNING) && |
| 833 | !(worker->flags & WORKER_NOT_RUNNING)) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 834 | if (wakeup) { |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 835 | if (atomic_dec_and_test(&pool->nr_running) && |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 836 | !list_empty(&pool->worklist)) |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 837 | wake_up_worker(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 838 | } else |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 839 | atomic_dec(&pool->nr_running); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 842 | worker->flags |= flags; |
| 843 | } |
| 844 | |
| 845 | /** |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 846 | * worker_clr_flags - clear worker flags and adjust nr_running accordingly |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 847 | * @worker: self |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 848 | * @flags: flags to clear |
| 849 | * |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 850 | * Clear @flags in @worker->flags and adjust nr_running accordingly. |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 851 | * |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 852 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 853 | * spin_lock_irq(pool->lock) |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 854 | */ |
| 855 | static inline void worker_clr_flags(struct worker *worker, unsigned int flags) |
| 856 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 857 | struct worker_pool *pool = worker->pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 858 | unsigned int oflags = worker->flags; |
| 859 | |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 860 | WARN_ON_ONCE(worker->task != current); |
| 861 | |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 862 | worker->flags &= ~flags; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 863 | |
Tejun Heo | 42c025f | 2011-01-11 15:58:49 +0100 | [diff] [blame] | 864 | /* |
| 865 | * If transitioning out of NOT_RUNNING, increment nr_running. Note |
| 866 | * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask |
| 867 | * of multiple flags, not a single flag. |
| 868 | */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 869 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) |
| 870 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 871 | atomic_inc(&pool->nr_running); |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /** |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 875 | * find_worker_executing_work - find worker which is executing a work |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 876 | * @pool: pool of interest |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 877 | * @work: work to find worker for |
| 878 | * |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 879 | * Find a worker which is executing @work on @pool by searching |
| 880 | * @pool->busy_hash which is keyed by the address of @work. For a worker |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 881 | * to match, its current execution should match the address of @work and |
| 882 | * its work function. This is to avoid unwanted dependency between |
| 883 | * unrelated work executions through a work item being recycled while still |
| 884 | * being executed. |
| 885 | * |
| 886 | * This is a bit tricky. A work item may be freed once its execution |
| 887 | * starts and nothing prevents the freed area from being recycled for |
| 888 | * another work item. If the same work item address ends up being reused |
| 889 | * before the original execution finishes, workqueue will identify the |
| 890 | * recycled work item as currently executing and make it wait until the |
| 891 | * current execution finishes, introducing an unwanted dependency. |
| 892 | * |
| 893 | * This function checks the work item address, work function and workqueue |
| 894 | * to avoid false positives. Note that this isn't complete as one may |
| 895 | * construct a work function which can introduce dependency onto itself |
| 896 | * through a recycled work item. Well, if somebody wants to shoot oneself |
| 897 | * in the foot that badly, there's only so much we can do, and if such |
| 898 | * deadlock actually occurs, it should be easy to locate the culprit work |
| 899 | * function. |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 900 | * |
| 901 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 902 | * spin_lock_irq(pool->lock). |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 903 | * |
| 904 | * RETURNS: |
| 905 | * Pointer to worker which is executing @work if found, NULL |
| 906 | * otherwise. |
| 907 | */ |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 908 | static struct worker *find_worker_executing_work(struct worker_pool *pool, |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 909 | struct work_struct *work) |
| 910 | { |
Sasha Levin | 42f8570 | 2012-12-17 10:01:23 -0500 | [diff] [blame] | 911 | struct worker *worker; |
Sasha Levin | 42f8570 | 2012-12-17 10:01:23 -0500 | [diff] [blame] | 912 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 913 | hash_for_each_possible(pool->busy_hash, worker, hentry, |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 914 | (unsigned long)work) |
| 915 | if (worker->current_work == work && |
| 916 | worker->current_func == work->func) |
Sasha Levin | 42f8570 | 2012-12-17 10:01:23 -0500 | [diff] [blame] | 917 | return worker; |
| 918 | |
| 919 | return NULL; |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 923 | * move_linked_works - move linked works to a list |
| 924 | * @work: start of series of works to be scheduled |
| 925 | * @head: target list to append @work to |
| 926 | * @nextp: out paramter for nested worklist walking |
| 927 | * |
| 928 | * Schedule linked works starting from @work to @head. Work series to |
| 929 | * be scheduled starts at @work and includes any consecutive work with |
| 930 | * WORK_STRUCT_LINKED set in its predecessor. |
| 931 | * |
| 932 | * If @nextp is not NULL, it's updated to point to the next work of |
| 933 | * the last scheduled work. This allows move_linked_works() to be |
| 934 | * nested inside outer list_for_each_entry_safe(). |
| 935 | * |
| 936 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 937 | * spin_lock_irq(pool->lock). |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 938 | */ |
| 939 | static void move_linked_works(struct work_struct *work, struct list_head *head, |
| 940 | struct work_struct **nextp) |
| 941 | { |
| 942 | struct work_struct *n; |
| 943 | |
| 944 | /* |
| 945 | * Linked worklist will always end before the end of the list, |
| 946 | * use NULL for list head. |
| 947 | */ |
| 948 | list_for_each_entry_safe_from(work, n, NULL, entry) { |
| 949 | list_move_tail(&work->entry, head); |
| 950 | if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) |
| 951 | break; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * If we're already inside safe list traversal and have moved |
| 956 | * multiple works to the scheduled queue, the next position |
| 957 | * needs to be updated. |
| 958 | */ |
| 959 | if (nextp) |
| 960 | *nextp = n; |
| 961 | } |
| 962 | |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 963 | /** |
| 964 | * get_pwq - get an extra reference on the specified pool_workqueue |
| 965 | * @pwq: pool_workqueue to get |
| 966 | * |
| 967 | * Obtain an extra reference on @pwq. The caller should guarantee that |
| 968 | * @pwq has positive refcnt and be holding the matching pool->lock. |
| 969 | */ |
| 970 | static void get_pwq(struct pool_workqueue *pwq) |
| 971 | { |
| 972 | lockdep_assert_held(&pwq->pool->lock); |
| 973 | WARN_ON_ONCE(pwq->refcnt <= 0); |
| 974 | pwq->refcnt++; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * put_pwq - put a pool_workqueue reference |
| 979 | * @pwq: pool_workqueue to put |
| 980 | * |
| 981 | * Drop a reference of @pwq. If its refcnt reaches zero, schedule its |
| 982 | * destruction. The caller should be holding the matching pool->lock. |
| 983 | */ |
| 984 | static void put_pwq(struct pool_workqueue *pwq) |
| 985 | { |
| 986 | lockdep_assert_held(&pwq->pool->lock); |
| 987 | if (likely(--pwq->refcnt)) |
| 988 | return; |
| 989 | if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) |
| 990 | return; |
| 991 | /* |
| 992 | * @pwq can't be released under pool->lock, bounce to |
| 993 | * pwq_unbound_release_workfn(). This never recurses on the same |
| 994 | * pool->lock as this path is taken only for unbound workqueues and |
| 995 | * the release work item is scheduled on a per-cpu workqueue. To |
| 996 | * avoid lockdep warning, unbound pool->locks are given lockdep |
| 997 | * subclass of 1 in get_unbound_pool(). |
| 998 | */ |
| 999 | schedule_work(&pwq->unbound_release_work); |
| 1000 | } |
| 1001 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1002 | static void pwq_activate_delayed_work(struct work_struct *work) |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1003 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1004 | struct pool_workqueue *pwq = get_work_pwq(work); |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1005 | |
| 1006 | trace_workqueue_activate_work(work); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1007 | move_linked_works(work, &pwq->pool->worklist, NULL); |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1008 | __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1009 | pwq->nr_active++; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1012 | static void pwq_activate_first_delayed(struct pool_workqueue *pwq) |
Lai Jiangshan | 3aa6249 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1013 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1014 | struct work_struct *work = list_first_entry(&pwq->delayed_works, |
Lai Jiangshan | 3aa6249 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1015 | struct work_struct, entry); |
| 1016 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1017 | pwq_activate_delayed_work(work); |
Lai Jiangshan | 3aa6249 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1020 | /** |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1021 | * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight |
| 1022 | * @pwq: pwq of interest |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1023 | * @color: color of work which left the queue |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1024 | * |
| 1025 | * A work either has completed or is removed from pending queue, |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1026 | * decrement nr_in_flight of its pwq and handle workqueue flushing. |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1027 | * |
| 1028 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1029 | * spin_lock_irq(pool->lock). |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1030 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1031 | static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1032 | { |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1033 | /* uncolored work items don't participate in flushing or nr_active */ |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1034 | if (color == WORK_NO_COLOR) |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1035 | goto out_put; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1036 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1037 | pwq->nr_in_flight[color]--; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1038 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1039 | pwq->nr_active--; |
| 1040 | if (!list_empty(&pwq->delayed_works)) { |
Lai Jiangshan | b3f9f40 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1041 | /* one down, submit a delayed one */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1042 | if (pwq->nr_active < pwq->max_active) |
| 1043 | pwq_activate_first_delayed(pwq); |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /* is flush in progress and are we at the flushing tip? */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1047 | if (likely(pwq->flush_color != color)) |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1048 | goto out_put; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1049 | |
| 1050 | /* are there still in-flight works? */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1051 | if (pwq->nr_in_flight[color]) |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1052 | goto out_put; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1053 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1054 | /* this pwq is done, clear flush_color */ |
| 1055 | pwq->flush_color = -1; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1058 | * If this was the last pwq, wake up the first flusher. It |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1059 | * will handle the rest. |
| 1060 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1061 | if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) |
| 1062 | complete(&pwq->wq->first_flusher->done); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1063 | out_put: |
| 1064 | put_pwq(pwq); |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1067 | /** |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1068 | * try_to_grab_pending - steal work item from worklist and disable irq |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1069 | * @work: work item to steal |
| 1070 | * @is_dwork: @work is a delayed_work |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1071 | * @flags: place to store irq state |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1072 | * |
| 1073 | * Try to grab PENDING bit of @work. This function can handle @work in any |
| 1074 | * stable state - idle, on timer or on worklist. Return values are |
| 1075 | * |
| 1076 | * 1 if @work was pending and we successfully stole PENDING |
| 1077 | * 0 if @work was idle and we claimed PENDING |
| 1078 | * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1079 | * -ENOENT if someone else is canceling @work, this state may persist |
| 1080 | * for arbitrarily long |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1081 | * |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1082 | * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting |
Tejun Heo | e0aecdd | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 1083 | * interrupted while holding PENDING and @work off queue, irq must be |
| 1084 | * disabled on entry. This, combined with delayed_work->timer being |
| 1085 | * irqsafe, ensures that we return -EAGAIN for finite short period of time. |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1086 | * |
| 1087 | * On successful return, >= 0, irq is disabled and the caller is |
| 1088 | * responsible for releasing it using local_irq_restore(*@flags). |
| 1089 | * |
Tejun Heo | e0aecdd | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 1090 | * This function is safe to call from any context including IRQ handler. |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1091 | */ |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1092 | static int try_to_grab_pending(struct work_struct *work, bool is_dwork, |
| 1093 | unsigned long *flags) |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1094 | { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1095 | struct worker_pool *pool; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1096 | struct pool_workqueue *pwq; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1097 | |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1098 | local_irq_save(*flags); |
| 1099 | |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1100 | /* try to steal the timer if it exists */ |
| 1101 | if (is_dwork) { |
| 1102 | struct delayed_work *dwork = to_delayed_work(work); |
| 1103 | |
Tejun Heo | e0aecdd | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 1104 | /* |
| 1105 | * dwork->timer is irqsafe. If del_timer() fails, it's |
| 1106 | * guaranteed that the timer is not queued anywhere and not |
| 1107 | * running on the local CPU. |
| 1108 | */ |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1109 | if (likely(del_timer(&dwork->timer))) |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
| 1113 | /* try to claim PENDING the normal way */ |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1114 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
| 1115 | return 0; |
| 1116 | |
| 1117 | /* |
| 1118 | * The queueing is in progress, or it is already queued. Try to |
| 1119 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. |
| 1120 | */ |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1121 | pool = get_work_pool(work); |
| 1122 | if (!pool) |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1123 | goto fail; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1124 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1125 | spin_lock(&pool->lock); |
Lai Jiangshan | 0b3dae6 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1126 | /* |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1127 | * work->data is guaranteed to point to pwq only while the work |
| 1128 | * item is queued on pwq->wq, and both updating work->data to point |
| 1129 | * to pwq on queueing and to pool on dequeueing are done under |
| 1130 | * pwq->pool->lock. This in turn guarantees that, if work->data |
| 1131 | * points to pwq which is associated with a locked pool, the work |
Lai Jiangshan | 0b3dae6 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1132 | * item is currently queued on that pool. |
| 1133 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1134 | pwq = get_work_pwq(work); |
| 1135 | if (pwq && pwq->pool == pool) { |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1136 | debug_work_deactivate(work); |
Lai Jiangshan | 3aa6249 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1137 | |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1138 | /* |
| 1139 | * A delayed work item cannot be grabbed directly because |
| 1140 | * it might have linked NO_COLOR work items which, if left |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1141 | * on the delayed_list, will confuse pwq->nr_active |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1142 | * management later on and cause stall. Make sure the work |
| 1143 | * item is activated before grabbing. |
| 1144 | */ |
| 1145 | if (*work_data_bits(work) & WORK_STRUCT_DELAYED) |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1146 | pwq_activate_delayed_work(work); |
Lai Jiangshan | 3aa6249 | 2012-09-18 10:40:00 -0700 | [diff] [blame] | 1147 | |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1148 | list_del_init(&work->entry); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1149 | pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work)); |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1150 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1151 | /* work->data points to pwq iff queued, point to pool */ |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1152 | set_work_pool_and_keep_pending(work, pool->id); |
Lai Jiangshan | 4468a00 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1153 | |
Tejun Heo | 1606283 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1154 | spin_unlock(&pool->lock); |
| 1155 | return 1; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1156 | } |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1157 | spin_unlock(&pool->lock); |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1158 | fail: |
| 1159 | local_irq_restore(*flags); |
| 1160 | if (work_is_canceling(work)) |
| 1161 | return -ENOENT; |
| 1162 | cpu_relax(); |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1163 | return -EAGAIN; |
Tejun Heo | bf4ede0 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /** |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1167 | * insert_work - insert a work into a pool |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1168 | * @pwq: pwq @work belongs to |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1169 | * @work: work to insert |
| 1170 | * @head: insertion point |
| 1171 | * @extra_flags: extra WORK_STRUCT_* flags to set |
| 1172 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1173 | * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1174 | * work_struct flags. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1175 | * |
| 1176 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1177 | * spin_lock_irq(pool->lock). |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1178 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1179 | static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, |
| 1180 | struct list_head *head, unsigned int extra_flags) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1181 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1182 | struct worker_pool *pool = pwq->pool; |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 1183 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1184 | /* we own @work, set data and link */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1185 | set_work_pwq(work, pwq, extra_flags); |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 1186 | list_add_tail(&work->entry, head); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1187 | get_pwq(pwq); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1188 | |
| 1189 | /* |
| 1190 | * Ensure either worker_sched_deactivated() sees the above |
| 1191 | * list_add_tail() or we see zero nr_running to avoid workers |
| 1192 | * lying around lazily while there are works to be processed. |
| 1193 | */ |
| 1194 | smp_mb(); |
| 1195 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1196 | if (__need_more_worker(pool)) |
| 1197 | wake_up_worker(pool); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1200 | /* |
| 1201 | * Test whether @work is being queued from another work executing on the |
Tejun Heo | 8d03ecf | 2013-02-13 19:29:10 -0800 | [diff] [blame] | 1202 | * same workqueue. |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1203 | */ |
| 1204 | static bool is_chained_work(struct workqueue_struct *wq) |
| 1205 | { |
Tejun Heo | 8d03ecf | 2013-02-13 19:29:10 -0800 | [diff] [blame] | 1206 | struct worker *worker; |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1207 | |
Tejun Heo | 8d03ecf | 2013-02-13 19:29:10 -0800 | [diff] [blame] | 1208 | worker = current_wq_worker(); |
| 1209 | /* |
| 1210 | * Return %true iff I'm a worker execuing a work item on @wq. If |
| 1211 | * I'm @worker, it's safe to dereference it without locking. |
| 1212 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1213 | return worker && worker->current_pwq->wq == wq; |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1216 | static void __queue_work(int cpu, struct workqueue_struct *wq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | struct work_struct *work) |
| 1218 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1219 | struct pool_workqueue *pwq; |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1220 | struct worker_pool *last_pool; |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1221 | struct list_head *worklist; |
Tejun Heo | 8a2e8e5d | 2010-08-25 10:33:56 +0200 | [diff] [blame] | 1222 | unsigned int work_flags; |
Joonsoo Kim | b75cac9 | 2012-08-15 23:25:37 +0900 | [diff] [blame] | 1223 | unsigned int req_cpu = cpu; |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * While a work item is PENDING && off queue, a task trying to |
| 1227 | * steal the PENDING will busy-loop waiting for it to either get |
| 1228 | * queued or lose PENDING. Grabbing PENDING and queueing should |
| 1229 | * happen with IRQ disabled. |
| 1230 | */ |
| 1231 | WARN_ON_ONCE(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1233 | debug_work_activate(work); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1234 | |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1235 | /* if dying, only works from the same workqueue are allowed */ |
Tejun Heo | 618b01e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1236 | if (unlikely(wq->flags & __WQ_DRAINING) && |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 1237 | WARN_ON_ONCE(!is_chained_work(wq))) |
Tejun Heo | e41e704 | 2010-08-24 14:22:47 +0200 | [diff] [blame] | 1238 | return; |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1239 | retry: |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1240 | /* pwq which will be used unless @work is executing elsewhere */ |
Tejun Heo | c7fc77f | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 1241 | if (!(wq->flags & WQ_UNBOUND)) { |
Tejun Heo | 5746982 | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1242 | if (cpu == WORK_CPU_UNBOUND) |
Tejun Heo | c7fc77f | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 1243 | cpu = raw_smp_processor_id(); |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1244 | pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1245 | } else { |
| 1246 | pwq = first_pwq(wq); |
| 1247 | } |
Tejun Heo | dbf2576 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 1248 | |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1249 | /* |
| 1250 | * If @work was previously on a different pool, it might still be |
| 1251 | * running there, in which case the work needs to be queued on that |
| 1252 | * pool to guarantee non-reentrancy. |
| 1253 | */ |
| 1254 | last_pool = get_work_pool(work); |
| 1255 | if (last_pool && last_pool != pwq->pool) { |
| 1256 | struct worker *worker; |
Tejun Heo | 18aa9ef | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1257 | |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1258 | spin_lock(&last_pool->lock); |
Tejun Heo | 18aa9ef | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1259 | |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1260 | worker = find_worker_executing_work(last_pool, work); |
Tejun Heo | 18aa9ef | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1261 | |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1262 | if (worker && worker->current_pwq->wq == wq) { |
| 1263 | pwq = worker->current_pwq; |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1264 | } else { |
Tejun Heo | c917808 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1265 | /* meh... not running there, queue here */ |
| 1266 | spin_unlock(&last_pool->lock); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1267 | spin_lock(&pwq->pool->lock); |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1268 | } |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 1269 | } else { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1270 | spin_lock(&pwq->pool->lock); |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 1273 | /* |
| 1274 | * pwq is determined and locked. For unbound pools, we could have |
| 1275 | * raced with pwq release and it could already be dead. If its |
| 1276 | * refcnt is zero, repeat pwq selection. Note that pwqs never die |
| 1277 | * without another pwq replacing it as the first pwq or while a |
| 1278 | * work item is executing on it, so the retying is guaranteed to |
| 1279 | * make forward-progress. |
| 1280 | */ |
| 1281 | if (unlikely(!pwq->refcnt)) { |
| 1282 | if (wq->flags & WQ_UNBOUND) { |
| 1283 | spin_unlock(&pwq->pool->lock); |
| 1284 | cpu_relax(); |
| 1285 | goto retry; |
| 1286 | } |
| 1287 | /* oops */ |
| 1288 | WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", |
| 1289 | wq->name, cpu); |
| 1290 | } |
| 1291 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1292 | /* pwq determined, queue */ |
| 1293 | trace_workqueue_queue_work(req_cpu, pwq, work); |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1294 | |
Dan Carpenter | f5b2552 | 2012-04-13 22:06:58 +0300 | [diff] [blame] | 1295 | if (WARN_ON(!list_empty(&work->entry))) { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1296 | spin_unlock(&pwq->pool->lock); |
Dan Carpenter | f5b2552 | 2012-04-13 22:06:58 +0300 | [diff] [blame] | 1297 | return; |
| 1298 | } |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1299 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1300 | pwq->nr_in_flight[pwq->work_color]++; |
| 1301 | work_flags = work_color_to_flags(pwq->work_color); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1302 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1303 | if (likely(pwq->nr_active < pwq->max_active)) { |
Tejun Heo | cdadf00 | 2010-10-05 10:49:55 +0200 | [diff] [blame] | 1304 | trace_workqueue_activate_work(work); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1305 | pwq->nr_active++; |
| 1306 | worklist = &pwq->pool->worklist; |
Tejun Heo | 8a2e8e5d | 2010-08-25 10:33:56 +0200 | [diff] [blame] | 1307 | } else { |
| 1308 | work_flags |= WORK_STRUCT_DELAYED; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1309 | worklist = &pwq->delayed_works; |
Tejun Heo | 8a2e8e5d | 2010-08-25 10:33:56 +0200 | [diff] [blame] | 1310 | } |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1311 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1312 | insert_work(pwq, work, worklist, work_flags); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1313 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1314 | spin_unlock(&pwq->pool->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1317 | /** |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1318 | * queue_work_on - queue work on specific cpu |
| 1319 | * @cpu: CPU number to execute work on |
| 1320 | * @wq: workqueue to use |
| 1321 | * @work: work to queue |
| 1322 | * |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1323 | * Returns %false if @work was already on a queue, %true otherwise. |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1324 | * |
| 1325 | * We queue the work to a specific CPU, the caller must ensure it |
| 1326 | * can't go away. |
| 1327 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1328 | bool queue_work_on(int cpu, struct workqueue_struct *wq, |
| 1329 | struct work_struct *work) |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1330 | { |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1331 | bool ret = false; |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1332 | unsigned long flags; |
| 1333 | |
| 1334 | local_irq_save(flags); |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1335 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1336 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1337 | __queue_work(cpu, wq, work); |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1338 | ret = true; |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1339 | } |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1340 | |
| 1341 | local_irq_restore(flags); |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1342 | return ret; |
| 1343 | } |
| 1344 | EXPORT_SYMBOL_GPL(queue_work_on); |
| 1345 | |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1346 | /** |
| 1347 | * queue_work - queue work on a workqueue |
| 1348 | * @wq: workqueue to use |
| 1349 | * @work: work to queue |
| 1350 | * |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1351 | * Returns %false if @work was already on a queue, %true otherwise. |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1352 | * |
| 1353 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
| 1354 | * it can be processed by another CPU. |
| 1355 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1356 | bool queue_work(struct workqueue_struct *wq, struct work_struct *work) |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1357 | { |
Tejun Heo | 5746982 | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1358 | return queue_work_on(WORK_CPU_UNBOUND, wq, work); |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(queue_work); |
| 1361 | |
Tejun Heo | d8e794d | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1362 | void delayed_work_timer_fn(unsigned long __data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1364 | struct delayed_work *dwork = (struct delayed_work *)__data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Tejun Heo | e0aecdd | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 1366 | /* should have been called from irqsafe timer with irq already off */ |
Lai Jiangshan | 60c057b | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1367 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | } |
Konstantin Khlebnikov | 1438ade5 | 2013-01-24 16:36:31 +0400 | [diff] [blame] | 1369 | EXPORT_SYMBOL(delayed_work_timer_fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1371 | static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, |
| 1372 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | { |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1374 | struct timer_list *timer = &dwork->timer; |
| 1375 | struct work_struct *work = &dwork->work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1377 | WARN_ON_ONCE(timer->function != delayed_work_timer_fn || |
| 1378 | timer->data != (unsigned long)dwork); |
Tejun Heo | fc4b514 | 2012-12-04 07:40:39 -0800 | [diff] [blame] | 1379 | WARN_ON_ONCE(timer_pending(timer)); |
| 1380 | WARN_ON_ONCE(!list_empty(&work->entry)); |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1381 | |
Tejun Heo | 8852aac | 2012-12-01 16:23:42 -0800 | [diff] [blame] | 1382 | /* |
| 1383 | * If @delay is 0, queue @dwork->work immediately. This is for |
| 1384 | * both optimization and correctness. The earliest @timer can |
| 1385 | * expire is on the closest next tick and delayed_work users depend |
| 1386 | * on that there's no such delay when @delay is 0. |
| 1387 | */ |
| 1388 | if (!delay) { |
| 1389 | __queue_work(cpu, wq, &dwork->work); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1393 | timer_stats_timer_set_start_info(&dwork->timer); |
| 1394 | |
Lai Jiangshan | 60c057b | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 1395 | dwork->wq = wq; |
Tejun Heo | 1265057 | 2012-08-08 09:38:42 -0700 | [diff] [blame] | 1396 | dwork->cpu = cpu; |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1397 | timer->expires = jiffies + delay; |
| 1398 | |
| 1399 | if (unlikely(cpu != WORK_CPU_UNBOUND)) |
| 1400 | add_timer_on(timer, cpu); |
| 1401 | else |
| 1402 | add_timer(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1405 | /** |
| 1406 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 1407 | * @cpu: CPU number to execute work on |
| 1408 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 1409 | * @dwork: work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1410 | * @delay: number of jiffies to wait before queueing |
| 1411 | * |
Tejun Heo | 715f130 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1412 | * Returns %false if @work was already on a queue, %true otherwise. If |
| 1413 | * @delay is zero and @dwork is idle, it will be scheduled for immediate |
| 1414 | * execution. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1415 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1416 | bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
| 1417 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 1418 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1419 | struct work_struct *work = &dwork->work; |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1420 | bool ret = false; |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1421 | unsigned long flags; |
| 1422 | |
| 1423 | /* read the comment in __queue_work() */ |
| 1424 | local_irq_save(flags); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 1425 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1426 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
Tejun Heo | 7beb2ed | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1427 | __queue_delayed_work(cpu, wq, dwork, delay); |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1428 | ret = true; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 1429 | } |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1430 | |
| 1431 | local_irq_restore(flags); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 1432 | return ret; |
| 1433 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1434 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1436 | /** |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1437 | * queue_delayed_work - queue work on a workqueue after delay |
| 1438 | * @wq: workqueue to use |
| 1439 | * @dwork: delayable work to queue |
| 1440 | * @delay: number of jiffies to wait before queueing |
| 1441 | * |
Tejun Heo | 715f130 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 1442 | * Equivalent to queue_delayed_work_on() but tries to use the local CPU. |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1443 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1444 | bool queue_delayed_work(struct workqueue_struct *wq, |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1445 | struct delayed_work *dwork, unsigned long delay) |
| 1446 | { |
Tejun Heo | 5746982 | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 1447 | return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 1448 | } |
| 1449 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
| 1450 | |
| 1451 | /** |
Tejun Heo | 8376fe2 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 1452 | * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU |
| 1453 | * @cpu: CPU number to execute work on |
| 1454 | * @wq: workqueue to use |
| 1455 | * @dwork: work to queue |
| 1456 | * @delay: number of jiffies to wait before queueing |
| 1457 | * |
| 1458 | * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, |
| 1459 | * modify @dwork's timer so that it expires after @delay. If @delay is |
| 1460 | * zero, @work is guaranteed to be scheduled immediately regardless of its |
| 1461 | * current state. |
| 1462 | * |
| 1463 | * Returns %false if @dwork was idle and queued, %true if @dwork was |
| 1464 | * pending and its timer was modified. |
| 1465 | * |
Tejun Heo | e0aecdd | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 1466 | * This function is safe to call from any context including IRQ handler. |
Tejun Heo | 8376fe2 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 1467 | * See try_to_grab_pending() for details. |
| 1468 | */ |
| 1469 | bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, |
| 1470 | struct delayed_work *dwork, unsigned long delay) |
| 1471 | { |
| 1472 | unsigned long flags; |
| 1473 | int ret; |
| 1474 | |
| 1475 | do { |
| 1476 | ret = try_to_grab_pending(&dwork->work, true, &flags); |
| 1477 | } while (unlikely(ret == -EAGAIN)); |
| 1478 | |
| 1479 | if (likely(ret >= 0)) { |
| 1480 | __queue_delayed_work(cpu, wq, dwork, delay); |
| 1481 | local_irq_restore(flags); |
| 1482 | } |
| 1483 | |
| 1484 | /* -ENOENT from try_to_grab_pending() becomes %true */ |
| 1485 | return ret; |
| 1486 | } |
| 1487 | EXPORT_SYMBOL_GPL(mod_delayed_work_on); |
| 1488 | |
| 1489 | /** |
| 1490 | * mod_delayed_work - modify delay of or queue a delayed work |
| 1491 | * @wq: workqueue to use |
| 1492 | * @dwork: work to queue |
| 1493 | * @delay: number of jiffies to wait before queueing |
| 1494 | * |
| 1495 | * mod_delayed_work_on() on local CPU. |
| 1496 | */ |
| 1497 | bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, |
| 1498 | unsigned long delay) |
| 1499 | { |
| 1500 | return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); |
| 1501 | } |
| 1502 | EXPORT_SYMBOL_GPL(mod_delayed_work); |
| 1503 | |
| 1504 | /** |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1505 | * worker_enter_idle - enter idle state |
| 1506 | * @worker: worker which is entering idle state |
| 1507 | * |
| 1508 | * @worker is entering idle state. Update stats and idle timer if |
| 1509 | * necessary. |
| 1510 | * |
| 1511 | * LOCKING: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1512 | * spin_lock_irq(pool->lock). |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1513 | */ |
| 1514 | static void worker_enter_idle(struct worker *worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | { |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1516 | struct worker_pool *pool = worker->pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 1518 | if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || |
| 1519 | WARN_ON_ONCE(!list_empty(&worker->entry) && |
| 1520 | (worker->hentry.next || worker->hentry.pprev))) |
| 1521 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 1523 | /* can't use worker_set_flags(), also called from start_worker() */ |
| 1524 | worker->flags |= WORKER_IDLE; |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1525 | pool->nr_idle++; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1526 | worker->last_active = jiffies; |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 1527 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1528 | /* idle_list is LIFO */ |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1529 | list_add(&worker->entry, &pool->idle_list); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1530 | |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1531 | if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) |
| 1532 | mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 1533 | |
Tejun Heo | 544ecf3 | 2012-05-14 15:04:50 -0700 | [diff] [blame] | 1534 | /* |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1535 | * Sanity check nr_running. Because wq_unbind_fn() releases |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1536 | * pool->lock between setting %WORKER_UNBOUND and zapping |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1537 | * nr_running, the warning may trigger spuriously. Check iff |
| 1538 | * unbind is not in progress. |
Tejun Heo | 544ecf3 | 2012-05-14 15:04:50 -0700 | [diff] [blame] | 1539 | */ |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1540 | WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1541 | pool->nr_workers == pool->nr_idle && |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 1542 | atomic_read(&pool->nr_running)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1545 | /** |
| 1546 | * worker_leave_idle - leave idle state |
| 1547 | * @worker: worker which is leaving idle state |
| 1548 | * |
| 1549 | * @worker is leaving idle state. Update stats. |
| 1550 | * |
| 1551 | * LOCKING: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1552 | * spin_lock_irq(pool->lock). |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1553 | */ |
| 1554 | static void worker_leave_idle(struct worker *worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | { |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1556 | struct worker_pool *pool = worker->pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 1558 | if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) |
| 1559 | return; |
Tejun Heo | d302f01 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1560 | worker_clr_flags(worker, WORKER_IDLE); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1561 | pool->nr_idle--; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1562 | list_del_init(&worker->entry); |
| 1563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1565 | /** |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1566 | * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it |
| 1567 | * @pool: target worker_pool |
| 1568 | * |
| 1569 | * Bind %current to the cpu of @pool if it is associated and lock @pool. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1570 | * |
| 1571 | * Works which are scheduled while the cpu is online must at least be |
| 1572 | * scheduled to a worker which is bound to the cpu so that if they are |
| 1573 | * flushed from cpu callbacks while cpu is going down, they are |
| 1574 | * guaranteed to execute on the cpu. |
| 1575 | * |
Lai Jiangshan | f5faa07 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1576 | * This function is to be used by unbound workers and rescuers to bind |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1577 | * themselves to the target cpu and may race with cpu going down or |
| 1578 | * coming online. kthread_bind() can't be used because it may put the |
| 1579 | * worker to already dead cpu and set_cpus_allowed_ptr() can't be used |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1580 | * verbatim as it's best effort and blocking and pool may be |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1581 | * [dis]associated in the meantime. |
| 1582 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1583 | * This function tries set_cpus_allowed() and locks pool and verifies the |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1584 | * binding against %POOL_DISASSOCIATED which is set during |
Tejun Heo | f2d5a0e | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 1585 | * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker |
| 1586 | * enters idle state or fetches works without dropping lock, it can |
| 1587 | * guarantee the scheduling requirement described in the first paragraph. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1588 | * |
| 1589 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1590 | * Might sleep. Called without any lock but returns with pool->lock |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1591 | * held. |
| 1592 | * |
| 1593 | * RETURNS: |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1594 | * %true if the associated pool is online (@worker is successfully |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1595 | * bound), %false if offline. |
| 1596 | */ |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1597 | static bool worker_maybe_bind_and_lock(struct worker_pool *pool) |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1598 | __acquires(&pool->lock) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1599 | { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1600 | while (true) { |
| 1601 | /* |
| 1602 | * The following call may fail, succeed or succeed |
| 1603 | * without actually migrating the task to the cpu if |
| 1604 | * it races with cpu hotunplug operation. Verify |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1605 | * against POOL_DISASSOCIATED. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1606 | */ |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1607 | if (!(pool->flags & POOL_DISASSOCIATED)) |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1608 | set_cpus_allowed_ptr(current, pool->attrs->cpumask); |
Oleg Nesterov | 85f4186 | 2007-05-09 02:34:20 -0700 | [diff] [blame] | 1609 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1610 | spin_lock_irq(&pool->lock); |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1611 | if (pool->flags & POOL_DISASSOCIATED) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1612 | return false; |
Lai Jiangshan | f5faa07 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1613 | if (task_cpu(current) == pool->cpu && |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1614 | cpumask_equal(¤t->cpus_allowed, pool->attrs->cpumask)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1615 | return true; |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1616 | spin_unlock_irq(&pool->lock); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1617 | |
Tejun Heo | 5035b20 | 2011-04-29 18:08:37 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * We've raced with CPU hot[un]plug. Give it a breather |
| 1620 | * and retry migration. cond_resched() is required here; |
| 1621 | * otherwise, we might deadlock against cpu_stop trying to |
| 1622 | * bring down the CPU on non-preemptive kernel. |
| 1623 | */ |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1624 | cpu_relax(); |
Tejun Heo | 5035b20 | 2011-04-29 18:08:37 +0200 | [diff] [blame] | 1625 | cond_resched(); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /* |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1630 | * Rebind an idle @worker to its CPU. worker_thread() will test |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 1631 | * list_empty(@worker->entry) before leaving idle and call this function. |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1632 | */ |
| 1633 | static void idle_worker_rebind(struct worker *worker) |
| 1634 | { |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 1635 | /* CPU may go down again inbetween, clear UNBOUND only on success */ |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1636 | if (worker_maybe_bind_and_lock(worker->pool)) |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 1637 | worker_clr_flags(worker, WORKER_UNBOUND); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1638 | |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1639 | /* rebind complete, become available again */ |
| 1640 | list_add(&worker->entry, &worker->pool->idle_list); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1641 | spin_unlock_irq(&worker->pool->lock); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * Function for @worker->rebind.work used to rebind unbound busy workers to |
Tejun Heo | 403c821 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1646 | * the associated cpu which is coming back online. This is scheduled by |
| 1647 | * cpu up but can race with other cpu hotplug operations and may be |
| 1648 | * executed twice without intervening cpu down. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1649 | */ |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1650 | static void busy_worker_rebind_fn(struct work_struct *work) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1651 | { |
| 1652 | struct worker *worker = container_of(work, struct worker, rebind_work); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1653 | |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 1654 | if (worker_maybe_bind_and_lock(worker->pool)) |
Lai Jiangshan | eab6d82 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1655 | worker_clr_flags(worker, WORKER_UNBOUND); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1656 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1657 | spin_unlock_irq(&worker->pool->lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1660 | /** |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1661 | * rebind_workers - rebind all workers of a pool to the associated CPU |
| 1662 | * @pool: pool of interest |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1663 | * |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1664 | * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1665 | * is different for idle and busy ones. |
| 1666 | * |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1667 | * Idle ones will be removed from the idle_list and woken up. They will |
| 1668 | * add themselves back after completing rebind. This ensures that the |
| 1669 | * idle_list doesn't contain any unbound workers when re-bound busy workers |
| 1670 | * try to perform local wake-ups for concurrency management. |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1671 | * |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1672 | * Busy workers can rebind after they finish their current work items. |
| 1673 | * Queueing the rebind work item at the head of the scheduled list is |
| 1674 | * enough. Note that nr_running will be properly bumped as busy workers |
| 1675 | * rebind. |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1676 | * |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1677 | * On return, all non-manager workers are scheduled for rebind - see |
| 1678 | * manage_workers() for the manager special case. Any idle worker |
| 1679 | * including the manager will not appear on @idle_list until rebind is |
| 1680 | * complete, making local wake-ups safe. |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1681 | */ |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1682 | static void rebind_workers(struct worker_pool *pool) |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1683 | { |
Lai Jiangshan | ea1abd6 | 2012-09-18 09:59:22 -0700 | [diff] [blame] | 1684 | struct worker *worker, *n; |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1685 | int i; |
| 1686 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1687 | lockdep_assert_held(&pool->assoc_mutex); |
| 1688 | lockdep_assert_held(&pool->lock); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1689 | |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 1690 | /* dequeue and kick idle ones */ |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1691 | list_for_each_entry_safe(worker, n, &pool->idle_list, entry) { |
| 1692 | /* |
| 1693 | * idle workers should be off @pool->idle_list until rebind |
| 1694 | * is complete to avoid receiving premature local wake-ups. |
| 1695 | */ |
| 1696 | list_del_init(&worker->entry); |
Lai Jiangshan | 96e6530 | 2012-09-02 00:28:19 +0800 | [diff] [blame] | 1697 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1698 | /* |
| 1699 | * worker_thread() will see the above dequeuing and call |
| 1700 | * idle_worker_rebind(). |
| 1701 | */ |
| 1702 | wake_up_process(worker->task); |
| 1703 | } |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1704 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1705 | /* rebind busy workers */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1706 | for_each_busy_worker(worker, i, pool) { |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1707 | struct work_struct *rebind_work = &worker->rebind_work; |
| 1708 | struct workqueue_struct *wq; |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1709 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1710 | if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, |
| 1711 | work_data_bits(rebind_work))) |
| 1712 | continue; |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1713 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1714 | debug_work_activate(rebind_work); |
Tejun Heo | 90beca5 | 2012-09-04 23:12:33 -0700 | [diff] [blame] | 1715 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1716 | /* |
| 1717 | * wq doesn't really matter but let's keep @worker->pool |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1718 | * and @pwq->pool consistent for sanity. |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1719 | */ |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1720 | if (worker->pool->attrs->nice < 0) |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1721 | wq = system_highpri_wq; |
| 1722 | else |
| 1723 | wq = system_wq; |
Tejun Heo | ec58815 | 2012-09-04 23:16:32 -0700 | [diff] [blame] | 1724 | |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1725 | insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work, |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1726 | worker->scheduled.next, |
| 1727 | work_color_to_flags(WORK_NO_COLOR)); |
Tejun Heo | ec58815 | 2012-09-04 23:16:32 -0700 | [diff] [blame] | 1728 | } |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1731 | static struct worker *alloc_worker(void) |
| 1732 | { |
| 1733 | struct worker *worker; |
| 1734 | |
| 1735 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1736 | if (worker) { |
| 1737 | INIT_LIST_HEAD(&worker->entry); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1738 | INIT_LIST_HEAD(&worker->scheduled); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1739 | INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1740 | /* on creation a worker is in !idle && prep state */ |
| 1741 | worker->flags = WORKER_PREP; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1742 | } |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1743 | return worker; |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * create_worker - create a new workqueue worker |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1748 | * @pool: pool the new worker will belong to |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1749 | * |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1750 | * Create a new worker which is bound to @pool. The returned worker |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1751 | * can be started by calling start_worker() or destroyed using |
| 1752 | * destroy_worker(). |
| 1753 | * |
| 1754 | * CONTEXT: |
| 1755 | * Might sleep. Does GFP_KERNEL allocations. |
| 1756 | * |
| 1757 | * RETURNS: |
| 1758 | * Pointer to the newly created worker. |
| 1759 | */ |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1760 | static struct worker *create_worker(struct worker_pool *pool) |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1761 | { |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1762 | const char *pri = pool->attrs->nice < 0 ? "H" : ""; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1763 | struct worker *worker = NULL; |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 1764 | int id = -1; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1765 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1766 | spin_lock_irq(&pool->lock); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1767 | while (ida_get_new(&pool->worker_ida, &id)) { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1768 | spin_unlock_irq(&pool->lock); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1769 | if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1770 | goto fail; |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1771 | spin_lock_irq(&pool->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1772 | } |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1773 | spin_unlock_irq(&pool->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1774 | |
| 1775 | worker = alloc_worker(); |
| 1776 | if (!worker) |
| 1777 | goto fail; |
| 1778 | |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1779 | worker->pool = pool; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1780 | worker->id = id; |
| 1781 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 1782 | if (pool->cpu >= 0) |
Eric Dumazet | 94dcf29 | 2011-03-22 16:30:45 -0700 | [diff] [blame] | 1783 | worker->task = kthread_create_on_node(worker_thread, |
Tejun Heo | ec22ca5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1784 | worker, cpu_to_node(pool->cpu), |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1785 | "kworker/%d:%d%s", pool->cpu, id, pri); |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 1786 | else |
| 1787 | worker->task = kthread_create(worker_thread, worker, |
Tejun Heo | ac6104c | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 1788 | "kworker/u%d:%d%s", |
| 1789 | pool->id, id, pri); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1790 | if (IS_ERR(worker->task)) |
| 1791 | goto fail; |
| 1792 | |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1793 | set_user_nice(worker->task, pool->attrs->nice); |
| 1794 | set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); |
Tejun Heo | 3270476 | 2012-07-13 22:16:45 -0700 | [diff] [blame] | 1795 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1796 | /* |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1797 | * %PF_THREAD_BOUND is used to prevent userland from meddling with |
| 1798 | * cpumask of workqueue workers. This is an abuse. We need |
| 1799 | * %PF_NO_SETAFFINITY. |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1800 | */ |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 1801 | worker->task->flags |= PF_THREAD_BOUND; |
| 1802 | |
| 1803 | /* |
| 1804 | * The caller is responsible for ensuring %POOL_DISASSOCIATED |
| 1805 | * remains stable across this function. See the comments above the |
| 1806 | * flag definition for details. |
| 1807 | */ |
| 1808 | if (pool->flags & POOL_DISASSOCIATED) |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1809 | worker->flags |= WORKER_UNBOUND; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1810 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1811 | return worker; |
| 1812 | fail: |
| 1813 | if (id >= 0) { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1814 | spin_lock_irq(&pool->lock); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1815 | ida_remove(&pool->worker_ida, id); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1816 | spin_unlock_irq(&pool->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1817 | } |
| 1818 | kfree(worker); |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * start_worker - start a newly created worker |
| 1824 | * @worker: worker to start |
| 1825 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1826 | * Make the pool aware of @worker and start it. |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1827 | * |
| 1828 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1829 | * spin_lock_irq(pool->lock). |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1830 | */ |
| 1831 | static void start_worker(struct worker *worker) |
| 1832 | { |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 1833 | worker->flags |= WORKER_STARTED; |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1834 | worker->pool->nr_workers++; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1835 | worker_enter_idle(worker); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1836 | wake_up_process(worker->task); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
| 1840 | * destroy_worker - destroy a workqueue worker |
| 1841 | * @worker: worker to be destroyed |
| 1842 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1843 | * Destroy @worker and adjust @pool stats accordingly. |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1844 | * |
| 1845 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1846 | * spin_lock_irq(pool->lock) which is released and regrabbed. |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1847 | */ |
| 1848 | static void destroy_worker(struct worker *worker) |
| 1849 | { |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1850 | struct worker_pool *pool = worker->pool; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1851 | int id = worker->id; |
| 1852 | |
| 1853 | /* sanity check frenzy */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 1854 | if (WARN_ON(worker->current_work) || |
| 1855 | WARN_ON(!list_empty(&worker->scheduled))) |
| 1856 | return; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1857 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1858 | if (worker->flags & WORKER_STARTED) |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1859 | pool->nr_workers--; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1860 | if (worker->flags & WORKER_IDLE) |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1861 | pool->nr_idle--; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1862 | |
| 1863 | list_del_init(&worker->entry); |
Tejun Heo | cb44476 | 2010-07-02 10:03:50 +0200 | [diff] [blame] | 1864 | worker->flags |= WORKER_DIE; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1865 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1866 | spin_unlock_irq(&pool->lock); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1867 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1868 | kthread_stop(worker->task); |
| 1869 | kfree(worker); |
| 1870 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1871 | spin_lock_irq(&pool->lock); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1872 | ida_remove(&pool->worker_ida, id); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1873 | } |
| 1874 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1875 | static void idle_worker_timeout(unsigned long __pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1876 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1877 | struct worker_pool *pool = (void *)__pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1878 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1879 | spin_lock_irq(&pool->lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1880 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1881 | if (too_many_workers(pool)) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1882 | struct worker *worker; |
| 1883 | unsigned long expires; |
| 1884 | |
| 1885 | /* idle_list is kept in LIFO order, check the last one */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1886 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1887 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
| 1888 | |
| 1889 | if (time_before(jiffies, expires)) |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1890 | mod_timer(&pool->idle_timer, expires); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1891 | else { |
| 1892 | /* it's been idle for too long, wake up manager */ |
Tejun Heo | 11ebea5 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1893 | pool->flags |= POOL_MANAGE_WORKERS; |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1894 | wake_up_worker(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1898 | spin_unlock_irq(&pool->lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1899 | } |
| 1900 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1901 | static void send_mayday(struct work_struct *work) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1902 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 1903 | struct pool_workqueue *pwq = get_work_pwq(work); |
| 1904 | struct workqueue_struct *wq = pwq->wq; |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1905 | |
| 1906 | lockdep_assert_held(&workqueue_lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1907 | |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 1908 | if (!wq->rescuer) |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1909 | return; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1910 | |
| 1911 | /* mayday mayday mayday */ |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1912 | if (list_empty(&pwq->mayday_node)) { |
| 1913 | list_add_tail(&pwq->mayday_node, &wq->maydays); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1914 | wake_up_process(wq->rescuer->task); |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1915 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1916 | } |
| 1917 | |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 1918 | static void pool_mayday_timeout(unsigned long __pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1919 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1920 | struct worker_pool *pool = (void *)__pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1921 | struct work_struct *work; |
| 1922 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1923 | spin_lock_irq(&workqueue_lock); /* for wq->maydays */ |
| 1924 | spin_lock(&pool->lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1925 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1926 | if (need_to_create_worker(pool)) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1927 | /* |
| 1928 | * We've been trying to create a new worker but |
| 1929 | * haven't been successful. We might be hitting an |
| 1930 | * allocation deadlock. Send distress signals to |
| 1931 | * rescuers. |
| 1932 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1933 | list_for_each_entry(work, &pool->worklist, entry) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1934 | send_mayday(work); |
| 1935 | } |
| 1936 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 1937 | spin_unlock(&pool->lock); |
| 1938 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1939 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1940 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * maybe_create_worker - create a new worker if necessary |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1945 | * @pool: pool to create a new worker for |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1946 | * |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1947 | * Create a new worker for @pool if necessary. @pool is guaranteed to |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1948 | * have at least one idle worker on return from this function. If |
| 1949 | * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1950 | * sent to all rescuers with works scheduled on @pool to resolve |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1951 | * possible allocation deadlock. |
| 1952 | * |
| 1953 | * On return, need_to_create_worker() is guaranteed to be false and |
| 1954 | * may_start_working() true. |
| 1955 | * |
| 1956 | * LOCKING: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1957 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1958 | * multiple times. Does GFP_KERNEL allocations. Called only from |
| 1959 | * manager. |
| 1960 | * |
| 1961 | * RETURNS: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1962 | * false if no action was taken and pool->lock stayed locked, true |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1963 | * otherwise. |
| 1964 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1965 | static bool maybe_create_worker(struct worker_pool *pool) |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1966 | __releases(&pool->lock) |
| 1967 | __acquires(&pool->lock) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1968 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1969 | if (!need_to_create_worker(pool)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1970 | return false; |
| 1971 | restart: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1972 | spin_unlock_irq(&pool->lock); |
Tejun Heo | 9f9c23644 | 2010-07-14 11:31:20 +0200 | [diff] [blame] | 1973 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1974 | /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1975 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1976 | |
| 1977 | while (true) { |
| 1978 | struct worker *worker; |
| 1979 | |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 1980 | worker = create_worker(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1981 | if (worker) { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1982 | del_timer_sync(&pool->mayday_timer); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 1983 | spin_lock_irq(&pool->lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1984 | start_worker(worker); |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 1985 | if (WARN_ON_ONCE(need_to_create_worker(pool))) |
| 1986 | goto restart; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1987 | return true; |
| 1988 | } |
| 1989 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1990 | if (!need_to_create_worker(pool)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1991 | break; |
| 1992 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1993 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1994 | schedule_timeout(CREATE_COOLDOWN); |
Tejun Heo | 9f9c23644 | 2010-07-14 11:31:20 +0200 | [diff] [blame] | 1995 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 1996 | if (!need_to_create_worker(pool)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 1997 | break; |
| 1998 | } |
| 1999 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2000 | del_timer_sync(&pool->mayday_timer); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2001 | spin_lock_irq(&pool->lock); |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2002 | if (need_to_create_worker(pool)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2003 | goto restart; |
| 2004 | return true; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * maybe_destroy_worker - destroy workers which have been idle for a while |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2009 | * @pool: pool to destroy workers for |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2010 | * |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2011 | * Destroy @pool workers which have been idle for longer than |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2012 | * IDLE_WORKER_TIMEOUT. |
| 2013 | * |
| 2014 | * LOCKING: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2015 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2016 | * multiple times. Called only from manager. |
| 2017 | * |
| 2018 | * RETURNS: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2019 | * false if no action was taken and pool->lock stayed locked, true |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2020 | * otherwise. |
| 2021 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2022 | static bool maybe_destroy_workers(struct worker_pool *pool) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2023 | { |
| 2024 | bool ret = false; |
| 2025 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2026 | while (too_many_workers(pool)) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2027 | struct worker *worker; |
| 2028 | unsigned long expires; |
| 2029 | |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2030 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2031 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
| 2032 | |
| 2033 | if (time_before(jiffies, expires)) { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2034 | mod_timer(&pool->idle_timer, expires); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2035 | break; |
| 2036 | } |
| 2037 | |
| 2038 | destroy_worker(worker); |
| 2039 | ret = true; |
| 2040 | } |
| 2041 | |
| 2042 | return ret; |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * manage_workers - manage worker pool |
| 2047 | * @worker: self |
| 2048 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2049 | * Assume the manager role and manage the worker pool @worker belongs |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2050 | * to. At any given time, there can be only zero or one manager per |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2051 | * pool. The exclusion is handled automatically by this function. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2052 | * |
| 2053 | * The caller can safely start processing works on false return. On |
| 2054 | * true return, it's guaranteed that need_to_create_worker() is false |
| 2055 | * and may_start_working() is true. |
| 2056 | * |
| 2057 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2058 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2059 | * multiple times. Does GFP_KERNEL allocations. |
| 2060 | * |
| 2061 | * RETURNS: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2062 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
| 2063 | * multiple times. Does GFP_KERNEL allocations. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2064 | */ |
| 2065 | static bool manage_workers(struct worker *worker) |
| 2066 | { |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2067 | struct worker_pool *pool = worker->pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2068 | bool ret = false; |
| 2069 | |
Tejun Heo | 34a06bd | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2070 | if (!mutex_trylock(&pool->manager_arb)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2071 | return ret; |
| 2072 | |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2073 | /* |
| 2074 | * To simplify both worker management and CPU hotplug, hold off |
| 2075 | * management while hotplug is in progress. CPU hotplug path can't |
Tejun Heo | 34a06bd | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2076 | * grab @pool->manager_arb to achieve this because that can lead to |
| 2077 | * idle worker depletion (all become busy thinking someone else is |
| 2078 | * managing) which in turn can result in deadlock under extreme |
| 2079 | * circumstances. Use @pool->assoc_mutex to synchronize manager |
| 2080 | * against CPU hotplug. |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2081 | * |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2082 | * assoc_mutex would always be free unless CPU hotplug is in |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2083 | * progress. trylock first without dropping @pool->lock. |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2084 | */ |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2085 | if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2086 | spin_unlock_irq(&pool->lock); |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2087 | mutex_lock(&pool->assoc_mutex); |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2088 | /* |
| 2089 | * CPU hotplug could have happened while we were waiting |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2090 | * for assoc_mutex. Hotplug itself can't handle us |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2091 | * because manager isn't either on idle or busy list, and |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2092 | * @pool's state and ours could have deviated. |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2093 | * |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2094 | * As hotplug is now excluded via assoc_mutex, we can |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2095 | * simply try to bind. It will succeed or fail depending |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2096 | * on @pool's current state. Try it and adjust |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2097 | * %WORKER_UNBOUND accordingly. |
| 2098 | */ |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 2099 | if (worker_maybe_bind_and_lock(pool)) |
Lai Jiangshan | ee378aa | 2012-09-10 10:03:44 -0700 | [diff] [blame] | 2100 | worker->flags &= ~WORKER_UNBOUND; |
| 2101 | else |
| 2102 | worker->flags |= WORKER_UNBOUND; |
| 2103 | |
| 2104 | ret = true; |
| 2105 | } |
| 2106 | |
Tejun Heo | 11ebea5 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2107 | pool->flags &= ~POOL_MANAGE_WORKERS; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2108 | |
| 2109 | /* |
| 2110 | * Destroy and then create so that may_start_working() is true |
| 2111 | * on return. |
| 2112 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2113 | ret |= maybe_destroy_workers(pool); |
| 2114 | ret |= maybe_create_worker(pool); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2115 | |
Lai Jiangshan | b2eb83d | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2116 | mutex_unlock(&pool->assoc_mutex); |
Tejun Heo | 34a06bd | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2117 | mutex_unlock(&pool->manager_arb); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2118 | return ret; |
| 2119 | } |
| 2120 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2121 | /** |
| 2122 | * process_one_work - process single work |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2123 | * @worker: self |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2124 | * @work: work to process |
| 2125 | * |
| 2126 | * Process @work. This function contains all the logics necessary to |
| 2127 | * process a single work including synchronization against and |
| 2128 | * interaction with other workers on the same cpu, queueing and |
| 2129 | * flushing. As long as context requirement is met, any worker can |
| 2130 | * call this function to process a work. |
| 2131 | * |
| 2132 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2133 | * spin_lock_irq(pool->lock) which is released and regrabbed. |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2134 | */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2135 | static void process_one_work(struct worker *worker, struct work_struct *work) |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2136 | __releases(&pool->lock) |
| 2137 | __acquires(&pool->lock) |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2138 | { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2139 | struct pool_workqueue *pwq = get_work_pwq(work); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2140 | struct worker_pool *pool = worker->pool; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2141 | bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2142 | int work_color; |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2143 | struct worker *collision; |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2144 | #ifdef CONFIG_LOCKDEP |
| 2145 | /* |
| 2146 | * It is permissible to free the struct work_struct from |
| 2147 | * inside the function that is called from it, this we need to |
| 2148 | * take into account for lockdep too. To avoid bogus "held |
| 2149 | * lock freed" warnings as well as problems when looking into |
| 2150 | * work->lockdep_map, make a copy and use that here. |
| 2151 | */ |
Peter Zijlstra | 4d82a1d | 2012-05-15 08:06:19 -0700 | [diff] [blame] | 2152 | struct lockdep_map lockdep_map; |
| 2153 | |
| 2154 | lockdep_copy_map(&lockdep_map, &work->lockdep_map); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2155 | #endif |
Tejun Heo | 6fec10a | 2012-07-22 10:16:34 -0700 | [diff] [blame] | 2156 | /* |
| 2157 | * Ensure we're on the correct CPU. DISASSOCIATED test is |
| 2158 | * necessary to avoid spurious warnings from rescuers servicing the |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2159 | * unbound or a disassociated pool. |
Tejun Heo | 6fec10a | 2012-07-22 10:16:34 -0700 | [diff] [blame] | 2160 | */ |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2161 | WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) && |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2162 | !(pool->flags & POOL_DISASSOCIATED) && |
Tejun Heo | ec22ca5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2163 | raw_smp_processor_id() != pool->cpu); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 2164 | |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2165 | /* |
| 2166 | * A single work shouldn't be executed concurrently by |
| 2167 | * multiple workers on a single cpu. Check whether anyone is |
| 2168 | * already processing the work. If so, defer the work to the |
| 2169 | * currently executing one. |
| 2170 | */ |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2171 | collision = find_worker_executing_work(pool, work); |
Tejun Heo | 7e11629 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2172 | if (unlikely(collision)) { |
| 2173 | move_linked_works(work, &collision->scheduled, NULL); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 2177 | /* claim and dequeue */ |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2178 | debug_work_deactivate(work); |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2179 | hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2180 | worker->current_work = work; |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 2181 | worker->current_func = work->func; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2182 | worker->current_pwq = pwq; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2183 | work_color = get_work_color(work); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2184 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2185 | list_del_init(&work->entry); |
| 2186 | |
Tejun Heo | 649027d | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2187 | /* |
Tejun Heo | fb0e7be | 2010-06-29 10:07:15 +0200 | [diff] [blame] | 2188 | * CPU intensive works don't participate in concurrency |
| 2189 | * management. They're the scheduler's responsibility. |
| 2190 | */ |
| 2191 | if (unlikely(cpu_intensive)) |
| 2192 | worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); |
| 2193 | |
Tejun Heo | 974271c | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2194 | /* |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2195 | * Unbound pool isn't concurrency managed and work items should be |
Tejun Heo | 974271c | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2196 | * executed ASAP. Wake up another worker if necessary. |
| 2197 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2198 | if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) |
| 2199 | wake_up_worker(pool); |
Tejun Heo | 974271c | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2200 | |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 2201 | /* |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2202 | * Record the last pool and clear PENDING which should be the last |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2203 | * update to @work. Also, do this inside @pool->lock so that |
Tejun Heo | 23657bb | 2012-08-13 17:08:19 -0700 | [diff] [blame] | 2204 | * PENDING and queued state changes happen together while IRQ is |
| 2205 | * disabled. |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 2206 | */ |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2207 | set_work_pool_and_clear_pending(work, pool->id); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2208 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2209 | spin_unlock_irq(&pool->lock); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2210 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2211 | lock_map_acquire_read(&pwq->wq->lockdep_map); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2212 | lock_map_acquire(&lockdep_map); |
Arjan van de Ven | e36c886 | 2010-08-21 13:07:26 -0700 | [diff] [blame] | 2213 | trace_workqueue_execute_start(work); |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 2214 | worker->current_func(work); |
Arjan van de Ven | e36c886 | 2010-08-21 13:07:26 -0700 | [diff] [blame] | 2215 | /* |
| 2216 | * While we must be careful to not use "work" after this, the trace |
| 2217 | * point will only record its address. |
| 2218 | */ |
| 2219 | trace_workqueue_execute_end(work); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2220 | lock_map_release(&lockdep_map); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2221 | lock_map_release(&pwq->wq->lockdep_map); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2222 | |
| 2223 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 2224 | pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" |
| 2225 | " last function: %pf\n", |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 2226 | current->comm, preempt_count(), task_pid_nr(current), |
| 2227 | worker->current_func); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2228 | debug_show_held_locks(current); |
| 2229 | dump_stack(); |
| 2230 | } |
| 2231 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2232 | spin_lock_irq(&pool->lock); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2233 | |
Tejun Heo | fb0e7be | 2010-06-29 10:07:15 +0200 | [diff] [blame] | 2234 | /* clear cpu intensive status */ |
| 2235 | if (unlikely(cpu_intensive)) |
| 2236 | worker_clr_flags(worker, WORKER_CPU_INTENSIVE); |
| 2237 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2238 | /* we're done with it, release */ |
Sasha Levin | 42f8570 | 2012-12-17 10:01:23 -0500 | [diff] [blame] | 2239 | hash_del(&worker->hentry); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2240 | worker->current_work = NULL; |
Tejun Heo | a2c1c57 | 2012-12-18 10:35:02 -0800 | [diff] [blame] | 2241 | worker->current_func = NULL; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2242 | worker->current_pwq = NULL; |
| 2243 | pwq_dec_nr_in_flight(pwq, work_color); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2244 | } |
| 2245 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2246 | /** |
| 2247 | * process_scheduled_works - process scheduled works |
| 2248 | * @worker: self |
| 2249 | * |
| 2250 | * Process all scheduled works. Please note that the scheduled list |
| 2251 | * may change while processing a work, so this function repeatedly |
| 2252 | * fetches a work from the top and executes it. |
| 2253 | * |
| 2254 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2255 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2256 | * multiple times. |
| 2257 | */ |
| 2258 | static void process_scheduled_works(struct worker *worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | { |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2260 | while (!list_empty(&worker->scheduled)) { |
| 2261 | struct work_struct *work = list_first_entry(&worker->scheduled, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | struct work_struct, entry); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2263 | process_one_work(worker, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2267 | /** |
| 2268 | * worker_thread - the worker thread function |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2269 | * @__worker: self |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2270 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2271 | * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools |
| 2272 | * of these per each cpu. These workers process all works regardless of |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2273 | * their specific target workqueue. The only exception is works which |
| 2274 | * belong to workqueues with a rescuer which will be explained in |
| 2275 | * rescuer_thread(). |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2276 | */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2277 | static int worker_thread(void *__worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | { |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2279 | struct worker *worker = __worker; |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2280 | struct worker_pool *pool = worker->pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2282 | /* tell the scheduler that this is a workqueue worker */ |
| 2283 | worker->task->flags |= PF_WQ_WORKER; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2284 | woke_up: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2285 | spin_lock_irq(&pool->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2287 | /* we are off idle list if destruction or rebind is requested */ |
| 2288 | if (unlikely(list_empty(&worker->entry))) { |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2289 | spin_unlock_irq(&pool->lock); |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 2290 | |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2291 | /* if DIE is set, destruction is requested */ |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 2292 | if (worker->flags & WORKER_DIE) { |
| 2293 | worker->task->flags &= ~PF_WQ_WORKER; |
| 2294 | return 0; |
| 2295 | } |
| 2296 | |
Lai Jiangshan | 5f7dabf | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 2297 | /* otherwise, rebind */ |
Tejun Heo | 25511a4 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 2298 | idle_worker_rebind(worker); |
| 2299 | goto woke_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2302 | worker_leave_idle(worker); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2303 | recheck: |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2304 | /* no more worker necessary? */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2305 | if (!need_more_worker(pool)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2306 | goto sleep; |
| 2307 | |
| 2308 | /* do we need to manage? */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2309 | if (unlikely(!may_start_working(pool)) && manage_workers(worker)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2310 | goto recheck; |
| 2311 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2312 | /* |
| 2313 | * ->scheduled list can only be filled while a worker is |
| 2314 | * preparing to process a work or actually processing it. |
| 2315 | * Make sure nobody diddled with it while I was sleeping. |
| 2316 | */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2317 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2318 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2319 | /* |
| 2320 | * When control reaches this point, we're guaranteed to have |
| 2321 | * at least one idle worker or that someone else has already |
| 2322 | * assumed the manager role. |
| 2323 | */ |
| 2324 | worker_clr_flags(worker, WORKER_PREP); |
| 2325 | |
| 2326 | do { |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2327 | struct work_struct *work = |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2328 | list_first_entry(&pool->worklist, |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2329 | struct work_struct, entry); |
| 2330 | |
| 2331 | if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { |
| 2332 | /* optimization path, not strictly necessary */ |
| 2333 | process_one_work(worker, work); |
| 2334 | if (unlikely(!list_empty(&worker->scheduled))) |
| 2335 | process_scheduled_works(worker); |
| 2336 | } else { |
| 2337 | move_linked_works(work, &worker->scheduled, NULL); |
| 2338 | process_scheduled_works(worker); |
| 2339 | } |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2340 | } while (keep_working(pool)); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2341 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2342 | worker_set_flags(worker, WORKER_PREP, false); |
Tejun Heo | d313dd8 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 2343 | sleep: |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2344 | if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2345 | goto recheck; |
Tejun Heo | d313dd8 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 2346 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2347 | /* |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2348 | * pool->lock is held and there's no work to process and no need to |
| 2349 | * manage, sleep. Workers are woken up only while holding |
| 2350 | * pool->lock or from local cpu, so setting the current state |
| 2351 | * before releasing pool->lock is enough to prevent losing any |
| 2352 | * event. |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2353 | */ |
| 2354 | worker_enter_idle(worker); |
| 2355 | __set_current_state(TASK_INTERRUPTIBLE); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2356 | spin_unlock_irq(&pool->lock); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2357 | schedule(); |
| 2358 | goto woke_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2361 | /** |
| 2362 | * rescuer_thread - the rescuer thread function |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2363 | * @__rescuer: self |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2364 | * |
| 2365 | * Workqueue rescuer thread function. There's one rescuer for each |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 2366 | * workqueue which has WQ_MEM_RECLAIM set. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2367 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2368 | * Regular work processing on a pool may block trying to create a new |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2369 | * worker which uses GFP_KERNEL allocation which has slight chance of |
| 2370 | * developing into deadlock if some works currently on the same queue |
| 2371 | * need to be processed to satisfy the GFP_KERNEL allocation. This is |
| 2372 | * the problem rescuer solves. |
| 2373 | * |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 2374 | * When such condition is possible, the pool summons rescuers of all |
| 2375 | * workqueues which have works queued on the pool and let them process |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2376 | * those works so that forward progress can be guaranteed. |
| 2377 | * |
| 2378 | * This should happen rarely. |
| 2379 | */ |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2380 | static int rescuer_thread(void *__rescuer) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2381 | { |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2382 | struct worker *rescuer = __rescuer; |
| 2383 | struct workqueue_struct *wq = rescuer->rescue_wq; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2384 | struct list_head *scheduled = &rescuer->scheduled; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2385 | |
| 2386 | set_user_nice(current, RESCUER_NICE_LEVEL); |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2387 | |
| 2388 | /* |
| 2389 | * Mark rescuer as worker too. As WORKER_PREP is never cleared, it |
| 2390 | * doesn't participate in concurrency management. |
| 2391 | */ |
| 2392 | rescuer->task->flags |= PF_WQ_WORKER; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2393 | repeat: |
| 2394 | set_current_state(TASK_INTERRUPTIBLE); |
| 2395 | |
Mike Galbraith | 412d32e | 2012-11-28 07:17:18 +0100 | [diff] [blame] | 2396 | if (kthread_should_stop()) { |
| 2397 | __set_current_state(TASK_RUNNING); |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2398 | rescuer->task->flags &= ~PF_WQ_WORKER; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2399 | return 0; |
Mike Galbraith | 412d32e | 2012-11-28 07:17:18 +0100 | [diff] [blame] | 2400 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2401 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 2402 | /* see whether any pwq is asking for help */ |
| 2403 | spin_lock_irq(&workqueue_lock); |
| 2404 | |
| 2405 | while (!list_empty(&wq->maydays)) { |
| 2406 | struct pool_workqueue *pwq = list_first_entry(&wq->maydays, |
| 2407 | struct pool_workqueue, mayday_node); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2408 | struct worker_pool *pool = pwq->pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2409 | struct work_struct *work, *n; |
| 2410 | |
| 2411 | __set_current_state(TASK_RUNNING); |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 2412 | list_del_init(&pwq->mayday_node); |
| 2413 | |
| 2414 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2415 | |
| 2416 | /* migrate to the target cpu if possible */ |
Lai Jiangshan | f36dc67 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 2417 | worker_maybe_bind_and_lock(pool); |
Lai Jiangshan | b310410 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 2418 | rescuer->pool = pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2419 | |
| 2420 | /* |
| 2421 | * Slurp in all works issued via this workqueue and |
| 2422 | * process'em. |
| 2423 | */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2424 | WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); |
Tejun Heo | bd7bdd4 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2425 | list_for_each_entry_safe(work, n, &pool->worklist, entry) |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2426 | if (get_work_pwq(work) == pwq) |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2427 | move_linked_works(work, scheduled, &n); |
| 2428 | |
| 2429 | process_scheduled_works(rescuer); |
Tejun Heo | 7576958 | 2011-02-14 14:04:46 +0100 | [diff] [blame] | 2430 | |
| 2431 | /* |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2432 | * Leave this pool. If keep_working() is %true, notify a |
Tejun Heo | 7576958 | 2011-02-14 14:04:46 +0100 | [diff] [blame] | 2433 | * regular worker; otherwise, we end up with 0 concurrency |
| 2434 | * and stalling the execution. |
| 2435 | */ |
Tejun Heo | 63d95a9 | 2012-07-12 14:46:37 -0700 | [diff] [blame] | 2436 | if (keep_working(pool)) |
| 2437 | wake_up_worker(pool); |
Tejun Heo | 7576958 | 2011-02-14 14:04:46 +0100 | [diff] [blame] | 2438 | |
Lai Jiangshan | b310410 | 2013-02-19 12:17:02 -0800 | [diff] [blame] | 2439 | rescuer->pool = NULL; |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 2440 | spin_unlock(&pool->lock); |
| 2441 | spin_lock(&workqueue_lock); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2442 | } |
| 2443 | |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 2444 | spin_unlock_irq(&workqueue_lock); |
| 2445 | |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 2446 | /* rescuers should never participate in concurrency management */ |
| 2447 | WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 2448 | schedule(); |
| 2449 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 2452 | struct wq_barrier { |
| 2453 | struct work_struct work; |
| 2454 | struct completion done; |
| 2455 | }; |
| 2456 | |
| 2457 | static void wq_barrier_func(struct work_struct *work) |
| 2458 | { |
| 2459 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 2460 | complete(&barr->done); |
| 2461 | } |
| 2462 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2463 | /** |
| 2464 | * insert_wq_barrier - insert a barrier work |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2465 | * @pwq: pwq to insert barrier into |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2466 | * @barr: wq_barrier to insert |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2467 | * @target: target work to attach @barr to |
| 2468 | * @worker: worker currently executing @target, NULL if @target is not executing |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2469 | * |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2470 | * @barr is linked to @target such that @barr is completed only after |
| 2471 | * @target finishes execution. Please note that the ordering |
| 2472 | * guarantee is observed only with respect to @target and on the local |
| 2473 | * cpu. |
| 2474 | * |
| 2475 | * Currently, a queued barrier can't be canceled. This is because |
| 2476 | * try_to_grab_pending() can't determine whether the work to be |
| 2477 | * grabbed is at the head of the queue and thus can't clear LINKED |
| 2478 | * flag of the previous work while there must be a valid next work |
| 2479 | * after a work with LINKED flag set. |
| 2480 | * |
| 2481 | * Note that when @worker is non-NULL, @target may be modified |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2482 | * underneath us, so we can't reliably determine pwq from @target. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2483 | * |
| 2484 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2485 | * spin_lock_irq(pool->lock). |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2486 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2487 | static void insert_wq_barrier(struct pool_workqueue *pwq, |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2488 | struct wq_barrier *barr, |
| 2489 | struct work_struct *target, struct worker *worker) |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 2490 | { |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2491 | struct list_head *head; |
| 2492 | unsigned int linked = 0; |
| 2493 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 2494 | /* |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2495 | * debugobject calls are safe here even with pool->lock locked |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 2496 | * as we know for sure that this will not trigger any of the |
| 2497 | * checks and call back into the fixup functions where we |
| 2498 | * might deadlock. |
| 2499 | */ |
Andrew Morton | ca1cab3 | 2010-10-26 14:22:34 -0700 | [diff] [blame] | 2500 | INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 2501 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 2502 | init_completion(&barr->done); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 2503 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2504 | /* |
| 2505 | * If @target is currently being executed, schedule the |
| 2506 | * barrier to the worker; otherwise, put it after @target. |
| 2507 | */ |
| 2508 | if (worker) |
| 2509 | head = worker->scheduled.next; |
| 2510 | else { |
| 2511 | unsigned long *bits = work_data_bits(target); |
| 2512 | |
| 2513 | head = target->entry.next; |
| 2514 | /* there can already be other linked works, inherit and set */ |
| 2515 | linked = *bits & WORK_STRUCT_LINKED; |
| 2516 | __set_bit(WORK_STRUCT_LINKED_BIT, bits); |
| 2517 | } |
| 2518 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 2519 | debug_work_activate(&barr->work); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2520 | insert_work(pwq, &barr->work, head, |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2521 | work_color_to_flags(WORK_NO_COLOR) | linked); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2524 | /** |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2525 | * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2526 | * @wq: workqueue being flushed |
| 2527 | * @flush_color: new flush color, < 0 for no-op |
| 2528 | * @work_color: new work color, < 0 for no-op |
| 2529 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2530 | * Prepare pwqs for workqueue flushing. |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2531 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2532 | * If @flush_color is non-negative, flush_color on all pwqs should be |
| 2533 | * -1. If no pwq has in-flight commands at the specified color, all |
| 2534 | * pwq->flush_color's stay at -1 and %false is returned. If any pwq |
| 2535 | * has in flight commands, its pwq->flush_color is set to |
| 2536 | * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2537 | * wakeup logic is armed and %true is returned. |
| 2538 | * |
| 2539 | * The caller should have initialized @wq->first_flusher prior to |
| 2540 | * calling this function with non-negative @flush_color. If |
| 2541 | * @flush_color is negative, no flush color update is done and %false |
| 2542 | * is returned. |
| 2543 | * |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2544 | * If @work_color is non-negative, all pwqs should have the same |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2545 | * work_color which is previous to @work_color and all will be |
| 2546 | * advanced to @work_color. |
| 2547 | * |
| 2548 | * CONTEXT: |
| 2549 | * mutex_lock(wq->flush_mutex). |
| 2550 | * |
| 2551 | * RETURNS: |
| 2552 | * %true if @flush_color >= 0 and there's something to flush. %false |
| 2553 | * otherwise. |
| 2554 | */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2555 | static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2556 | int flush_color, int work_color) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2558 | bool wait = false; |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 2559 | struct pool_workqueue *pwq; |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 2560 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2561 | if (flush_color >= 0) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2562 | WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2563 | atomic_set(&wq->nr_pwqs_to_flush, 1); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 2564 | } |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 2565 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2566 | local_irq_disable(); |
| 2567 | |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 2568 | for_each_pwq(pwq, wq) { |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2569 | struct worker_pool *pool = pwq->pool; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2570 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2571 | spin_lock(&pool->lock); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2572 | |
| 2573 | if (flush_color >= 0) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2574 | WARN_ON_ONCE(pwq->flush_color != -1); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2575 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2576 | if (pwq->nr_in_flight[flush_color]) { |
| 2577 | pwq->flush_color = flush_color; |
| 2578 | atomic_inc(&wq->nr_pwqs_to_flush); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2579 | wait = true; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | if (work_color >= 0) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2584 | WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2585 | pwq->work_color = work_color; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2588 | spin_unlock(&pool->lock); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2591 | local_irq_enable(); |
| 2592 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2593 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2594 | complete(&wq->first_flusher->done); |
| 2595 | |
| 2596 | return wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 2599 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 2601 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | * |
| 2603 | * Forces execution of the workqueue and blocks until its completion. |
| 2604 | * This is typically used in driver shutdown handlers. |
| 2605 | * |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 2606 | * We sleep until all works which were queued on entry have been handled, |
| 2607 | * but we are not livelocked by new incoming ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 2609 | void flush_workqueue(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2611 | struct wq_flusher this_flusher = { |
| 2612 | .list = LIST_HEAD_INIT(this_flusher.list), |
| 2613 | .flush_color = -1, |
| 2614 | .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), |
| 2615 | }; |
| 2616 | int next_color; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 2617 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 2618 | lock_map_acquire(&wq->lockdep_map); |
| 2619 | lock_map_release(&wq->lockdep_map); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2620 | |
| 2621 | mutex_lock(&wq->flush_mutex); |
| 2622 | |
| 2623 | /* |
| 2624 | * Start-to-wait phase |
| 2625 | */ |
| 2626 | next_color = work_next_color(wq->work_color); |
| 2627 | |
| 2628 | if (next_color != wq->flush_color) { |
| 2629 | /* |
| 2630 | * Color space is not full. The current work_color |
| 2631 | * becomes our flush_color and work_color is advanced |
| 2632 | * by one. |
| 2633 | */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2634 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2635 | this_flusher.flush_color = wq->work_color; |
| 2636 | wq->work_color = next_color; |
| 2637 | |
| 2638 | if (!wq->first_flusher) { |
| 2639 | /* no flush in progress, become the first flusher */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2640 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2641 | |
| 2642 | wq->first_flusher = &this_flusher; |
| 2643 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2644 | if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2645 | wq->work_color)) { |
| 2646 | /* nothing to flush, done */ |
| 2647 | wq->flush_color = next_color; |
| 2648 | wq->first_flusher = NULL; |
| 2649 | goto out_unlock; |
| 2650 | } |
| 2651 | } else { |
| 2652 | /* wait in queue */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2653 | WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2654 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2655 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2656 | } |
| 2657 | } else { |
| 2658 | /* |
| 2659 | * Oops, color space is full, wait on overflow queue. |
| 2660 | * The next flush completion will assign us |
| 2661 | * flush_color and transfer to flusher_queue. |
| 2662 | */ |
| 2663 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); |
| 2664 | } |
| 2665 | |
| 2666 | mutex_unlock(&wq->flush_mutex); |
| 2667 | |
| 2668 | wait_for_completion(&this_flusher.done); |
| 2669 | |
| 2670 | /* |
| 2671 | * Wake-up-and-cascade phase |
| 2672 | * |
| 2673 | * First flushers are responsible for cascading flushes and |
| 2674 | * handling overflow. Non-first flushers can simply return. |
| 2675 | */ |
| 2676 | if (wq->first_flusher != &this_flusher) |
| 2677 | return; |
| 2678 | |
| 2679 | mutex_lock(&wq->flush_mutex); |
| 2680 | |
Tejun Heo | 4ce48b3 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 2681 | /* we might have raced, check again with mutex held */ |
| 2682 | if (wq->first_flusher != &this_flusher) |
| 2683 | goto out_unlock; |
| 2684 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2685 | wq->first_flusher = NULL; |
| 2686 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2687 | WARN_ON_ONCE(!list_empty(&this_flusher.list)); |
| 2688 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2689 | |
| 2690 | while (true) { |
| 2691 | struct wq_flusher *next, *tmp; |
| 2692 | |
| 2693 | /* complete all the flushers sharing the current flush color */ |
| 2694 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { |
| 2695 | if (next->flush_color != wq->flush_color) |
| 2696 | break; |
| 2697 | list_del_init(&next->list); |
| 2698 | complete(&next->done); |
| 2699 | } |
| 2700 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2701 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && |
| 2702 | wq->flush_color != work_next_color(wq->work_color)); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2703 | |
| 2704 | /* this flush_color is finished, advance by one */ |
| 2705 | wq->flush_color = work_next_color(wq->flush_color); |
| 2706 | |
| 2707 | /* one color has been freed, handle overflow queue */ |
| 2708 | if (!list_empty(&wq->flusher_overflow)) { |
| 2709 | /* |
| 2710 | * Assign the same color to all overflowed |
| 2711 | * flushers, advance work_color and append to |
| 2712 | * flusher_queue. This is the start-to-wait |
| 2713 | * phase for these overflowed flushers. |
| 2714 | */ |
| 2715 | list_for_each_entry(tmp, &wq->flusher_overflow, list) |
| 2716 | tmp->flush_color = wq->work_color; |
| 2717 | |
| 2718 | wq->work_color = work_next_color(wq->work_color); |
| 2719 | |
| 2720 | list_splice_tail_init(&wq->flusher_overflow, |
| 2721 | &wq->flusher_queue); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2722 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | if (list_empty(&wq->flusher_queue)) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2726 | WARN_ON_ONCE(wq->flush_color != wq->work_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2727 | break; |
| 2728 | } |
| 2729 | |
| 2730 | /* |
| 2731 | * Need to flush more colors. Make the next flusher |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2732 | * the new first flusher and arm pwqs. |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2733 | */ |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2734 | WARN_ON_ONCE(wq->flush_color == wq->work_color); |
| 2735 | WARN_ON_ONCE(wq->flush_color != next->flush_color); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2736 | |
| 2737 | list_del_init(&next->list); |
| 2738 | wq->first_flusher = next; |
| 2739 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2740 | if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2741 | break; |
| 2742 | |
| 2743 | /* |
| 2744 | * Meh... this color is already done, clear first |
| 2745 | * flusher and repeat cascading. |
| 2746 | */ |
| 2747 | wq->first_flusher = NULL; |
| 2748 | } |
| 2749 | |
| 2750 | out_unlock: |
| 2751 | mutex_unlock(&wq->flush_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 2753 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2755 | /** |
| 2756 | * drain_workqueue - drain a workqueue |
| 2757 | * @wq: workqueue to drain |
| 2758 | * |
| 2759 | * Wait until the workqueue becomes empty. While draining is in progress, |
| 2760 | * only chain queueing is allowed. IOW, only currently pending or running |
| 2761 | * work items on @wq can queue further work items on it. @wq is flushed |
| 2762 | * repeatedly until it becomes empty. The number of flushing is detemined |
| 2763 | * by the depth of chaining and should be relatively short. Whine if it |
| 2764 | * takes too long. |
| 2765 | */ |
| 2766 | void drain_workqueue(struct workqueue_struct *wq) |
| 2767 | { |
| 2768 | unsigned int flush_cnt = 0; |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 2769 | struct pool_workqueue *pwq; |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2770 | |
| 2771 | /* |
| 2772 | * __queue_work() needs to test whether there are drainers, is much |
| 2773 | * hotter than drain_workqueue() and already looks at @wq->flags. |
Tejun Heo | 618b01e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 2774 | * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2775 | */ |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2776 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2777 | if (!wq->nr_drainers++) |
Tejun Heo | 618b01e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 2778 | wq->flags |= __WQ_DRAINING; |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 2779 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2780 | reflush: |
| 2781 | flush_workqueue(wq); |
| 2782 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2783 | local_irq_disable(); |
| 2784 | |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 2785 | for_each_pwq(pwq, wq) { |
Thomas Tuttle | fa2563e | 2011-09-14 16:22:28 -0700 | [diff] [blame] | 2786 | bool drained; |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2787 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2788 | spin_lock(&pwq->pool->lock); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2789 | drained = !pwq->nr_active && list_empty(&pwq->delayed_works); |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2790 | spin_unlock(&pwq->pool->lock); |
Thomas Tuttle | fa2563e | 2011-09-14 16:22:28 -0700 | [diff] [blame] | 2791 | |
| 2792 | if (drained) |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2793 | continue; |
| 2794 | |
| 2795 | if (++flush_cnt == 10 || |
| 2796 | (flush_cnt % 100 == 0 && flush_cnt <= 1000)) |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 2797 | pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", |
| 2798 | wq->name, flush_cnt); |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2799 | |
| 2800 | local_irq_enable(); |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2801 | goto reflush; |
| 2802 | } |
| 2803 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2804 | spin_lock(&workqueue_lock); |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2805 | if (!--wq->nr_drainers) |
Tejun Heo | 618b01e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 2806 | wq->flags &= ~__WQ_DRAINING; |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2807 | spin_unlock(&workqueue_lock); |
| 2808 | |
| 2809 | local_irq_enable(); |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 2810 | } |
| 2811 | EXPORT_SYMBOL_GPL(drain_workqueue); |
| 2812 | |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2813 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2814 | { |
| 2815 | struct worker *worker = NULL; |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2816 | struct worker_pool *pool; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2817 | struct pool_workqueue *pwq; |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2818 | |
| 2819 | might_sleep(); |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2820 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 2821 | local_irq_disable(); |
| 2822 | pool = get_work_pool(work); |
| 2823 | if (!pool) { |
| 2824 | local_irq_enable(); |
| 2825 | return false; |
| 2826 | } |
| 2827 | |
| 2828 | spin_lock(&pool->lock); |
Lai Jiangshan | 0b3dae6 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 2829 | /* see the comment in try_to_grab_pending() with the same code */ |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2830 | pwq = get_work_pwq(work); |
| 2831 | if (pwq) { |
| 2832 | if (unlikely(pwq->pool != pool)) |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2833 | goto already_gone; |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2834 | } else { |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2835 | worker = find_worker_executing_work(pool, work); |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2836 | if (!worker) |
| 2837 | goto already_gone; |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2838 | pwq = worker->current_pwq; |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2839 | } |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2840 | |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2841 | insert_wq_barrier(pwq, barr, work, worker); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2842 | spin_unlock_irq(&pool->lock); |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2843 | |
Tejun Heo | e159489b | 2011-01-09 23:32:15 +0100 | [diff] [blame] | 2844 | /* |
| 2845 | * If @max_active is 1 or rescuer is in use, flushing another work |
| 2846 | * item on the same workqueue may lead to deadlock. Make sure the |
| 2847 | * flusher is not running on the same workqueue by verifying write |
| 2848 | * access. |
| 2849 | */ |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 2850 | if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer) |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2851 | lock_map_acquire(&pwq->wq->lockdep_map); |
Tejun Heo | e159489b | 2011-01-09 23:32:15 +0100 | [diff] [blame] | 2852 | else |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 2853 | lock_map_acquire_read(&pwq->wq->lockdep_map); |
| 2854 | lock_map_release(&pwq->wq->lockdep_map); |
Tejun Heo | e159489b | 2011-01-09 23:32:15 +0100 | [diff] [blame] | 2855 | |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2856 | return true; |
| 2857 | already_gone: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2858 | spin_unlock_irq(&pool->lock); |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2859 | return false; |
| 2860 | } |
| 2861 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2862 | /** |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2863 | * flush_work - wait for a work to finish executing the last queueing instance |
| 2864 | * @work: the work to flush |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2865 | * |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2866 | * Wait until @work has finished execution. @work is guaranteed to be idle |
| 2867 | * on return if it hasn't been requeued since flush started. |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2868 | * |
| 2869 | * RETURNS: |
| 2870 | * %true if flush_work() waited for the work to finish execution, |
| 2871 | * %false if it was already idle. |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2872 | */ |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2873 | bool flush_work(struct work_struct *work) |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2874 | { |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2875 | struct wq_barrier barr; |
| 2876 | |
Stephen Boyd | 0976dfc | 2012-04-20 17:28:50 -0700 | [diff] [blame] | 2877 | lock_map_acquire(&work->lockdep_map); |
| 2878 | lock_map_release(&work->lockdep_map); |
| 2879 | |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2880 | if (start_flush_work(work, &barr)) { |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2881 | wait_for_completion(&barr.done); |
| 2882 | destroy_work_on_stack(&barr.work); |
| 2883 | return true; |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2884 | } else { |
Tejun Heo | baf5902 | 2010-09-16 10:42:16 +0200 | [diff] [blame] | 2885 | return false; |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2886 | } |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 2887 | } |
| 2888 | EXPORT_SYMBOL_GPL(flush_work); |
| 2889 | |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 2890 | static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2891 | { |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 2892 | unsigned long flags; |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 2893 | int ret; |
| 2894 | |
| 2895 | do { |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 2896 | ret = try_to_grab_pending(work, is_dwork, &flags); |
| 2897 | /* |
| 2898 | * If someone else is canceling, wait for the same event it |
| 2899 | * would be waiting for before retrying. |
| 2900 | */ |
| 2901 | if (unlikely(ret == -ENOENT)) |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2902 | flush_work(work); |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 2903 | } while (unlikely(ret < 0)); |
| 2904 | |
Tejun Heo | bbb68df | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 2905 | /* tell other tasks trying to grab @work to back off */ |
| 2906 | mark_work_canceling(work); |
| 2907 | local_irq_restore(flags); |
| 2908 | |
Tejun Heo | 606a502 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 2909 | flush_work(work); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2910 | clear_work_data(work); |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 2911 | return ret; |
| 2912 | } |
| 2913 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2914 | /** |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2915 | * cancel_work_sync - cancel a work and wait for it to finish |
| 2916 | * @work: the work to cancel |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2917 | * |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2918 | * Cancel @work and wait for its execution to finish. This function |
| 2919 | * can be used even if the work re-queues itself or migrates to |
| 2920 | * another workqueue. On return from this function, @work is |
| 2921 | * guaranteed to be not pending or executing on any CPU. |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 2922 | * |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2923 | * cancel_work_sync(&delayed_work->work) must not be used for |
| 2924 | * delayed_work's. Use cancel_delayed_work_sync() instead. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2925 | * |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2926 | * The caller must ensure that the workqueue on which @work was last |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2927 | * queued can't be destroyed before this function returns. |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2928 | * |
| 2929 | * RETURNS: |
| 2930 | * %true if @work was pending, %false otherwise. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2931 | */ |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2932 | bool cancel_work_sync(struct work_struct *work) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2933 | { |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 2934 | return __cancel_work_timer(work, false); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 2935 | } |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 2936 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 2937 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2938 | /** |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2939 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
| 2940 | * @dwork: the delayed work to flush |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2941 | * |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2942 | * Delayed timer is cancelled and the pending work is queued for |
| 2943 | * immediate execution. Like flush_work(), this function only |
| 2944 | * considers the last queueing instance of @dwork. |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 2945 | * |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2946 | * RETURNS: |
| 2947 | * %true if flush_work() waited for the work to finish execution, |
| 2948 | * %false if it was already idle. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 2949 | */ |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2950 | bool flush_delayed_work(struct delayed_work *dwork) |
| 2951 | { |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 2952 | local_irq_disable(); |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2953 | if (del_timer_sync(&dwork->timer)) |
Lai Jiangshan | 60c057b | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 2954 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
Tejun Heo | 8930cab | 2012-08-03 10:30:45 -0700 | [diff] [blame] | 2955 | local_irq_enable(); |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2956 | return flush_work(&dwork->work); |
| 2957 | } |
| 2958 | EXPORT_SYMBOL(flush_delayed_work); |
| 2959 | |
| 2960 | /** |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2961 | * cancel_delayed_work - cancel a delayed work |
| 2962 | * @dwork: delayed_work to cancel |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2963 | * |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2964 | * Kill off a pending delayed_work. Returns %true if @dwork was pending |
| 2965 | * and canceled; %false if wasn't pending. Note that the work callback |
| 2966 | * function may still be running on return, unless it returns %true and the |
| 2967 | * work doesn't re-arm itself. Explicitly flush or use |
| 2968 | * cancel_delayed_work_sync() to wait on it. |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2969 | * |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2970 | * This function is safe to call from any context including IRQ handler. |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2971 | */ |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2972 | bool cancel_delayed_work(struct delayed_work *dwork) |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2973 | { |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2974 | unsigned long flags; |
| 2975 | int ret; |
| 2976 | |
| 2977 | do { |
| 2978 | ret = try_to_grab_pending(&dwork->work, true, &flags); |
| 2979 | } while (unlikely(ret == -EAGAIN)); |
| 2980 | |
| 2981 | if (unlikely(ret < 0)) |
| 2982 | return false; |
| 2983 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 2984 | set_work_pool_and_clear_pending(&dwork->work, |
| 2985 | get_work_pool_id(&dwork->work)); |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2986 | local_irq_restore(flags); |
Dan Magenheimer | c0158ca | 2012-10-18 16:31:37 -0700 | [diff] [blame] | 2987 | return ret; |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2988 | } |
Tejun Heo | 57b30ae | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 2989 | EXPORT_SYMBOL(cancel_delayed_work); |
Tejun Heo | 0938349 | 2010-09-16 10:48:29 +0200 | [diff] [blame] | 2990 | |
| 2991 | /** |
Tejun Heo | 401a8d0 | 2010-09-16 10:36:00 +0200 | [diff] [blame] | 2992 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish |
| 2993 | * @dwork: the delayed work cancel |
| 2994 | * |
| 2995 | * This is cancel_work_sync() for delayed works. |
| 2996 | * |
| 2997 | * RETURNS: |
| 2998 | * %true if @dwork was pending, %false otherwise. |
| 2999 | */ |
| 3000 | bool cancel_delayed_work_sync(struct delayed_work *dwork) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 3001 | { |
Tejun Heo | 36e227d | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 3002 | return __cancel_work_timer(&dwork->work, true); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 3003 | } |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 3004 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3006 | /** |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 3007 | * schedule_work_on - put work task on a specific cpu |
| 3008 | * @cpu: cpu to put the work task on |
| 3009 | * @work: job to be done |
| 3010 | * |
| 3011 | * This puts a job on a specific cpu |
| 3012 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3013 | bool schedule_work_on(int cpu, struct work_struct *work) |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 3014 | { |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3015 | return queue_work_on(cpu, system_wq, work); |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 3016 | } |
| 3017 | EXPORT_SYMBOL(schedule_work_on); |
| 3018 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3019 | /** |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 3020 | * schedule_work - put work task in global workqueue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | * @work: job to be done |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | * |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3023 | * Returns %false if @work was already on the kernel-global workqueue and |
| 3024 | * %true otherwise. |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 3025 | * |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3026 | * This puts a job in the kernel-global workqueue if it was not already |
| 3027 | * queued and leaves it in the same position on the kernel-global |
| 3028 | * workqueue otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3030 | bool schedule_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | { |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3032 | return queue_work(system_wq, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | } |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3034 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 3036 | /** |
| 3037 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 3038 | * @cpu: cpu to use |
| 3039 | * @dwork: job to be done |
| 3040 | * @delay: number of jiffies to wait |
| 3041 | * |
| 3042 | * After waiting for a given time this puts a job in the kernel-global |
| 3043 | * workqueue on the specified CPU. |
| 3044 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3045 | bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, |
| 3046 | unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | { |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3048 | return queue_delayed_work_on(cpu, system_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 3050 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3052 | /** |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3053 | * schedule_delayed_work - put work task in global workqueue after delay |
| 3054 | * @dwork: job to be done |
| 3055 | * @delay: number of jiffies to wait or 0 for immediate execution |
| 3056 | * |
| 3057 | * After waiting for a given time this puts a job in the kernel-global |
| 3058 | * workqueue. |
| 3059 | */ |
Tejun Heo | d4283e9 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3060 | bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) |
Tejun Heo | 0a13c00 | 2012-08-03 10:30:44 -0700 | [diff] [blame] | 3061 | { |
| 3062 | return queue_delayed_work(system_wq, dwork, delay); |
| 3063 | } |
| 3064 | EXPORT_SYMBOL(schedule_delayed_work); |
| 3065 | |
| 3066 | /** |
Tejun Heo | 31ddd87 | 2010-10-19 11:14:49 +0200 | [diff] [blame] | 3067 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3068 | * @func: the function to call |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3069 | * |
Tejun Heo | 31ddd87 | 2010-10-19 11:14:49 +0200 | [diff] [blame] | 3070 | * schedule_on_each_cpu() executes @func on each online CPU using the |
| 3071 | * system workqueue and blocks until all CPUs have completed. |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3072 | * schedule_on_each_cpu() is very slow. |
Tejun Heo | 31ddd87 | 2010-10-19 11:14:49 +0200 | [diff] [blame] | 3073 | * |
| 3074 | * RETURNS: |
| 3075 | * 0 on success, -errno on failure. |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3076 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3077 | int schedule_on_each_cpu(work_func_t func) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 3078 | { |
| 3079 | int cpu; |
Namhyung Kim | 38f5156 | 2010-08-08 14:24:09 +0200 | [diff] [blame] | 3080 | struct work_struct __percpu *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 3081 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3082 | works = alloc_percpu(struct work_struct); |
| 3083 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 3084 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3085 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 3086 | get_online_cpus(); |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 3087 | |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 3088 | for_each_online_cpu(cpu) { |
Ingo Molnar | 9bfb183 | 2006-12-18 20:05:09 +0100 | [diff] [blame] | 3089 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 3090 | |
| 3091 | INIT_WORK(work, func); |
Tejun Heo | b71ab8c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3092 | schedule_work_on(cpu, work); |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 3093 | } |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 3094 | |
| 3095 | for_each_online_cpu(cpu) |
| 3096 | flush_work(per_cpu_ptr(works, cpu)); |
| 3097 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 3098 | put_online_cpus(); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 3099 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 3100 | return 0; |
| 3101 | } |
| 3102 | |
Alan Stern | eef6a7d | 2010-02-12 17:39:21 +0900 | [diff] [blame] | 3103 | /** |
| 3104 | * flush_scheduled_work - ensure that any scheduled work has run to completion. |
| 3105 | * |
| 3106 | * Forces execution of the kernel-global workqueue and blocks until its |
| 3107 | * completion. |
| 3108 | * |
| 3109 | * Think twice before calling this function! It's very easy to get into |
| 3110 | * trouble if you don't take great care. Either of the following situations |
| 3111 | * will lead to deadlock: |
| 3112 | * |
| 3113 | * One of the work items currently on the workqueue needs to acquire |
| 3114 | * a lock held by your code or its caller. |
| 3115 | * |
| 3116 | * Your code is running in the context of a work routine. |
| 3117 | * |
| 3118 | * They will be detected by lockdep when they occur, but the first might not |
| 3119 | * occur very often. It depends on what work items are on the workqueue and |
| 3120 | * what locks they need, which you have no control over. |
| 3121 | * |
| 3122 | * In most situations flushing the entire workqueue is overkill; you merely |
| 3123 | * need to know that a particular work item isn't queued and isn't running. |
| 3124 | * In such cases you should use cancel_delayed_work_sync() or |
| 3125 | * cancel_work_sync() instead. |
| 3126 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | void flush_scheduled_work(void) |
| 3128 | { |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3129 | flush_workqueue(system_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 3131 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | |
| 3133 | /** |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 3134 | * execute_in_process_context - reliably execute the routine with user context |
| 3135 | * @fn: the function to execute |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 3136 | * @ew: guaranteed storage for the execute work structure (must |
| 3137 | * be available when the work executes) |
| 3138 | * |
| 3139 | * Executes the function immediately if process context is available, |
| 3140 | * otherwise schedules the function for delayed execution. |
| 3141 | * |
| 3142 | * Returns: 0 - function was executed |
| 3143 | * 1 - function was scheduled for execution |
| 3144 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3145 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 3146 | { |
| 3147 | if (!in_interrupt()) { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3148 | fn(&ew->work); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3152 | INIT_WORK(&ew->work, fn); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 3153 | schedule_work(&ew->work); |
| 3154 | |
| 3155 | return 1; |
| 3156 | } |
| 3157 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 3158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | int keventd_up(void) |
| 3160 | { |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3161 | return system_wq != NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 3164 | #ifdef CONFIG_SYSFS |
| 3165 | /* |
| 3166 | * Workqueues with WQ_SYSFS flag set is visible to userland via |
| 3167 | * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the |
| 3168 | * following attributes. |
| 3169 | * |
| 3170 | * per_cpu RO bool : whether the workqueue is per-cpu or unbound |
| 3171 | * max_active RW int : maximum number of in-flight work items |
| 3172 | * |
| 3173 | * Unbound workqueues have the following extra attributes. |
| 3174 | * |
| 3175 | * id RO int : the associated pool ID |
| 3176 | * nice RW int : nice value of the workers |
| 3177 | * cpumask RW mask : bitmask of allowed CPUs for the workers |
| 3178 | */ |
| 3179 | struct wq_device { |
| 3180 | struct workqueue_struct *wq; |
| 3181 | struct device dev; |
| 3182 | }; |
| 3183 | |
| 3184 | static struct workqueue_struct *dev_to_wq(struct device *dev) |
| 3185 | { |
| 3186 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
| 3187 | |
| 3188 | return wq_dev->wq; |
| 3189 | } |
| 3190 | |
| 3191 | static ssize_t wq_per_cpu_show(struct device *dev, |
| 3192 | struct device_attribute *attr, char *buf) |
| 3193 | { |
| 3194 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3195 | |
| 3196 | return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); |
| 3197 | } |
| 3198 | |
| 3199 | static ssize_t wq_max_active_show(struct device *dev, |
| 3200 | struct device_attribute *attr, char *buf) |
| 3201 | { |
| 3202 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3203 | |
| 3204 | return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); |
| 3205 | } |
| 3206 | |
| 3207 | static ssize_t wq_max_active_store(struct device *dev, |
| 3208 | struct device_attribute *attr, |
| 3209 | const char *buf, size_t count) |
| 3210 | { |
| 3211 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3212 | int val; |
| 3213 | |
| 3214 | if (sscanf(buf, "%d", &val) != 1 || val <= 0) |
| 3215 | return -EINVAL; |
| 3216 | |
| 3217 | workqueue_set_max_active(wq, val); |
| 3218 | return count; |
| 3219 | } |
| 3220 | |
| 3221 | static struct device_attribute wq_sysfs_attrs[] = { |
| 3222 | __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL), |
| 3223 | __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store), |
| 3224 | __ATTR_NULL, |
| 3225 | }; |
| 3226 | |
| 3227 | static ssize_t wq_pool_id_show(struct device *dev, |
| 3228 | struct device_attribute *attr, char *buf) |
| 3229 | { |
| 3230 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3231 | struct worker_pool *pool; |
| 3232 | int written; |
| 3233 | |
| 3234 | rcu_read_lock_sched(); |
| 3235 | pool = first_pwq(wq)->pool; |
| 3236 | written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id); |
| 3237 | rcu_read_unlock_sched(); |
| 3238 | |
| 3239 | return written; |
| 3240 | } |
| 3241 | |
| 3242 | static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, |
| 3243 | char *buf) |
| 3244 | { |
| 3245 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3246 | int written; |
| 3247 | |
| 3248 | rcu_read_lock_sched(); |
| 3249 | written = scnprintf(buf, PAGE_SIZE, "%d\n", |
| 3250 | first_pwq(wq)->pool->attrs->nice); |
| 3251 | rcu_read_unlock_sched(); |
| 3252 | |
| 3253 | return written; |
| 3254 | } |
| 3255 | |
| 3256 | /* prepare workqueue_attrs for sysfs store operations */ |
| 3257 | static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) |
| 3258 | { |
| 3259 | struct workqueue_attrs *attrs; |
| 3260 | |
| 3261 | attrs = alloc_workqueue_attrs(GFP_KERNEL); |
| 3262 | if (!attrs) |
| 3263 | return NULL; |
| 3264 | |
| 3265 | rcu_read_lock_sched(); |
| 3266 | copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs); |
| 3267 | rcu_read_unlock_sched(); |
| 3268 | return attrs; |
| 3269 | } |
| 3270 | |
| 3271 | static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, |
| 3272 | const char *buf, size_t count) |
| 3273 | { |
| 3274 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3275 | struct workqueue_attrs *attrs; |
| 3276 | int ret; |
| 3277 | |
| 3278 | attrs = wq_sysfs_prep_attrs(wq); |
| 3279 | if (!attrs) |
| 3280 | return -ENOMEM; |
| 3281 | |
| 3282 | if (sscanf(buf, "%d", &attrs->nice) == 1 && |
| 3283 | attrs->nice >= -20 && attrs->nice <= 19) |
| 3284 | ret = apply_workqueue_attrs(wq, attrs); |
| 3285 | else |
| 3286 | ret = -EINVAL; |
| 3287 | |
| 3288 | free_workqueue_attrs(attrs); |
| 3289 | return ret ?: count; |
| 3290 | } |
| 3291 | |
| 3292 | static ssize_t wq_cpumask_show(struct device *dev, |
| 3293 | struct device_attribute *attr, char *buf) |
| 3294 | { |
| 3295 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3296 | int written; |
| 3297 | |
| 3298 | rcu_read_lock_sched(); |
| 3299 | written = cpumask_scnprintf(buf, PAGE_SIZE, |
| 3300 | first_pwq(wq)->pool->attrs->cpumask); |
| 3301 | rcu_read_unlock_sched(); |
| 3302 | |
| 3303 | written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); |
| 3304 | return written; |
| 3305 | } |
| 3306 | |
| 3307 | static ssize_t wq_cpumask_store(struct device *dev, |
| 3308 | struct device_attribute *attr, |
| 3309 | const char *buf, size_t count) |
| 3310 | { |
| 3311 | struct workqueue_struct *wq = dev_to_wq(dev); |
| 3312 | struct workqueue_attrs *attrs; |
| 3313 | int ret; |
| 3314 | |
| 3315 | attrs = wq_sysfs_prep_attrs(wq); |
| 3316 | if (!attrs) |
| 3317 | return -ENOMEM; |
| 3318 | |
| 3319 | ret = cpumask_parse(buf, attrs->cpumask); |
| 3320 | if (!ret) |
| 3321 | ret = apply_workqueue_attrs(wq, attrs); |
| 3322 | |
| 3323 | free_workqueue_attrs(attrs); |
| 3324 | return ret ?: count; |
| 3325 | } |
| 3326 | |
| 3327 | static struct device_attribute wq_sysfs_unbound_attrs[] = { |
| 3328 | __ATTR(pool_id, 0444, wq_pool_id_show, NULL), |
| 3329 | __ATTR(nice, 0644, wq_nice_show, wq_nice_store), |
| 3330 | __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), |
| 3331 | __ATTR_NULL, |
| 3332 | }; |
| 3333 | |
| 3334 | static struct bus_type wq_subsys = { |
| 3335 | .name = "workqueue", |
| 3336 | .dev_attrs = wq_sysfs_attrs, |
| 3337 | }; |
| 3338 | |
| 3339 | static int __init wq_sysfs_init(void) |
| 3340 | { |
| 3341 | return subsys_virtual_register(&wq_subsys, NULL); |
| 3342 | } |
| 3343 | core_initcall(wq_sysfs_init); |
| 3344 | |
| 3345 | static void wq_device_release(struct device *dev) |
| 3346 | { |
| 3347 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
| 3348 | |
| 3349 | kfree(wq_dev); |
| 3350 | } |
| 3351 | |
| 3352 | /** |
| 3353 | * workqueue_sysfs_register - make a workqueue visible in sysfs |
| 3354 | * @wq: the workqueue to register |
| 3355 | * |
| 3356 | * Expose @wq in sysfs under /sys/bus/workqueue/devices. |
| 3357 | * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set |
| 3358 | * which is the preferred method. |
| 3359 | * |
| 3360 | * Workqueue user should use this function directly iff it wants to apply |
| 3361 | * workqueue_attrs before making the workqueue visible in sysfs; otherwise, |
| 3362 | * apply_workqueue_attrs() may race against userland updating the |
| 3363 | * attributes. |
| 3364 | * |
| 3365 | * Returns 0 on success, -errno on failure. |
| 3366 | */ |
| 3367 | int workqueue_sysfs_register(struct workqueue_struct *wq) |
| 3368 | { |
| 3369 | struct wq_device *wq_dev; |
| 3370 | int ret; |
| 3371 | |
| 3372 | /* |
| 3373 | * Adjusting max_active or creating new pwqs by applyting |
| 3374 | * attributes breaks ordering guarantee. Disallow exposing ordered |
| 3375 | * workqueues. |
| 3376 | */ |
| 3377 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
| 3378 | return -EINVAL; |
| 3379 | |
| 3380 | wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); |
| 3381 | if (!wq_dev) |
| 3382 | return -ENOMEM; |
| 3383 | |
| 3384 | wq_dev->wq = wq; |
| 3385 | wq_dev->dev.bus = &wq_subsys; |
| 3386 | wq_dev->dev.init_name = wq->name; |
| 3387 | wq_dev->dev.release = wq_device_release; |
| 3388 | |
| 3389 | /* |
| 3390 | * unbound_attrs are created separately. Suppress uevent until |
| 3391 | * everything is ready. |
| 3392 | */ |
| 3393 | dev_set_uevent_suppress(&wq_dev->dev, true); |
| 3394 | |
| 3395 | ret = device_register(&wq_dev->dev); |
| 3396 | if (ret) { |
| 3397 | kfree(wq_dev); |
| 3398 | wq->wq_dev = NULL; |
| 3399 | return ret; |
| 3400 | } |
| 3401 | |
| 3402 | if (wq->flags & WQ_UNBOUND) { |
| 3403 | struct device_attribute *attr; |
| 3404 | |
| 3405 | for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { |
| 3406 | ret = device_create_file(&wq_dev->dev, attr); |
| 3407 | if (ret) { |
| 3408 | device_unregister(&wq_dev->dev); |
| 3409 | wq->wq_dev = NULL; |
| 3410 | return ret; |
| 3411 | } |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); |
| 3416 | return 0; |
| 3417 | } |
| 3418 | |
| 3419 | /** |
| 3420 | * workqueue_sysfs_unregister - undo workqueue_sysfs_register() |
| 3421 | * @wq: the workqueue to unregister |
| 3422 | * |
| 3423 | * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. |
| 3424 | */ |
| 3425 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) |
| 3426 | { |
| 3427 | struct wq_device *wq_dev = wq->wq_dev; |
| 3428 | |
| 3429 | if (!wq->wq_dev) |
| 3430 | return; |
| 3431 | |
| 3432 | wq->wq_dev = NULL; |
| 3433 | device_unregister(&wq_dev->dev); |
| 3434 | } |
| 3435 | #else /* CONFIG_SYSFS */ |
| 3436 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } |
| 3437 | #endif /* CONFIG_SYSFS */ |
| 3438 | |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3439 | /** |
| 3440 | * free_workqueue_attrs - free a workqueue_attrs |
| 3441 | * @attrs: workqueue_attrs to free |
| 3442 | * |
| 3443 | * Undo alloc_workqueue_attrs(). |
| 3444 | */ |
| 3445 | void free_workqueue_attrs(struct workqueue_attrs *attrs) |
| 3446 | { |
| 3447 | if (attrs) { |
| 3448 | free_cpumask_var(attrs->cpumask); |
| 3449 | kfree(attrs); |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * alloc_workqueue_attrs - allocate a workqueue_attrs |
| 3455 | * @gfp_mask: allocation mask to use |
| 3456 | * |
| 3457 | * Allocate a new workqueue_attrs, initialize with default settings and |
| 3458 | * return it. Returns NULL on failure. |
| 3459 | */ |
| 3460 | struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) |
| 3461 | { |
| 3462 | struct workqueue_attrs *attrs; |
| 3463 | |
| 3464 | attrs = kzalloc(sizeof(*attrs), gfp_mask); |
| 3465 | if (!attrs) |
| 3466 | goto fail; |
| 3467 | if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) |
| 3468 | goto fail; |
| 3469 | |
| 3470 | cpumask_setall(attrs->cpumask); |
| 3471 | return attrs; |
| 3472 | fail: |
| 3473 | free_workqueue_attrs(attrs); |
| 3474 | return NULL; |
| 3475 | } |
| 3476 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3477 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
| 3478 | const struct workqueue_attrs *from) |
| 3479 | { |
| 3480 | to->nice = from->nice; |
| 3481 | cpumask_copy(to->cpumask, from->cpumask); |
| 3482 | } |
| 3483 | |
| 3484 | /* |
| 3485 | * Hacky implementation of jhash of bitmaps which only considers the |
| 3486 | * specified number of bits. We probably want a proper implementation in |
| 3487 | * include/linux/jhash.h. |
| 3488 | */ |
| 3489 | static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash) |
| 3490 | { |
| 3491 | int nr_longs = bits / BITS_PER_LONG; |
| 3492 | int nr_leftover = bits % BITS_PER_LONG; |
| 3493 | unsigned long leftover = 0; |
| 3494 | |
| 3495 | if (nr_longs) |
| 3496 | hash = jhash(bitmap, nr_longs * sizeof(long), hash); |
| 3497 | if (nr_leftover) { |
| 3498 | bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover); |
| 3499 | hash = jhash(&leftover, sizeof(long), hash); |
| 3500 | } |
| 3501 | return hash; |
| 3502 | } |
| 3503 | |
| 3504 | /* hash value of the content of @attr */ |
| 3505 | static u32 wqattrs_hash(const struct workqueue_attrs *attrs) |
| 3506 | { |
| 3507 | u32 hash = 0; |
| 3508 | |
| 3509 | hash = jhash_1word(attrs->nice, hash); |
| 3510 | hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash); |
| 3511 | return hash; |
| 3512 | } |
| 3513 | |
| 3514 | /* content equality test */ |
| 3515 | static bool wqattrs_equal(const struct workqueue_attrs *a, |
| 3516 | const struct workqueue_attrs *b) |
| 3517 | { |
| 3518 | if (a->nice != b->nice) |
| 3519 | return false; |
| 3520 | if (!cpumask_equal(a->cpumask, b->cpumask)) |
| 3521 | return false; |
| 3522 | return true; |
| 3523 | } |
| 3524 | |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3525 | /** |
| 3526 | * init_worker_pool - initialize a newly zalloc'd worker_pool |
| 3527 | * @pool: worker_pool to initialize |
| 3528 | * |
| 3529 | * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs. |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3530 | * Returns 0 on success, -errno on failure. Even on failure, all fields |
| 3531 | * inside @pool proper are initialized and put_unbound_pool() can be called |
| 3532 | * on @pool safely to release it. |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3533 | */ |
| 3534 | static int init_worker_pool(struct worker_pool *pool) |
Tejun Heo | 4e1a1f9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3535 | { |
| 3536 | spin_lock_init(&pool->lock); |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3537 | pool->id = -1; |
| 3538 | pool->cpu = -1; |
Tejun Heo | 4e1a1f9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3539 | pool->flags |= POOL_DISASSOCIATED; |
| 3540 | INIT_LIST_HEAD(&pool->worklist); |
| 3541 | INIT_LIST_HEAD(&pool->idle_list); |
| 3542 | hash_init(pool->busy_hash); |
| 3543 | |
| 3544 | init_timer_deferrable(&pool->idle_timer); |
| 3545 | pool->idle_timer.function = idle_worker_timeout; |
| 3546 | pool->idle_timer.data = (unsigned long)pool; |
| 3547 | |
| 3548 | setup_timer(&pool->mayday_timer, pool_mayday_timeout, |
| 3549 | (unsigned long)pool); |
| 3550 | |
| 3551 | mutex_init(&pool->manager_arb); |
| 3552 | mutex_init(&pool->assoc_mutex); |
| 3553 | ida_init(&pool->worker_ida); |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3554 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3555 | INIT_HLIST_NODE(&pool->hash_node); |
| 3556 | pool->refcnt = 1; |
| 3557 | |
| 3558 | /* shouldn't fail above this point */ |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3559 | pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); |
| 3560 | if (!pool->attrs) |
| 3561 | return -ENOMEM; |
| 3562 | return 0; |
Tejun Heo | 4e1a1f9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3565 | static void rcu_free_pool(struct rcu_head *rcu) |
| 3566 | { |
| 3567 | struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); |
| 3568 | |
| 3569 | ida_destroy(&pool->worker_ida); |
| 3570 | free_workqueue_attrs(pool->attrs); |
| 3571 | kfree(pool); |
| 3572 | } |
| 3573 | |
| 3574 | /** |
| 3575 | * put_unbound_pool - put a worker_pool |
| 3576 | * @pool: worker_pool to put |
| 3577 | * |
| 3578 | * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU |
| 3579 | * safe manner. |
| 3580 | */ |
| 3581 | static void put_unbound_pool(struct worker_pool *pool) |
| 3582 | { |
| 3583 | struct worker *worker; |
| 3584 | |
| 3585 | spin_lock_irq(&workqueue_lock); |
| 3586 | if (--pool->refcnt) { |
| 3587 | spin_unlock_irq(&workqueue_lock); |
| 3588 | return; |
| 3589 | } |
| 3590 | |
| 3591 | /* sanity checks */ |
| 3592 | if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) || |
| 3593 | WARN_ON(!list_empty(&pool->worklist))) { |
| 3594 | spin_unlock_irq(&workqueue_lock); |
| 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | /* release id and unhash */ |
| 3599 | if (pool->id >= 0) |
| 3600 | idr_remove(&worker_pool_idr, pool->id); |
| 3601 | hash_del(&pool->hash_node); |
| 3602 | |
| 3603 | spin_unlock_irq(&workqueue_lock); |
| 3604 | |
| 3605 | /* lock out manager and destroy all workers */ |
| 3606 | mutex_lock(&pool->manager_arb); |
| 3607 | spin_lock_irq(&pool->lock); |
| 3608 | |
| 3609 | while ((worker = first_worker(pool))) |
| 3610 | destroy_worker(worker); |
| 3611 | WARN_ON(pool->nr_workers || pool->nr_idle); |
| 3612 | |
| 3613 | spin_unlock_irq(&pool->lock); |
| 3614 | mutex_unlock(&pool->manager_arb); |
| 3615 | |
| 3616 | /* shut down the timers */ |
| 3617 | del_timer_sync(&pool->idle_timer); |
| 3618 | del_timer_sync(&pool->mayday_timer); |
| 3619 | |
| 3620 | /* sched-RCU protected to allow dereferences from get_work_pool() */ |
| 3621 | call_rcu_sched(&pool->rcu, rcu_free_pool); |
| 3622 | } |
| 3623 | |
| 3624 | /** |
| 3625 | * get_unbound_pool - get a worker_pool with the specified attributes |
| 3626 | * @attrs: the attributes of the worker_pool to get |
| 3627 | * |
| 3628 | * Obtain a worker_pool which has the same attributes as @attrs, bump the |
| 3629 | * reference count and return it. If there already is a matching |
| 3630 | * worker_pool, it will be used; otherwise, this function attempts to |
| 3631 | * create a new one. On failure, returns NULL. |
| 3632 | */ |
| 3633 | static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) |
| 3634 | { |
| 3635 | static DEFINE_MUTEX(create_mutex); |
| 3636 | u32 hash = wqattrs_hash(attrs); |
| 3637 | struct worker_pool *pool; |
| 3638 | struct worker *worker; |
| 3639 | |
| 3640 | mutex_lock(&create_mutex); |
| 3641 | |
| 3642 | /* do we already have a matching pool? */ |
| 3643 | spin_lock_irq(&workqueue_lock); |
| 3644 | hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { |
| 3645 | if (wqattrs_equal(pool->attrs, attrs)) { |
| 3646 | pool->refcnt++; |
| 3647 | goto out_unlock; |
| 3648 | } |
| 3649 | } |
| 3650 | spin_unlock_irq(&workqueue_lock); |
| 3651 | |
| 3652 | /* nope, create a new one */ |
| 3653 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
| 3654 | if (!pool || init_worker_pool(pool) < 0) |
| 3655 | goto fail; |
| 3656 | |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3657 | lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3658 | copy_workqueue_attrs(pool->attrs, attrs); |
| 3659 | |
| 3660 | if (worker_pool_assign_id(pool) < 0) |
| 3661 | goto fail; |
| 3662 | |
| 3663 | /* create and start the initial worker */ |
| 3664 | worker = create_worker(pool); |
| 3665 | if (!worker) |
| 3666 | goto fail; |
| 3667 | |
| 3668 | spin_lock_irq(&pool->lock); |
| 3669 | start_worker(worker); |
| 3670 | spin_unlock_irq(&pool->lock); |
| 3671 | |
| 3672 | /* install */ |
| 3673 | spin_lock_irq(&workqueue_lock); |
| 3674 | hash_add(unbound_pool_hash, &pool->hash_node, hash); |
| 3675 | out_unlock: |
| 3676 | spin_unlock_irq(&workqueue_lock); |
| 3677 | mutex_unlock(&create_mutex); |
| 3678 | return pool; |
| 3679 | fail: |
| 3680 | mutex_unlock(&create_mutex); |
| 3681 | if (pool) |
| 3682 | put_unbound_pool(pool); |
| 3683 | return NULL; |
| 3684 | } |
| 3685 | |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3686 | static void rcu_free_pwq(struct rcu_head *rcu) |
| 3687 | { |
| 3688 | kmem_cache_free(pwq_cache, |
| 3689 | container_of(rcu, struct pool_workqueue, rcu)); |
| 3690 | } |
| 3691 | |
| 3692 | /* |
| 3693 | * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt |
| 3694 | * and needs to be destroyed. |
| 3695 | */ |
| 3696 | static void pwq_unbound_release_workfn(struct work_struct *work) |
| 3697 | { |
| 3698 | struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, |
| 3699 | unbound_release_work); |
| 3700 | struct workqueue_struct *wq = pwq->wq; |
| 3701 | struct worker_pool *pool = pwq->pool; |
| 3702 | |
| 3703 | if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) |
| 3704 | return; |
| 3705 | |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3706 | /* |
| 3707 | * Unlink @pwq. Synchronization against flush_mutex isn't strictly |
| 3708 | * necessary on release but do it anyway. It's easier to verify |
| 3709 | * and consistent with the linking path. |
| 3710 | */ |
| 3711 | mutex_lock(&wq->flush_mutex); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3712 | spin_lock_irq(&workqueue_lock); |
| 3713 | list_del_rcu(&pwq->pwqs_node); |
| 3714 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3715 | mutex_unlock(&wq->flush_mutex); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3716 | |
| 3717 | put_unbound_pool(pool); |
| 3718 | call_rcu_sched(&pwq->rcu, rcu_free_pwq); |
| 3719 | |
| 3720 | /* |
| 3721 | * If we're the last pwq going away, @wq is already dead and no one |
| 3722 | * is gonna access it anymore. Free it. |
| 3723 | */ |
| 3724 | if (list_empty(&wq->pwqs)) |
| 3725 | kfree(wq); |
| 3726 | } |
| 3727 | |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3728 | /** |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3729 | * pwq_adjust_max_active - update a pwq's max_active to the current setting |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3730 | * @pwq: target pool_workqueue |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3731 | * |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3732 | * If @pwq isn't freezing, set @pwq->max_active to the associated |
| 3733 | * workqueue's saved_max_active and activate delayed work items |
| 3734 | * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3735 | */ |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3736 | static void pwq_adjust_max_active(struct pool_workqueue *pwq) |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3737 | { |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3738 | struct workqueue_struct *wq = pwq->wq; |
| 3739 | bool freezable = wq->flags & WQ_FREEZABLE; |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3740 | |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3741 | /* for @wq->saved_max_active */ |
| 3742 | lockdep_assert_held(&workqueue_lock); |
| 3743 | |
| 3744 | /* fast exit for non-freezable wqs */ |
| 3745 | if (!freezable && pwq->max_active == wq->saved_max_active) |
| 3746 | return; |
| 3747 | |
| 3748 | spin_lock(&pwq->pool->lock); |
| 3749 | |
| 3750 | if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) { |
| 3751 | pwq->max_active = wq->saved_max_active; |
| 3752 | |
| 3753 | while (!list_empty(&pwq->delayed_works) && |
| 3754 | pwq->nr_active < pwq->max_active) |
| 3755 | pwq_activate_first_delayed(pwq); |
| 3756 | } else { |
| 3757 | pwq->max_active = 0; |
| 3758 | } |
| 3759 | |
| 3760 | spin_unlock(&pwq->pool->lock); |
Tejun Heo | 0fbd95a | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3763 | static void init_and_link_pwq(struct pool_workqueue *pwq, |
| 3764 | struct workqueue_struct *wq, |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3765 | struct worker_pool *pool, |
| 3766 | struct pool_workqueue **p_last_pwq) |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3767 | { |
| 3768 | BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); |
| 3769 | |
| 3770 | pwq->pool = pool; |
| 3771 | pwq->wq = wq; |
| 3772 | pwq->flush_color = -1; |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3773 | pwq->refcnt = 1; |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3774 | INIT_LIST_HEAD(&pwq->delayed_works); |
| 3775 | INIT_LIST_HEAD(&pwq->mayday_node); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3776 | INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3777 | |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3778 | mutex_lock(&wq->flush_mutex); |
| 3779 | spin_lock_irq(&workqueue_lock); |
| 3780 | |
Tejun Heo | 983ca25 | 2013-03-13 16:51:35 -0700 | [diff] [blame^] | 3781 | /* |
| 3782 | * Set the matching work_color. This is synchronized with |
| 3783 | * flush_mutex to avoid confusing flush_workqueue(). |
| 3784 | */ |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3785 | if (p_last_pwq) |
| 3786 | *p_last_pwq = first_pwq(wq); |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3787 | pwq->work_color = wq->work_color; |
Tejun Heo | 983ca25 | 2013-03-13 16:51:35 -0700 | [diff] [blame^] | 3788 | |
| 3789 | /* sync max_active to the current setting */ |
| 3790 | pwq_adjust_max_active(pwq); |
| 3791 | |
| 3792 | /* link in @pwq */ |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3793 | list_add_rcu(&pwq->pwqs_node, &wq->pwqs); |
Tejun Heo | 75ccf59 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3794 | |
| 3795 | spin_unlock_irq(&workqueue_lock); |
| 3796 | mutex_unlock(&wq->flush_mutex); |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3799 | /** |
| 3800 | * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue |
| 3801 | * @wq: the target workqueue |
| 3802 | * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() |
| 3803 | * |
| 3804 | * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the |
| 3805 | * current attributes, a new pwq is created and made the first pwq which |
| 3806 | * will serve all new work items. Older pwqs are released as in-flight |
| 3807 | * work items finish. Note that a work item which repeatedly requeues |
| 3808 | * itself back-to-back will stay on its current pwq. |
| 3809 | * |
| 3810 | * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on |
| 3811 | * failure. |
| 3812 | */ |
| 3813 | int apply_workqueue_attrs(struct workqueue_struct *wq, |
| 3814 | const struct workqueue_attrs *attrs) |
| 3815 | { |
| 3816 | struct pool_workqueue *pwq, *last_pwq; |
| 3817 | struct worker_pool *pool; |
| 3818 | |
Tejun Heo | 8719dce | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3819 | /* only unbound workqueues can change attributes */ |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3820 | if (WARN_ON(!(wq->flags & WQ_UNBOUND))) |
| 3821 | return -EINVAL; |
| 3822 | |
Tejun Heo | 8719dce | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3823 | /* creating multiple pwqs breaks ordering guarantee */ |
| 3824 | if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))) |
| 3825 | return -EINVAL; |
| 3826 | |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3827 | pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL); |
| 3828 | if (!pwq) |
| 3829 | return -ENOMEM; |
| 3830 | |
| 3831 | pool = get_unbound_pool(attrs); |
| 3832 | if (!pool) { |
| 3833 | kmem_cache_free(pwq_cache, pwq); |
| 3834 | return -ENOMEM; |
| 3835 | } |
| 3836 | |
| 3837 | init_and_link_pwq(pwq, wq, pool, &last_pwq); |
| 3838 | if (last_pwq) { |
| 3839 | spin_lock_irq(&last_pwq->pool->lock); |
| 3840 | put_pwq(last_pwq); |
| 3841 | spin_unlock_irq(&last_pwq->pool->lock); |
| 3842 | } |
| 3843 | |
| 3844 | return 0; |
| 3845 | } |
| 3846 | |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3847 | static int alloc_and_link_pwqs(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | { |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 3849 | bool highpri = wq->flags & WQ_HIGHPRI; |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3850 | int cpu; |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 3851 | |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3852 | if (!(wq->flags & WQ_UNBOUND)) { |
Tejun Heo | 420c0dd | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 3853 | wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); |
| 3854 | if (!wq->cpu_pwqs) |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3855 | return -ENOMEM; |
| 3856 | |
| 3857 | for_each_possible_cpu(cpu) { |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3858 | struct pool_workqueue *pwq = |
| 3859 | per_cpu_ptr(wq->cpu_pwqs, cpu); |
Tejun Heo | 7a62c2c | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3860 | struct worker_pool *cpu_pools = |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3861 | per_cpu(cpu_worker_pools, cpu); |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3862 | |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3863 | init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL); |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3864 | } |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3865 | return 0; |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3866 | } else { |
Tejun Heo | 9e8cd2f | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3867 | return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3868 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 3871 | static int wq_clamp_max_active(int max_active, unsigned int flags, |
| 3872 | const char *name) |
Tejun Heo | b71ab8c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3873 | { |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 3874 | int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; |
| 3875 | |
| 3876 | if (max_active < 1 || max_active > lim) |
Valentin Ilie | 044c782 | 2012-08-19 00:52:42 +0300 | [diff] [blame] | 3877 | pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", |
| 3878 | max_active, name, 1, lim); |
Tejun Heo | b71ab8c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3879 | |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 3880 | return clamp_val(max_active, 1, lim); |
Tejun Heo | b71ab8c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3883 | struct workqueue_struct *__alloc_workqueue_key(const char *fmt, |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3884 | unsigned int flags, |
| 3885 | int max_active, |
| 3886 | struct lock_class_key *key, |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3887 | const char *lock_name, ...) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3888 | { |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3889 | va_list args, args1; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3890 | struct workqueue_struct *wq; |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 3891 | struct pool_workqueue *pwq; |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3892 | size_t namelen; |
| 3893 | |
| 3894 | /* determine namelen, allocate wq and format name */ |
| 3895 | va_start(args, lock_name); |
| 3896 | va_copy(args1, args); |
| 3897 | namelen = vsnprintf(NULL, 0, fmt, args) + 1; |
| 3898 | |
| 3899 | wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); |
| 3900 | if (!wq) |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3901 | return NULL; |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3902 | |
| 3903 | vsnprintf(wq->name, namelen, fmt, args1); |
| 3904 | va_end(args); |
| 3905 | va_end(args1); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3906 | |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3907 | max_active = max_active ?: WQ_DFL_ACTIVE; |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3908 | max_active = wq_clamp_max_active(max_active, flags, wq->name); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3909 | |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3910 | /* init wq */ |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 3911 | wq->flags = flags; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3912 | wq->saved_max_active = max_active; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 3913 | mutex_init(&wq->flush_mutex); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 3914 | atomic_set(&wq->nr_pwqs_to_flush, 0); |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3915 | INIT_LIST_HEAD(&wq->pwqs); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 3916 | INIT_LIST_HEAD(&wq->flusher_queue); |
| 3917 | INIT_LIST_HEAD(&wq->flusher_overflow); |
Tejun Heo | 493a172 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 3918 | INIT_LIST_HEAD(&wq->maydays); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3919 | |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3920 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 3921 | INIT_LIST_HEAD(&wq->list); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3922 | |
Tejun Heo | 30cdf249 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3923 | if (alloc_and_link_pwqs(wq) < 0) |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3924 | goto err_free_wq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3925 | |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 3926 | /* |
| 3927 | * Workqueues which may be used during memory reclaim should |
| 3928 | * have a rescuer to guarantee forward progress. |
| 3929 | */ |
| 3930 | if (flags & WQ_MEM_RECLAIM) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3931 | struct worker *rescuer; |
| 3932 | |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3933 | rescuer = alloc_worker(); |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3934 | if (!rescuer) |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3935 | goto err_destroy; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3936 | |
Tejun Heo | 111c225 | 2013-01-17 17:16:24 -0800 | [diff] [blame] | 3937 | rescuer->rescue_wq = wq; |
| 3938 | rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", |
Tejun Heo | b196be8 | 2012-01-10 15:11:35 -0800 | [diff] [blame] | 3939 | wq->name); |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3940 | if (IS_ERR(rescuer->task)) { |
| 3941 | kfree(rescuer); |
| 3942 | goto err_destroy; |
| 3943 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3944 | |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3945 | wq->rescuer = rescuer; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3946 | rescuer->task->flags |= PF_THREAD_BOUND; |
| 3947 | wake_up_process(rescuer->task); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3948 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3949 | |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 3950 | if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) |
| 3951 | goto err_destroy; |
| 3952 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3953 | /* |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3954 | * workqueue_lock protects global freeze state and workqueues list. |
| 3955 | * Grab it, adjust max_active and add the new workqueue to |
| 3956 | * workqueues list. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3957 | */ |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3958 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3959 | |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 3960 | for_each_pwq(pwq, wq) |
| 3961 | pwq_adjust_max_active(pwq); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3962 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 3963 | list_add(&wq->list, &workqueues); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 3964 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3965 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 3966 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3967 | return wq; |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 3968 | |
| 3969 | err_free_wq: |
| 3970 | kfree(wq); |
| 3971 | return NULL; |
| 3972 | err_destroy: |
| 3973 | destroy_workqueue(wq); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 3974 | return NULL; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3975 | } |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 3976 | EXPORT_SYMBOL_GPL(__alloc_workqueue_key); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3977 | |
| 3978 | /** |
| 3979 | * destroy_workqueue - safely terminate a workqueue |
| 3980 | * @wq: target workqueue |
| 3981 | * |
| 3982 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 3983 | */ |
| 3984 | void destroy_workqueue(struct workqueue_struct *wq) |
| 3985 | { |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 3986 | struct pool_workqueue *pwq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 3987 | |
Tejun Heo | 9c5a2ba | 2011-04-05 18:01:44 +0200 | [diff] [blame] | 3988 | /* drain it before proceeding with destruction */ |
| 3989 | drain_workqueue(wq); |
Tejun Heo | c8efcc2 | 2010-12-20 19:32:04 +0100 | [diff] [blame] | 3990 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3991 | spin_lock_irq(&workqueue_lock); |
| 3992 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3993 | /* sanity checks */ |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 3994 | for_each_pwq(pwq, wq) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 3995 | int i; |
| 3996 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 3997 | for (i = 0; i < WORK_NR_COLORS; i++) { |
| 3998 | if (WARN_ON(pwq->nr_in_flight[i])) { |
| 3999 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4000 | return; |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 4004 | if (WARN_ON(pwq->refcnt > 1) || |
| 4005 | WARN_ON(pwq->nr_active) || |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4006 | WARN_ON(!list_empty(&pwq->delayed_works))) { |
| 4007 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4008 | return; |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4009 | } |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4012 | /* |
| 4013 | * wq list is used to freeze wq, remove from list after |
| 4014 | * flushing is complete in case freeze races us. |
| 4015 | */ |
Tejun Heo | d2c1d40 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 4016 | list_del_init(&wq->list); |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4017 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4018 | spin_unlock_irq(&workqueue_lock); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 4019 | |
Tejun Heo | 226223a | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 4020 | workqueue_sysfs_unregister(wq); |
| 4021 | |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4022 | if (wq->rescuer) { |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4023 | kthread_stop(wq->rescuer->task); |
Xiaotian Feng | 8d9df9f | 2010-08-16 09:54:28 +0200 | [diff] [blame] | 4024 | kfree(wq->rescuer); |
Tejun Heo | 493008a | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4025 | wq->rescuer = NULL; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4026 | } |
| 4027 | |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 4028 | if (!(wq->flags & WQ_UNBOUND)) { |
| 4029 | /* |
| 4030 | * The base ref is never dropped on per-cpu pwqs. Directly |
| 4031 | * free the pwqs and wq. |
| 4032 | */ |
| 4033 | free_percpu(wq->cpu_pwqs); |
| 4034 | kfree(wq); |
| 4035 | } else { |
| 4036 | /* |
| 4037 | * We're the sole accessor of @wq at this point. Directly |
| 4038 | * access the first pwq and put the base ref. As both pwqs |
| 4039 | * and pools are sched-RCU protected, the lock operations |
| 4040 | * are safe. @wq will be freed when the last pwq is |
| 4041 | * released. |
| 4042 | */ |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4043 | pwq = list_first_entry(&wq->pwqs, struct pool_workqueue, |
| 4044 | pwqs_node); |
Tejun Heo | 8864b4e | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 4045 | spin_lock_irq(&pwq->pool->lock); |
| 4046 | put_pwq(pwq); |
| 4047 | spin_unlock_irq(&pwq->pool->lock); |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4048 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 4049 | } |
| 4050 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 4051 | |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4052 | /** |
| 4053 | * workqueue_set_max_active - adjust max_active of a workqueue |
| 4054 | * @wq: target workqueue |
| 4055 | * @max_active: new max_active value. |
| 4056 | * |
| 4057 | * Set max_active of @wq to @max_active. |
| 4058 | * |
| 4059 | * CONTEXT: |
| 4060 | * Don't call from IRQ context. |
| 4061 | */ |
| 4062 | void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) |
| 4063 | { |
Tejun Heo | 49e3cf4 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4064 | struct pool_workqueue *pwq; |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4065 | |
Tejun Heo | 8719dce | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 4066 | /* disallow meddling with max_active for ordered workqueues */ |
| 4067 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
| 4068 | return; |
| 4069 | |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 4070 | max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4071 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4072 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4073 | |
| 4074 | wq->saved_max_active = max_active; |
| 4075 | |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 4076 | for_each_pwq(pwq, wq) |
| 4077 | pwq_adjust_max_active(pwq); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4078 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4079 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4080 | } |
| 4081 | EXPORT_SYMBOL_GPL(workqueue_set_max_active); |
| 4082 | |
| 4083 | /** |
Tejun Heo | e6267616 | 2013-03-12 17:41:37 -0700 | [diff] [blame] | 4084 | * current_is_workqueue_rescuer - is %current workqueue rescuer? |
| 4085 | * |
| 4086 | * Determine whether %current is a workqueue rescuer. Can be used from |
| 4087 | * work functions to determine whether it's being run off the rescuer task. |
| 4088 | */ |
| 4089 | bool current_is_workqueue_rescuer(void) |
| 4090 | { |
| 4091 | struct worker *worker = current_wq_worker(); |
| 4092 | |
| 4093 | return worker && worker == worker->current_pwq->wq->rescuer; |
| 4094 | } |
| 4095 | |
| 4096 | /** |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4097 | * workqueue_congested - test whether a workqueue is congested |
| 4098 | * @cpu: CPU in question |
| 4099 | * @wq: target workqueue |
| 4100 | * |
| 4101 | * Test whether @wq's cpu workqueue for @cpu is congested. There is |
| 4102 | * no synchronization around this function and the test result is |
| 4103 | * unreliable and only useful as advisory hints or for debugging. |
| 4104 | * |
| 4105 | * RETURNS: |
| 4106 | * %true if congested, %false otherwise. |
| 4107 | */ |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 4108 | bool workqueue_congested(int cpu, struct workqueue_struct *wq) |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4109 | { |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4110 | struct pool_workqueue *pwq; |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4111 | bool ret; |
| 4112 | |
| 4113 | preempt_disable(); |
Tejun Heo | 7fb98ea | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4114 | |
| 4115 | if (!(wq->flags & WQ_UNBOUND)) |
| 4116 | pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); |
| 4117 | else |
| 4118 | pwq = first_pwq(wq); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4119 | |
Tejun Heo | 76af4d9 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4120 | ret = !list_empty(&pwq->delayed_works); |
| 4121 | preempt_enable(); |
| 4122 | |
| 4123 | return ret; |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4124 | } |
| 4125 | EXPORT_SYMBOL_GPL(workqueue_congested); |
| 4126 | |
| 4127 | /** |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4128 | * work_busy - test whether a work is currently pending or running |
| 4129 | * @work: the work to be tested |
| 4130 | * |
| 4131 | * Test whether @work is currently pending or running. There is no |
| 4132 | * synchronization around this function and the test result is |
| 4133 | * unreliable and only useful as advisory hints or for debugging. |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4134 | * |
| 4135 | * RETURNS: |
| 4136 | * OR'd bitmask of WORK_BUSY_* bits. |
| 4137 | */ |
| 4138 | unsigned int work_busy(struct work_struct *work) |
| 4139 | { |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4140 | struct worker_pool *pool; |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4141 | unsigned long flags; |
| 4142 | unsigned int ret = 0; |
| 4143 | |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4144 | if (work_pending(work)) |
| 4145 | ret |= WORK_BUSY_PENDING; |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4146 | |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4147 | local_irq_save(flags); |
| 4148 | pool = get_work_pool(work); |
Lai Jiangshan | 038366c | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 4149 | if (pool) { |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4150 | spin_lock(&pool->lock); |
Lai Jiangshan | 038366c | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 4151 | if (find_worker_executing_work(pool, work)) |
| 4152 | ret |= WORK_BUSY_RUNNING; |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4153 | spin_unlock(&pool->lock); |
Lai Jiangshan | 038366c | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 4154 | } |
Tejun Heo | fa1b54e | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4155 | local_irq_restore(flags); |
Tejun Heo | dcd989c | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4156 | |
| 4157 | return ret; |
| 4158 | } |
| 4159 | EXPORT_SYMBOL_GPL(work_busy); |
| 4160 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4161 | /* |
| 4162 | * CPU hotplug. |
| 4163 | * |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4164 | * There are two challenges in supporting CPU hotplug. Firstly, there |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 4165 | * are a lot of assumptions on strong associations among work, pwq and |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4166 | * pool which make migrating pending and scheduled works very |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4167 | * difficult to implement without impacting hot paths. Secondly, |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4168 | * worker pools serve mix of short, long and very long running works making |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4169 | * blocked draining impractical. |
| 4170 | * |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4171 | * This is solved by allowing the pools to be disassociated from the CPU |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4172 | * running as an unbound one and allowing it to be reattached later if the |
| 4173 | * cpu comes back online. |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4174 | */ |
| 4175 | |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4176 | static void wq_unbind_fn(struct work_struct *work) |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4177 | { |
Tejun Heo | 38db41d | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4178 | int cpu = smp_processor_id(); |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4179 | struct worker_pool *pool; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4180 | struct worker *worker; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4181 | int i; |
| 4182 | |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4183 | for_each_cpu_worker_pool(pool, cpu) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4184 | WARN_ON_ONCE(cpu != smp_processor_id()); |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4185 | |
| 4186 | mutex_lock(&pool->assoc_mutex); |
| 4187 | spin_lock_irq(&pool->lock); |
| 4188 | |
| 4189 | /* |
| 4190 | * We've claimed all manager positions. Make all workers |
| 4191 | * unbound and set DISASSOCIATED. Before this, all workers |
| 4192 | * except for the ones which are still executing works from |
| 4193 | * before the last CPU down must be on the cpu. After |
| 4194 | * this, they may become diasporas. |
| 4195 | */ |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4196 | list_for_each_entry(worker, &pool->idle_list, entry) |
Tejun Heo | 403c821 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4197 | worker->flags |= WORKER_UNBOUND; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4198 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4199 | for_each_busy_worker(worker, i, pool) |
Tejun Heo | c9e7cf2 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4200 | worker->flags |= WORKER_UNBOUND; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4201 | |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4202 | pool->flags |= POOL_DISASSOCIATED; |
Tejun Heo | f2d5a0e | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4203 | |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4204 | spin_unlock_irq(&pool->lock); |
| 4205 | mutex_unlock(&pool->assoc_mutex); |
| 4206 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4207 | |
| 4208 | /* |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4209 | * Call schedule() so that we cross rq->lock and thus can guarantee |
| 4210 | * sched callbacks see the %WORKER_UNBOUND flag. This is necessary |
| 4211 | * as scheduler callbacks may be invoked from other cpus. |
| 4212 | */ |
| 4213 | schedule(); |
| 4214 | |
| 4215 | /* |
| 4216 | * Sched callbacks are disabled now. Zap nr_running. After this, |
| 4217 | * nr_running stays zero and need_more_worker() and keep_working() |
Tejun Heo | 38db41d | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4218 | * are always true as long as the worklist is not empty. Pools on |
| 4219 | * @cpu now behave as unbound (in terms of concurrency management) |
| 4220 | * pools which are served by workers tied to the CPU. |
Tejun Heo | 628c78e | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4221 | * |
| 4222 | * On return from this function, the current worker would trigger |
| 4223 | * unbound chain execution of pending work items if other workers |
| 4224 | * didn't already. |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4225 | */ |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4226 | for_each_cpu_worker_pool(pool, cpu) |
Tejun Heo | e19e397 | 2013-01-24 11:39:44 -0800 | [diff] [blame] | 4227 | atomic_set(&pool->nr_running, 0); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4228 | } |
| 4229 | |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4230 | /* |
| 4231 | * Workqueues should be brought up before normal priority CPU notifiers. |
| 4232 | * This will be registered high priority CPU notifier. |
| 4233 | */ |
Lai Jiangshan | 9fdf9b7 | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 4234 | static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4235 | unsigned long action, |
| 4236 | void *hcpu) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 4237 | { |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 4238 | int cpu = (unsigned long)hcpu; |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4239 | struct worker_pool *pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4241 | switch (action & ~CPU_TASKS_FROZEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | case CPU_UP_PREPARE: |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4243 | for_each_cpu_worker_pool(pool, cpu) { |
Tejun Heo | 3ce6337 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4244 | struct worker *worker; |
| 4245 | |
| 4246 | if (pool->nr_workers) |
| 4247 | continue; |
| 4248 | |
| 4249 | worker = create_worker(pool); |
| 4250 | if (!worker) |
| 4251 | return NOTIFY_BAD; |
| 4252 | |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4253 | spin_lock_irq(&pool->lock); |
Tejun Heo | 3ce6337 | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4254 | start_worker(worker); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4255 | spin_unlock_irq(&pool->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | } |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4257 | break; |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 4258 | |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4259 | case CPU_DOWN_FAILED: |
| 4260 | case CPU_ONLINE: |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4261 | for_each_cpu_worker_pool(pool, cpu) { |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4262 | mutex_lock(&pool->assoc_mutex); |
| 4263 | spin_lock_irq(&pool->lock); |
| 4264 | |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4265 | pool->flags &= ~POOL_DISASSOCIATED; |
Tejun Heo | 94cf58b | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4266 | rebind_workers(pool); |
| 4267 | |
| 4268 | spin_unlock_irq(&pool->lock); |
| 4269 | mutex_unlock(&pool->assoc_mutex); |
| 4270 | } |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4271 | break; |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4272 | } |
| 4273 | return NOTIFY_OK; |
| 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Workqueues should be brought down after normal priority CPU notifiers. |
| 4278 | * This will be registered as low priority CPU notifier. |
| 4279 | */ |
Lai Jiangshan | 9fdf9b7 | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 4280 | static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4281 | unsigned long action, |
| 4282 | void *hcpu) |
| 4283 | { |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 4284 | int cpu = (unsigned long)hcpu; |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4285 | struct work_struct unbind_work; |
| 4286 | |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4287 | switch (action & ~CPU_TASKS_FROZEN) { |
| 4288 | case CPU_DOWN_PREPARE: |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4289 | /* unbinding should happen on the local CPU */ |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4290 | INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); |
Joonsoo Kim | 7635d2f | 2012-08-15 23:25:41 +0900 | [diff] [blame] | 4291 | queue_work_on(cpu, system_highpri_wq, &unbind_work); |
Tejun Heo | 8db25e7 | 2012-07-17 12:39:28 -0700 | [diff] [blame] | 4292 | flush_work(&unbind_work); |
| 4293 | break; |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4294 | } |
| 4295 | return NOTIFY_OK; |
| 4296 | } |
| 4297 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4298 | #ifdef CONFIG_SMP |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 4299 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4300 | struct work_for_cpu { |
Tejun Heo | ed48ece | 2012-09-18 12:48:43 -0700 | [diff] [blame] | 4301 | struct work_struct work; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4302 | long (*fn)(void *); |
| 4303 | void *arg; |
| 4304 | long ret; |
| 4305 | }; |
| 4306 | |
Tejun Heo | ed48ece | 2012-09-18 12:48:43 -0700 | [diff] [blame] | 4307 | static void work_for_cpu_fn(struct work_struct *work) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4308 | { |
Tejun Heo | ed48ece | 2012-09-18 12:48:43 -0700 | [diff] [blame] | 4309 | struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); |
| 4310 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4311 | wfc->ret = wfc->fn(wfc->arg); |
| 4312 | } |
| 4313 | |
| 4314 | /** |
| 4315 | * work_on_cpu - run a function in user context on a particular cpu |
| 4316 | * @cpu: the cpu to run on |
| 4317 | * @fn: the function to run |
| 4318 | * @arg: the function arg |
| 4319 | * |
Rusty Russell | 31ad908 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 4320 | * This will return the value @fn returns. |
| 4321 | * It is up to the caller to ensure that the cpu doesn't go offline. |
Andrew Morton | 6b44003e | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 4322 | * The caller must not hold any locks which would prevent @fn from completing. |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4323 | */ |
Tejun Heo | d84ff05 | 2013-03-12 11:29:59 -0700 | [diff] [blame] | 4324 | long work_on_cpu(int cpu, long (*fn)(void *), void *arg) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4325 | { |
Tejun Heo | ed48ece | 2012-09-18 12:48:43 -0700 | [diff] [blame] | 4326 | struct work_for_cpu wfc = { .fn = fn, .arg = arg }; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4327 | |
Tejun Heo | ed48ece | 2012-09-18 12:48:43 -0700 | [diff] [blame] | 4328 | INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); |
| 4329 | schedule_work_on(cpu, &wfc.work); |
| 4330 | flush_work(&wfc.work); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 4331 | return wfc.ret; |
| 4332 | } |
| 4333 | EXPORT_SYMBOL_GPL(work_on_cpu); |
| 4334 | #endif /* CONFIG_SMP */ |
| 4335 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4336 | #ifdef CONFIG_FREEZER |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 4337 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4338 | /** |
| 4339 | * freeze_workqueues_begin - begin freezing workqueues |
| 4340 | * |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 4341 | * Start freezing workqueues. After this function returns, all freezable |
| 4342 | * workqueues will queue new works to their frozen_works list instead of |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4343 | * pool->worklist. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4344 | * |
| 4345 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4346 | * Grabs and releases workqueue_lock and pool->lock's. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4347 | */ |
| 4348 | void freeze_workqueues_begin(void) |
| 4349 | { |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4350 | struct worker_pool *pool; |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4351 | struct workqueue_struct *wq; |
| 4352 | struct pool_workqueue *pwq; |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4353 | int id; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4354 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4355 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4356 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4357 | WARN_ON_ONCE(workqueue_freezing); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4358 | workqueue_freezing = true; |
| 4359 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4360 | /* set FREEZING */ |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4361 | for_each_pool(pool, id) { |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4362 | spin_lock(&pool->lock); |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4363 | WARN_ON_ONCE(pool->flags & POOL_FREEZING); |
| 4364 | pool->flags |= POOL_FREEZING; |
Tejun Heo | 1711696 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4365 | spin_unlock(&pool->lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4366 | } |
| 4367 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4368 | /* suppress further executions by setting max_active to zero */ |
| 4369 | list_for_each_entry(wq, &workqueues, list) { |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 4370 | for_each_pwq(pwq, wq) |
| 4371 | pwq_adjust_max_active(pwq); |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4374 | spin_unlock_irq(&workqueue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | } |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4376 | |
| 4377 | /** |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 4378 | * freeze_workqueues_busy - are freezable workqueues still busy? |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4379 | * |
| 4380 | * Check whether freezing is complete. This function must be called |
| 4381 | * between freeze_workqueues_begin() and thaw_workqueues(). |
| 4382 | * |
| 4383 | * CONTEXT: |
| 4384 | * Grabs and releases workqueue_lock. |
| 4385 | * |
| 4386 | * RETURNS: |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 4387 | * %true if some freezable workqueues are still busy. %false if freezing |
| 4388 | * is complete. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4389 | */ |
| 4390 | bool freeze_workqueues_busy(void) |
| 4391 | { |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4392 | bool busy = false; |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4393 | struct workqueue_struct *wq; |
| 4394 | struct pool_workqueue *pwq; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4395 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4396 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4397 | |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4398 | WARN_ON_ONCE(!workqueue_freezing); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4399 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4400 | list_for_each_entry(wq, &workqueues, list) { |
| 4401 | if (!(wq->flags & WQ_FREEZABLE)) |
| 4402 | continue; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4403 | /* |
| 4404 | * nr_active is monotonically decreasing. It's safe |
| 4405 | * to peek without lock. |
| 4406 | */ |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4407 | for_each_pwq(pwq, wq) { |
Tejun Heo | 6183c00 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4408 | WARN_ON_ONCE(pwq->nr_active < 0); |
Tejun Heo | 112202d | 2013-02-13 19:29:12 -0800 | [diff] [blame] | 4409 | if (pwq->nr_active) { |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4410 | busy = true; |
| 4411 | goto out_unlock; |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | out_unlock: |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4416 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4417 | return busy; |
| 4418 | } |
| 4419 | |
| 4420 | /** |
| 4421 | * thaw_workqueues - thaw workqueues |
| 4422 | * |
| 4423 | * Thaw workqueues. Normal queueing is restored and all collected |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4424 | * frozen works are transferred to their respective pool worklists. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4425 | * |
| 4426 | * CONTEXT: |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4427 | * Grabs and releases workqueue_lock and pool->lock's. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4428 | */ |
| 4429 | void thaw_workqueues(void) |
| 4430 | { |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4431 | struct workqueue_struct *wq; |
| 4432 | struct pool_workqueue *pwq; |
| 4433 | struct worker_pool *pool; |
| 4434 | int id; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4435 | |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4436 | spin_lock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4437 | |
| 4438 | if (!workqueue_freezing) |
| 4439 | goto out_unlock; |
| 4440 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4441 | /* clear FREEZING */ |
| 4442 | for_each_pool(pool, id) { |
| 4443 | spin_lock(&pool->lock); |
| 4444 | WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); |
| 4445 | pool->flags &= ~POOL_FREEZING; |
| 4446 | spin_unlock(&pool->lock); |
| 4447 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4448 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4449 | /* restore max_active and repopulate worklist */ |
| 4450 | list_for_each_entry(wq, &workqueues, list) { |
Tejun Heo | 699ce09 | 2013-03-13 16:51:35 -0700 | [diff] [blame] | 4451 | for_each_pwq(pwq, wq) |
| 4452 | pwq_adjust_max_active(pwq); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4453 | } |
| 4454 | |
Tejun Heo | 24b8a84 | 2013-03-12 11:29:58 -0700 | [diff] [blame] | 4455 | /* kick workers */ |
| 4456 | for_each_pool(pool, id) { |
| 4457 | spin_lock(&pool->lock); |
| 4458 | wake_up_worker(pool); |
| 4459 | spin_unlock(&pool->lock); |
| 4460 | } |
| 4461 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4462 | workqueue_freezing = false; |
| 4463 | out_unlock: |
Tejun Heo | e98d5b1 | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4464 | spin_unlock_irq(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4465 | } |
| 4466 | #endif /* CONFIG_FREEZER */ |
| 4467 | |
Suresh Siddha | 6ee0578 | 2010-07-30 14:57:37 -0700 | [diff] [blame] | 4468 | static int __init init_workqueues(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | { |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4470 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
| 4471 | int i, cpu; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 4472 | |
Tejun Heo | 7c3eed5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4473 | /* make sure we have enough bits for OFFQ pool ID */ |
| 4474 | BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < |
Lai Jiangshan | 6be1958 | 2013-02-06 18:04:53 -0800 | [diff] [blame] | 4475 | WORK_CPU_END * NR_STD_WORKER_POOLS); |
Tejun Heo | b549007 | 2012-08-03 10:30:46 -0700 | [diff] [blame] | 4476 | |
Tejun Heo | e904e6c | 2013-03-12 11:29:57 -0700 | [diff] [blame] | 4477 | WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); |
| 4478 | |
| 4479 | pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); |
| 4480 | |
Tejun Heo | 6575820 | 2012-07-17 12:39:26 -0700 | [diff] [blame] | 4481 | cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); |
Lai Jiangshan | a5b4e57 | 2012-09-18 09:59:23 -0700 | [diff] [blame] | 4482 | hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4483 | |
Tejun Heo | 706026c | 2013-01-24 11:01:34 -0800 | [diff] [blame] | 4484 | /* initialize CPU pools */ |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4485 | for_each_possible_cpu(cpu) { |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4486 | struct worker_pool *pool; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4487 | |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4488 | i = 0; |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4489 | for_each_cpu_worker_pool(pool, cpu) { |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4490 | BUG_ON(init_worker_pool(pool)); |
Tejun Heo | ec22ca5 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4491 | pool->cpu = cpu; |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4492 | cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); |
Tejun Heo | 7a4e344 | 2013-03-12 11:30:00 -0700 | [diff] [blame] | 4493 | pool->attrs->nice = std_nice[i++]; |
| 4494 | |
Tejun Heo | 9daf9e6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4495 | /* alloc pool ID */ |
| 4496 | BUG_ON(worker_pool_assign_id(pool)); |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4497 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 4498 | } |
| 4499 | |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4500 | /* create the initial worker */ |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4501 | for_each_online_cpu(cpu) { |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4502 | struct worker_pool *pool; |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4503 | |
Tejun Heo | f02ae73 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4504 | for_each_cpu_worker_pool(pool, cpu) { |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4505 | struct worker *worker; |
| 4506 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4507 | pool->flags &= ~POOL_DISASSOCIATED; |
Tejun Heo | 2464757 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4508 | |
Tejun Heo | bc2ae0f | 2012-07-17 12:39:27 -0700 | [diff] [blame] | 4509 | worker = create_worker(pool); |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4510 | BUG_ON(!worker); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4511 | spin_lock_irq(&pool->lock); |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4512 | start_worker(worker); |
Tejun Heo | d565ed6 | 2013-01-24 11:01:33 -0800 | [diff] [blame] | 4513 | spin_unlock_irq(&pool->lock); |
Tejun Heo | 4ce62e9 | 2012-07-13 22:16:44 -0700 | [diff] [blame] | 4514 | } |
Tejun Heo | e22bee7 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4515 | } |
| 4516 | |
Tejun Heo | 29c91e9 | 2013-03-12 11:30:03 -0700 | [diff] [blame] | 4517 | /* create default unbound wq attrs */ |
| 4518 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { |
| 4519 | struct workqueue_attrs *attrs; |
| 4520 | |
| 4521 | BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); |
| 4522 | |
| 4523 | attrs->nice = std_nice[i]; |
| 4524 | cpumask_setall(attrs->cpumask); |
| 4525 | |
| 4526 | unbound_std_wq_attrs[i] = attrs; |
| 4527 | } |
| 4528 | |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4529 | system_wq = alloc_workqueue("events", 0, 0); |
Joonsoo Kim | 1aabe90 | 2012-08-15 23:25:39 +0900 | [diff] [blame] | 4530 | system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); |
Tejun Heo | d320c03 | 2010-06-29 10:07:14 +0200 | [diff] [blame] | 4531 | system_long_wq = alloc_workqueue("events_long", 0, 0); |
Tejun Heo | f342179 | 2010-07-02 10:03:51 +0200 | [diff] [blame] | 4532 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
| 4533 | WQ_UNBOUND_MAX_ACTIVE); |
Tejun Heo | 24d51ad | 2011-02-21 09:52:50 +0100 | [diff] [blame] | 4534 | system_freezable_wq = alloc_workqueue("events_freezable", |
| 4535 | WQ_FREEZABLE, 0); |
Joonsoo Kim | 1aabe90 | 2012-08-15 23:25:39 +0900 | [diff] [blame] | 4536 | BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || |
Tejun Heo | ae930e0 | 2012-08-20 14:51:23 -0700 | [diff] [blame] | 4537 | !system_unbound_wq || !system_freezable_wq); |
Suresh Siddha | 6ee0578 | 2010-07-30 14:57:37 -0700 | [diff] [blame] | 4538 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | } |
Suresh Siddha | 6ee0578 | 2010-07-30 14:57:37 -0700 | [diff] [blame] | 4540 | early_initcall(init_workqueues); |