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