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