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