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