diff options
Diffstat (limited to 'kernel/async.c')
-rw-r--r-- | kernel/async.c | 167 |
1 files changed, 72 insertions, 95 deletions
diff --git a/kernel/async.c b/kernel/async.c index 6f34904a0b53..8ddee2c3e5b0 100644 --- a/kernel/async.c +++ b/kernel/async.c | |||
@@ -57,65 +57,52 @@ asynchronous and synchronous parts of the kernel. | |||
57 | #include <linux/slab.h> | 57 | #include <linux/slab.h> |
58 | #include <linux/workqueue.h> | 58 | #include <linux/workqueue.h> |
59 | 59 | ||
60 | #include "workqueue_internal.h" | ||
61 | |||
60 | static async_cookie_t next_cookie = 1; | 62 | static async_cookie_t next_cookie = 1; |
61 | 63 | ||
62 | #define MAX_WORK 32768 | 64 | #define MAX_WORK 32768 |
65 | #define ASYNC_COOKIE_MAX ULLONG_MAX /* infinity cookie */ | ||
63 | 66 | ||
64 | static LIST_HEAD(async_pending); | 67 | static LIST_HEAD(async_global_pending); /* pending from all registered doms */ |
65 | static ASYNC_DOMAIN(async_running); | 68 | static ASYNC_DOMAIN(async_dfl_domain); |
66 | static LIST_HEAD(async_domains); | ||
67 | static DEFINE_SPINLOCK(async_lock); | 69 | static DEFINE_SPINLOCK(async_lock); |
68 | static DEFINE_MUTEX(async_register_mutex); | ||
69 | 70 | ||
70 | struct async_entry { | 71 | struct async_entry { |
71 | struct list_head list; | 72 | struct list_head domain_list; |
73 | struct list_head global_list; | ||
72 | struct work_struct work; | 74 | struct work_struct work; |
73 | async_cookie_t cookie; | 75 | async_cookie_t cookie; |
74 | async_func_ptr *func; | 76 | async_func_ptr *func; |
75 | void *data; | 77 | void *data; |
76 | struct async_domain *running; | 78 | struct async_domain *domain; |
77 | }; | 79 | }; |
78 | 80 | ||
79 | static DECLARE_WAIT_QUEUE_HEAD(async_done); | 81 | static DECLARE_WAIT_QUEUE_HEAD(async_done); |
80 | 82 | ||
81 | static atomic_t entry_count; | 83 | static atomic_t entry_count; |
82 | 84 | ||
83 | 85 | static async_cookie_t lowest_in_progress(struct async_domain *domain) | |
84 | /* | ||
85 | * MUST be called with the lock held! | ||
86 | */ | ||
87 | static async_cookie_t __lowest_in_progress(struct async_domain *running) | ||
88 | { | 86 | { |
89 | async_cookie_t first_running = next_cookie; /* infinity value */ | 87 | struct async_entry *first = NULL; |
90 | async_cookie_t first_pending = next_cookie; /* ditto */ | 88 | async_cookie_t ret = ASYNC_COOKIE_MAX; |
91 | struct async_entry *entry; | 89 | unsigned long flags; |
92 | 90 | ||
93 | /* | 91 | spin_lock_irqsave(&async_lock, flags); |
94 | * Both running and pending lists are sorted but not disjoint. | ||
95 | * Take the first cookies from both and return the min. | ||
96 | */ | ||
97 | if (!list_empty(&running->domain)) { | ||
98 | entry = list_first_entry(&running->domain, typeof(*entry), list); | ||
99 | first_running = entry->cookie; | ||
100 | } | ||
101 | 92 | ||
102 | list_for_each_entry(entry, &async_pending, list) { | 93 | if (domain) { |
103 | if (entry->running == running) { | 94 | if (!list_empty(&domain->pending)) |
104 | first_pending = entry->cookie; | 95 | first = list_first_entry(&domain->pending, |
105 | break; | 96 | struct async_entry, domain_list); |
106 | } | 97 | } else { |
98 | if (!list_empty(&async_global_pending)) | ||
99 | first = list_first_entry(&async_global_pending, | ||
100 | struct async_entry, global_list); | ||
107 | } | 101 | } |
108 | 102 | ||
109 | return min(first_running, first_pending); | 103 | if (first) |
110 | } | 104 | ret = first->cookie; |
111 | |||
112 | static async_cookie_t lowest_in_progress(struct async_domain *running) | ||
113 | { | ||
114 | unsigned long flags; | ||
115 | async_cookie_t ret; | ||
116 | 105 | ||
117 | spin_lock_irqsave(&async_lock, flags); | ||
118 | ret = __lowest_in_progress(running); | ||
119 | spin_unlock_irqrestore(&async_lock, flags); | 106 | spin_unlock_irqrestore(&async_lock, flags); |
120 | return ret; | 107 | return ret; |
121 | } | 108 | } |
@@ -127,20 +114,10 @@ static void async_run_entry_fn(struct work_struct *work) | |||
127 | { | 114 | { |
128 | struct async_entry *entry = | 115 | struct async_entry *entry = |
129 | container_of(work, struct async_entry, work); | 116 | container_of(work, struct async_entry, work); |
130 | struct async_entry *pos; | ||
131 | unsigned long flags; | 117 | unsigned long flags; |
132 | ktime_t uninitialized_var(calltime), delta, rettime; | 118 | ktime_t uninitialized_var(calltime), delta, rettime; |
133 | struct async_domain *running = entry->running; | ||
134 | 119 | ||
135 | /* 1) move self to the running queue, make sure it stays sorted */ | 120 | /* 1) run (and print duration) */ |
136 | spin_lock_irqsave(&async_lock, flags); | ||
137 | list_for_each_entry_reverse(pos, &running->domain, list) | ||
138 | if (entry->cookie < pos->cookie) | ||
139 | break; | ||
140 | list_move_tail(&entry->list, &pos->list); | ||
141 | spin_unlock_irqrestore(&async_lock, flags); | ||
142 | |||
143 | /* 2) run (and print duration) */ | ||
144 | if (initcall_debug && system_state == SYSTEM_BOOTING) { | 121 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
145 | printk(KERN_DEBUG "calling %lli_%pF @ %i\n", | 122 | printk(KERN_DEBUG "calling %lli_%pF @ %i\n", |
146 | (long long)entry->cookie, | 123 | (long long)entry->cookie, |
@@ -157,23 +134,22 @@ static void async_run_entry_fn(struct work_struct *work) | |||
157 | (long long)ktime_to_ns(delta) >> 10); | 134 | (long long)ktime_to_ns(delta) >> 10); |
158 | } | 135 | } |
159 | 136 | ||
160 | /* 3) remove self from the running queue */ | 137 | /* 2) remove self from the pending queues */ |
161 | spin_lock_irqsave(&async_lock, flags); | 138 | spin_lock_irqsave(&async_lock, flags); |
162 | list_del(&entry->list); | 139 | list_del_init(&entry->domain_list); |
163 | if (running->registered && --running->count == 0) | 140 | list_del_init(&entry->global_list); |
164 | list_del_init(&running->node); | ||
165 | 141 | ||
166 | /* 4) free the entry */ | 142 | /* 3) free the entry */ |
167 | kfree(entry); | 143 | kfree(entry); |
168 | atomic_dec(&entry_count); | 144 | atomic_dec(&entry_count); |
169 | 145 | ||
170 | spin_unlock_irqrestore(&async_lock, flags); | 146 | spin_unlock_irqrestore(&async_lock, flags); |
171 | 147 | ||
172 | /* 5) wake up any waiters */ | 148 | /* 4) wake up any waiters */ |
173 | wake_up(&async_done); | 149 | wake_up(&async_done); |
174 | } | 150 | } |
175 | 151 | ||
176 | static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct async_domain *running) | 152 | static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct async_domain *domain) |
177 | { | 153 | { |
178 | struct async_entry *entry; | 154 | struct async_entry *entry; |
179 | unsigned long flags; | 155 | unsigned long flags; |
@@ -196,16 +172,22 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct a | |||
196 | ptr(data, newcookie); | 172 | ptr(data, newcookie); |
197 | return newcookie; | 173 | return newcookie; |
198 | } | 174 | } |
175 | INIT_LIST_HEAD(&entry->domain_list); | ||
176 | INIT_LIST_HEAD(&entry->global_list); | ||
199 | INIT_WORK(&entry->work, async_run_entry_fn); | 177 | INIT_WORK(&entry->work, async_run_entry_fn); |
200 | entry->func = ptr; | 178 | entry->func = ptr; |
201 | entry->data = data; | 179 | entry->data = data; |
202 | entry->running = running; | 180 | entry->domain = domain; |
203 | 181 | ||
204 | spin_lock_irqsave(&async_lock, flags); | 182 | spin_lock_irqsave(&async_lock, flags); |
183 | |||
184 | /* allocate cookie and queue */ | ||
205 | newcookie = entry->cookie = next_cookie++; | 185 | newcookie = entry->cookie = next_cookie++; |
206 | list_add_tail(&entry->list, &async_pending); | 186 | |
207 | if (running->registered && running->count++ == 0) | 187 | list_add_tail(&entry->domain_list, &domain->pending); |
208 | list_add_tail(&running->node, &async_domains); | 188 | if (domain->registered) |
189 | list_add_tail(&entry->global_list, &async_global_pending); | ||
190 | |||
209 | atomic_inc(&entry_count); | 191 | atomic_inc(&entry_count); |
210 | spin_unlock_irqrestore(&async_lock, flags); | 192 | spin_unlock_irqrestore(&async_lock, flags); |
211 | 193 | ||
@@ -228,7 +210,7 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct a | |||
228 | */ | 210 | */ |
229 | async_cookie_t async_schedule(async_func_ptr *ptr, void *data) | 211 | async_cookie_t async_schedule(async_func_ptr *ptr, void *data) |
230 | { | 212 | { |
231 | return __async_schedule(ptr, data, &async_running); | 213 | return __async_schedule(ptr, data, &async_dfl_domain); |
232 | } | 214 | } |
233 | EXPORT_SYMBOL_GPL(async_schedule); | 215 | EXPORT_SYMBOL_GPL(async_schedule); |
234 | 216 | ||
@@ -236,18 +218,18 @@ EXPORT_SYMBOL_GPL(async_schedule); | |||
236 | * async_schedule_domain - schedule a function for asynchronous execution within a certain domain | 218 | * async_schedule_domain - schedule a function for asynchronous execution within a certain domain |
237 | * @ptr: function to execute asynchronously | 219 | * @ptr: function to execute asynchronously |
238 | * @data: data pointer to pass to the function | 220 | * @data: data pointer to pass to the function |
239 | * @running: running list for the domain | 221 | * @domain: the domain |
240 | * | 222 | * |
241 | * Returns an async_cookie_t that may be used for checkpointing later. | 223 | * Returns an async_cookie_t that may be used for checkpointing later. |
242 | * @running may be used in the async_synchronize_*_domain() functions | 224 | * @domain may be used in the async_synchronize_*_domain() functions to |
243 | * to wait within a certain synchronization domain rather than globally. | 225 | * wait within a certain synchronization domain rather than globally. A |
244 | * A synchronization domain is specified via the running queue @running to use. | 226 | * synchronization domain is specified via @domain. Note: This function |
245 | * Note: This function may be called from atomic or non-atomic contexts. | 227 | * may be called from atomic or non-atomic contexts. |
246 | */ | 228 | */ |
247 | async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data, | 229 | async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data, |
248 | struct async_domain *running) | 230 | struct async_domain *domain) |
249 | { | 231 | { |
250 | return __async_schedule(ptr, data, running); | 232 | return __async_schedule(ptr, data, domain); |
251 | } | 233 | } |
252 | EXPORT_SYMBOL_GPL(async_schedule_domain); | 234 | EXPORT_SYMBOL_GPL(async_schedule_domain); |
253 | 235 | ||
@@ -258,18 +240,7 @@ EXPORT_SYMBOL_GPL(async_schedule_domain); | |||
258 | */ | 240 | */ |
259 | void async_synchronize_full(void) | 241 | void async_synchronize_full(void) |
260 | { | 242 | { |
261 | mutex_lock(&async_register_mutex); | 243 | async_synchronize_full_domain(NULL); |
262 | do { | ||
263 | struct async_domain *domain = NULL; | ||
264 | |||
265 | spin_lock_irq(&async_lock); | ||
266 | if (!list_empty(&async_domains)) | ||
267 | domain = list_first_entry(&async_domains, typeof(*domain), node); | ||
268 | spin_unlock_irq(&async_lock); | ||
269 | |||
270 | async_synchronize_cookie_domain(next_cookie, domain); | ||
271 | } while (!list_empty(&async_domains)); | ||
272 | mutex_unlock(&async_register_mutex); | ||
273 | } | 244 | } |
274 | EXPORT_SYMBOL_GPL(async_synchronize_full); | 245 | EXPORT_SYMBOL_GPL(async_synchronize_full); |
275 | 246 | ||
@@ -284,51 +255,45 @@ EXPORT_SYMBOL_GPL(async_synchronize_full); | |||
284 | */ | 255 | */ |
285 | void async_unregister_domain(struct async_domain *domain) | 256 | void async_unregister_domain(struct async_domain *domain) |
286 | { | 257 | { |
287 | mutex_lock(&async_register_mutex); | ||
288 | spin_lock_irq(&async_lock); | 258 | spin_lock_irq(&async_lock); |
289 | WARN_ON(!domain->registered || !list_empty(&domain->node) || | 259 | WARN_ON(!domain->registered || !list_empty(&domain->pending)); |
290 | !list_empty(&domain->domain)); | ||
291 | domain->registered = 0; | 260 | domain->registered = 0; |
292 | spin_unlock_irq(&async_lock); | 261 | spin_unlock_irq(&async_lock); |
293 | mutex_unlock(&async_register_mutex); | ||
294 | } | 262 | } |
295 | EXPORT_SYMBOL_GPL(async_unregister_domain); | 263 | EXPORT_SYMBOL_GPL(async_unregister_domain); |
296 | 264 | ||
297 | /** | 265 | /** |
298 | * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain | 266 | * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain |
299 | * @domain: running list to synchronize on | 267 | * @domain: the domain to synchronize |
300 | * | 268 | * |
301 | * This function waits until all asynchronous function calls for the | 269 | * This function waits until all asynchronous function calls for the |
302 | * synchronization domain specified by the running list @domain have been done. | 270 | * synchronization domain specified by @domain have been done. |
303 | */ | 271 | */ |
304 | void async_synchronize_full_domain(struct async_domain *domain) | 272 | void async_synchronize_full_domain(struct async_domain *domain) |
305 | { | 273 | { |
306 | async_synchronize_cookie_domain(next_cookie, domain); | 274 | async_synchronize_cookie_domain(ASYNC_COOKIE_MAX, domain); |
307 | } | 275 | } |
308 | EXPORT_SYMBOL_GPL(async_synchronize_full_domain); | 276 | EXPORT_SYMBOL_GPL(async_synchronize_full_domain); |
309 | 277 | ||
310 | /** | 278 | /** |
311 | * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing | 279 | * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing |
312 | * @cookie: async_cookie_t to use as checkpoint | 280 | * @cookie: async_cookie_t to use as checkpoint |
313 | * @running: running list to synchronize on | 281 | * @domain: the domain to synchronize (%NULL for all registered domains) |
314 | * | 282 | * |
315 | * This function waits until all asynchronous function calls for the | 283 | * This function waits until all asynchronous function calls for the |
316 | * synchronization domain specified by running list @running submitted | 284 | * synchronization domain specified by @domain submitted prior to @cookie |
317 | * prior to @cookie have been done. | 285 | * have been done. |
318 | */ | 286 | */ |
319 | void async_synchronize_cookie_domain(async_cookie_t cookie, struct async_domain *running) | 287 | void async_synchronize_cookie_domain(async_cookie_t cookie, struct async_domain *domain) |
320 | { | 288 | { |
321 | ktime_t uninitialized_var(starttime), delta, endtime; | 289 | ktime_t uninitialized_var(starttime), delta, endtime; |
322 | 290 | ||
323 | if (!running) | ||
324 | return; | ||
325 | |||
326 | if (initcall_debug && system_state == SYSTEM_BOOTING) { | 291 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
327 | printk(KERN_DEBUG "async_waiting @ %i\n", task_pid_nr(current)); | 292 | printk(KERN_DEBUG "async_waiting @ %i\n", task_pid_nr(current)); |
328 | starttime = ktime_get(); | 293 | starttime = ktime_get(); |
329 | } | 294 | } |
330 | 295 | ||
331 | wait_event(async_done, lowest_in_progress(running) >= cookie); | 296 | wait_event(async_done, lowest_in_progress(domain) >= cookie); |
332 | 297 | ||
333 | if (initcall_debug && system_state == SYSTEM_BOOTING) { | 298 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
334 | endtime = ktime_get(); | 299 | endtime = ktime_get(); |
@@ -350,6 +315,18 @@ EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain); | |||
350 | */ | 315 | */ |
351 | void async_synchronize_cookie(async_cookie_t cookie) | 316 | void async_synchronize_cookie(async_cookie_t cookie) |
352 | { | 317 | { |
353 | async_synchronize_cookie_domain(cookie, &async_running); | 318 | async_synchronize_cookie_domain(cookie, &async_dfl_domain); |
354 | } | 319 | } |
355 | EXPORT_SYMBOL_GPL(async_synchronize_cookie); | 320 | EXPORT_SYMBOL_GPL(async_synchronize_cookie); |
321 | |||
322 | /** | ||
323 | * current_is_async - is %current an async worker task? | ||
324 | * | ||
325 | * Returns %true if %current is an async worker task. | ||
326 | */ | ||
327 | bool current_is_async(void) | ||
328 | { | ||
329 | struct worker *worker = current_wq_worker(); | ||
330 | |||
331 | return worker && worker->current_func == async_run_entry_fn; | ||
332 | } | ||