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