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