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