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