workqueue: fix potential ABBA deadlock in stats summing
[fio.git] / workqueue.c
CommitLineData
a9da8ab2
JA
1/*
2 * Rated submission helpers
3 *
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
5 *
6 */
7#include <unistd.h>
8
9#include "fio.h"
10#include "ioengine.h"
11#include "flist.h"
12#include "workqueue.h"
13#include "lib/getrusage.h"
14
15struct submit_worker {
16 pthread_t thread;
17 pthread_mutex_t lock;
18 pthread_cond_t cond;
19 struct flist_head work_list;
20 unsigned int flags;
21 unsigned int index;
22 uint64_t seq;
23 struct workqueue *wq;
24 struct thread_data td;
25};
26
27enum {
28 SW_F_IDLE = 1 << 0,
29 SW_F_RUNNING = 1 << 1,
30 SW_F_EXIT = 1 << 2,
31 SW_F_EXITED = 1 << 3,
32 SW_F_ACCOUNTED = 1 << 4,
33 SW_F_ERROR = 1 << 5,
34};
35
36static struct submit_worker *__get_submit_worker(struct workqueue *wq,
37 unsigned int start,
38 unsigned int end,
39 struct submit_worker **best)
40{
41 struct submit_worker *sw = NULL;
42
43 while (start <= end) {
44 sw = &wq->workers[start];
45 if (sw->flags & SW_F_IDLE)
46 return sw;
47 if (!(*best) || sw->seq < (*best)->seq)
48 *best = sw;
49 start++;
50 }
51
52 return NULL;
53}
54
55static struct submit_worker *get_submit_worker(struct workqueue *wq)
56{
57 unsigned int next = wq->next_free_worker;
58 struct submit_worker *sw, *best = NULL;
59
60 assert(next < wq->max_workers);
61
62 sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
63 if (!sw && next)
64 sw = __get_submit_worker(wq, 0, next - 1, &best);
65
66 /*
67 * No truly idle found, use best match
68 */
69 if (!sw)
70 sw = best;
71
72 if (sw->index == wq->next_free_worker) {
73 if (sw->index + 1 < wq->max_workers)
74 wq->next_free_worker = sw->index + 1;
75 else
76 wq->next_free_worker = 0;
77 }
78
79 return sw;
80}
81
1391052a 82static bool all_sw_idle(struct workqueue *wq)
a9da8ab2
JA
83{
84 int i;
85
86 for (i = 0; i < wq->max_workers; i++) {
87 struct submit_worker *sw = &wq->workers[i];
88
89 if (!(sw->flags & SW_F_IDLE))
1391052a 90 return false;
a9da8ab2
JA
91 }
92
1391052a 93 return true;
a9da8ab2
JA
94}
95
96/*
97 * Must be serialized wrt workqueue_enqueue() by caller
98 */
99void workqueue_flush(struct workqueue *wq)
100{
101 wq->wake_idle = 1;
102
103 while (!all_sw_idle(wq)) {
104 pthread_mutex_lock(&wq->flush_lock);
105 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
106 pthread_mutex_unlock(&wq->flush_lock);
107 }
108
109 wq->wake_idle = 0;
110}
111
112/*
113 * Must be serialized by caller.
114 */
115int workqueue_enqueue(struct workqueue *wq, struct io_u *io_u)
116{
117 struct submit_worker *sw;
118
119 sw = get_submit_worker(wq);
120 if (sw) {
121 const enum fio_ddir ddir = acct_ddir(io_u);
122 struct thread_data *parent = wq->td;
123
124 if (ddir_rw(ddir)) {
125 parent->io_issues[ddir]++;
126 parent->io_issue_bytes[ddir] += io_u->xfer_buflen;
50a8ce86 127 parent->rate_io_issue_bytes[ddir] += io_u->xfer_buflen;
a9da8ab2
JA
128 }
129
130 pthread_mutex_lock(&sw->lock);
131 flist_add_tail(&io_u->verify_list, &sw->work_list);
132 sw->seq = ++wq->work_seq;
133 sw->flags &= ~SW_F_IDLE;
134 pthread_mutex_unlock(&sw->lock);
135
136 pthread_cond_signal(&sw->cond);
137 return FIO_Q_QUEUED;
138 }
139
140 return FIO_Q_BUSY;
141}
142
143static void handle_list(struct submit_worker *sw, struct flist_head *list)
144{
145 struct workqueue *wq = sw->wq;
146 struct io_u *io_u;
147
148 while (!flist_empty(list)) {
149 io_u = flist_first_entry(list, struct io_u, verify_list);
150 flist_del_init(&io_u->verify_list);
151 wq->fn(&sw->td, io_u);
152 }
153}
154
155static int init_submit_worker(struct submit_worker *sw)
156{
157 struct thread_data *parent = sw->wq->td;
158 struct thread_data *td = &sw->td;
159 int fio_unused ret;
160
161 memcpy(&td->o, &parent->o, sizeof(td->o));
162 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
163 td->o.uid = td->o.gid = -1U;
164 dup_files(td, parent);
736a50cd 165 td->eo = parent->eo;
a9da8ab2
JA
166 fio_options_mem_dupe(td);
167
168 if (ioengine_load(td))
169 goto err;
170
171 if (td->o.odirect)
172 td->io_ops->flags |= FIO_RAWIO;
173
174 td->pid = gettid();
175
176 INIT_FLIST_HEAD(&td->io_log_list);
177 INIT_FLIST_HEAD(&td->io_hist_list);
178 INIT_FLIST_HEAD(&td->verify_list);
179 INIT_FLIST_HEAD(&td->trim_list);
180 INIT_FLIST_HEAD(&td->next_rand_list);
181 td->io_hist_tree = RB_ROOT;
182
183 td->o.iodepth = 1;
184 if (td_io_init(td))
185 goto err_io_init;
186
187 fio_gettime(&td->epoch, NULL);
188 fio_getrusage(&td->ru_start);
ac28d905 189 clear_io_state(td, 1);
a9da8ab2
JA
190
191 td_set_runstate(td, TD_RUNNING);
192 td->flags |= TD_F_CHILD;
193 td->parent = parent;
194 return 0;
195
196err_io_init:
197 close_ioengine(td);
198err:
199 return 1;
200}
201
f5cd2907 202#ifdef CONFIG_SFAA
a9da8ab2
JA
203static void sum_val(uint64_t *dst, uint64_t *src)
204{
205 if (*src) {
206 __sync_fetch_and_add(dst, *src);
207 *src = 0;
208 }
209}
2a274336
JA
210#else
211static void sum_val(uint64_t *dst, uint64_t *src)
212{
213 if (*src) {
214 *dst += *src;
215 *src = 0;
216 }
217}
218#endif
a9da8ab2 219
a6a3469e
JA
220static void pthread_double_unlock(pthread_mutex_t *lock1,
221 pthread_mutex_t *lock2)
a9da8ab2 222{
f5cd2907 223#ifndef CONFIG_SFAA
a6a3469e
JA
224 pthread_mutex_unlock(lock1);
225 pthread_mutex_unlock(lock2);
2a274336 226#endif
a6a3469e
JA
227}
228
229static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
230{
231#ifndef CONFIG_SFAA
232 if (lock1 < lock2) {
233 pthread_mutex_lock(lock1);
234 pthread_mutex_lock(lock2);
235 } else {
236 pthread_mutex_lock(lock2);
237 pthread_mutex_lock(lock1);
238 }
239#endif
240}
241
242static void sum_ddir(struct thread_data *dst, struct thread_data *src,
243 enum fio_ddir ddir)
244{
245 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
2a274336 246
a9da8ab2
JA
247 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
248 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
249 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
250 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
251 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
2a274336 252
a6a3469e 253 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
a9da8ab2
JA
254}
255
256static void update_accounting(struct submit_worker *sw)
257{
258 struct thread_data *src = &sw->td;
259 struct thread_data *dst = sw->wq->td;
260
261 if (td_read(src))
262 sum_ddir(dst, src, DDIR_READ);
263 if (td_write(src))
264 sum_ddir(dst, src, DDIR_WRITE);
265 if (td_trim(src))
266 sum_ddir(dst, src, DDIR_TRIM);
267}
268
269static void *worker_thread(void *data)
270{
271 struct submit_worker *sw = data;
272 struct workqueue *wq = sw->wq;
273 struct thread_data *td = &sw->td;
274 unsigned int eflags = 0, ret;
275 FLIST_HEAD(local_list);
276
277 ret = init_submit_worker(sw);
278 pthread_mutex_lock(&sw->lock);
279 sw->flags |= SW_F_RUNNING;
280 if (ret)
281 sw->flags |= SW_F_ERROR;
282 pthread_mutex_unlock(&sw->lock);
283
284 pthread_mutex_lock(&wq->flush_lock);
285 pthread_cond_signal(&wq->flush_cond);
286 pthread_mutex_unlock(&wq->flush_lock);
287
288 if (sw->flags & SW_F_ERROR)
289 goto done;
290
291 while (1) {
292 pthread_mutex_lock(&sw->lock);
293
294 if (flist_empty(&sw->work_list)) {
295 if (sw->flags & SW_F_EXIT) {
296 pthread_mutex_unlock(&sw->lock);
297 break;
298 }
299
300 if (td->io_u_queued || td->cur_depth ||
301 td->io_u_in_flight) {
40649b00
JA
302 int ret;
303
a9da8ab2 304 pthread_mutex_unlock(&sw->lock);
40649b00
JA
305 ret = io_u_quiesce(td);
306 if (ret > 0)
307 td->cur_depth -= ret;
a9da8ab2
JA
308 pthread_mutex_lock(&sw->lock);
309 }
310
311 /*
312 * We dropped and reaquired the lock, check
313 * state again.
314 */
315 if (!flist_empty(&sw->work_list))
316 goto handle_work;
317
318 if (sw->flags & SW_F_EXIT) {
319 pthread_mutex_unlock(&sw->lock);
320 break;
321 } else if (!(sw->flags & SW_F_IDLE)) {
322 sw->flags |= SW_F_IDLE;
323 wq->next_free_worker = sw->index;
324 if (wq->wake_idle)
325 pthread_cond_signal(&wq->flush_cond);
326 }
327 update_accounting(sw);
328 pthread_cond_wait(&sw->cond, &sw->lock);
329 } else {
330handle_work:
331 flist_splice_init(&sw->work_list, &local_list);
332 }
333 pthread_mutex_unlock(&sw->lock);
334 handle_list(sw, &local_list);
335 }
336
337 update_accounting(sw);
338
339done:
340 pthread_mutex_lock(&sw->lock);
341 sw->flags |= (SW_F_EXITED | eflags);
342 pthread_mutex_unlock(&sw->lock);
343 return NULL;
344}
345
346static void free_worker(struct submit_worker *sw)
347{
348 struct thread_data *td = &sw->td;
349
350 fio_options_free(td);
351 close_and_free_files(td);
352 if (td->io_ops)
353 close_ioengine(td);
354 td_set_runstate(td, TD_EXITED);
355
356 pthread_cond_destroy(&sw->cond);
357 pthread_mutex_destroy(&sw->lock);
358}
359
360static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
361{
362 struct thread_data *parent = sw->wq->td;
363
364 pthread_join(sw->thread, NULL);
365 (*sum_cnt)++;
366 sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt);
367 free_worker(sw);
368}
369
370void workqueue_exit(struct workqueue *wq)
371{
372 unsigned int shutdown, sum_cnt = 0;
373 struct submit_worker *sw;
374 int i;
375
376 for (i = 0; i < wq->max_workers; i++) {
377 sw = &wq->workers[i];
378
379 pthread_mutex_lock(&sw->lock);
380 sw->flags |= SW_F_EXIT;
381 pthread_cond_signal(&sw->cond);
382 pthread_mutex_unlock(&sw->lock);
383 }
384
385 do {
386 shutdown = 0;
387 for (i = 0; i < wq->max_workers; i++) {
388 sw = &wq->workers[i];
389 if (sw->flags & SW_F_ACCOUNTED)
390 continue;
391 sw->flags |= SW_F_ACCOUNTED;
392 shutdown_worker(sw, &sum_cnt);
393 shutdown++;
394 }
395 } while (shutdown && shutdown != wq->max_workers);
396
397 free(wq->workers);
398 pthread_mutex_destroy(&wq->flush_lock);
399 pthread_cond_destroy(&wq->flush_cond);
2a274336 400 pthread_mutex_destroy(&wq->stat_lock);
a9da8ab2
JA
401}
402
403static int start_worker(struct workqueue *wq, unsigned int index)
404{
405 struct submit_worker *sw = &wq->workers[index];
406 int ret;
407
408 INIT_FLIST_HEAD(&sw->work_list);
409 pthread_cond_init(&sw->cond, NULL);
410 pthread_mutex_init(&sw->lock, NULL);
411 sw->wq = wq;
412 sw->index = index;
413
414 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
415 if (!ret) {
416 pthread_mutex_lock(&sw->lock);
417 sw->flags = SW_F_IDLE;
418 pthread_mutex_unlock(&sw->lock);
419 return 0;
420 }
421
422 free_worker(sw);
423 return 1;
424}
425
426int workqueue_init(struct thread_data *td, struct workqueue *wq,
427 workqueue_fn *fn, unsigned max_pending)
428{
429 unsigned int running;
430 int i, error;
431
432 wq->max_workers = max_pending;
433 wq->td = td;
434 wq->fn = fn;
435 wq->work_seq = 0;
436 wq->next_free_worker = 0;
437 pthread_cond_init(&wq->flush_cond, NULL);
438 pthread_mutex_init(&wq->flush_lock, NULL);
2a274336 439 pthread_mutex_init(&wq->stat_lock, NULL);
a9da8ab2
JA
440
441 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
442
443 for (i = 0; i < wq->max_workers; i++)
444 if (start_worker(wq, i))
445 break;
446
447 wq->max_workers = i;
bddc8d16
JA
448 if (!wq->max_workers)
449 goto err;
a9da8ab2
JA
450
451 /*
452 * Wait for them all to be started and initialized
453 */
454 error = 0;
455 do {
456 struct submit_worker *sw;
457
458 running = 0;
459 pthread_mutex_lock(&wq->flush_lock);
460 for (i = 0; i < wq->max_workers; i++) {
461 sw = &wq->workers[i];
462 pthread_mutex_lock(&sw->lock);
463 if (sw->flags & SW_F_RUNNING)
464 running++;
465 if (sw->flags & SW_F_ERROR)
466 error++;
467 pthread_mutex_unlock(&sw->lock);
468 }
469
470 if (error || running == wq->max_workers) {
471 pthread_mutex_unlock(&wq->flush_lock);
472 break;
473 }
474
475 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
476 pthread_mutex_unlock(&wq->flush_lock);
477 } while (1);
478
bddc8d16
JA
479 if (!error)
480 return 0;
a9da8ab2 481
bddc8d16
JA
482err:
483 log_err("Can't create rate workqueue\n");
484 td_verror(td, ESRCH, "workqueue_init");
485 workqueue_exit(wq);
486 return 1;
a9da8ab2 487}