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