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