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