]> git.proxmox.com Git - mirror_qemu.git/blame - util/thread-pool.c
xen-block: Avoid leaks on new error path
[mirror_qemu.git] / util / thread-pool.c
CommitLineData
d354c7ec
PB
1/*
2 * QEMU block layer thread pool
3 *
4 * Copyright IBM, Corp. 2008
5 * Copyright Red Hat, Inc. 2012
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
16 */
d38ea87a 17#include "qemu/osdep.h"
1de7afc9
PB
18#include "qemu/queue.h"
19#include "qemu/thread.h"
10817bf0 20#include "qemu/coroutine.h"
c2b38b27 21#include "trace.h"
737e150e 22#include "block/thread-pool.h"
6a1751b7 23#include "qemu/main-loop.h"
d354c7ec 24
b811203c 25static void do_spawn_thread(ThreadPool *pool);
d354c7ec
PB
26
27typedef struct ThreadPoolElement ThreadPoolElement;
28
29enum ThreadState {
30 THREAD_QUEUED,
31 THREAD_ACTIVE,
32 THREAD_DONE,
d354c7ec
PB
33};
34
35struct ThreadPoolElement {
7c84b1b8 36 BlockAIOCB common;
b811203c 37 ThreadPool *pool;
d354c7ec
PB
38 ThreadPoolFunc *func;
39 void *arg;
19d092cf
PB
40
41 /* Moving state out of THREAD_QUEUED is protected by lock. After
42 * that, only the worker thread can write to it. Reads and writes
43 * of state and ret are ordered with memory barriers.
44 */
d354c7ec
PB
45 enum ThreadState state;
46 int ret;
47
48 /* Access to this list is protected by lock. */
49 QTAILQ_ENTRY(ThreadPoolElement) reqs;
50
0fdb7311 51 /* This list is only written by the thread pool's mother thread. */
d354c7ec
PB
52 QLIST_ENTRY(ThreadPoolElement) all;
53};
54
b811203c 55struct ThreadPool {
f7311ccc 56 AioContext *ctx;
c2e50e3d 57 QEMUBH *completion_bh;
b811203c 58 QemuMutex lock;
f7311ccc 59 QemuCond worker_stopped;
900fa208 60 QemuCond request_cond;
b811203c
SH
61 QEMUBH *new_thread_bh;
62
63 /* The following variables are only accessed from one AioContext. */
64 QLIST_HEAD(, ThreadPoolElement) head;
65
66 /* The following variables are protected by lock. */
67 QTAILQ_HEAD(, ThreadPoolElement) request_list;
68 int cur_threads;
69 int idle_threads;
70 int new_threads; /* backlog of threads we need to create */
71 int pending_threads; /* threads created but not running yet */
71ad4713
NSJ
72 int min_threads;
73 int max_threads;
b811203c
SH
74};
75
b811203c 76static void *worker_thread(void *opaque)
d354c7ec 77{
b811203c
SH
78 ThreadPool *pool = opaque;
79
80 qemu_mutex_lock(&pool->lock);
81 pool->pending_threads--;
82 do_spawn_thread(pool);
d354c7ec 83
232e9255 84 while (pool->cur_threads <= pool->max_threads) {
d354c7ec
PB
85 ThreadPoolElement *req;
86 int ret;
87
900fa208 88 if (QTAILQ_EMPTY(&pool->request_list)) {
b811203c 89 pool->idle_threads++;
900fa208 90 ret = qemu_cond_timedwait(&pool->request_cond, &pool->lock, 10000);
b811203c 91 pool->idle_threads--;
900fa208
PB
92 if (ret == 0 &&
93 QTAILQ_EMPTY(&pool->request_list) &&
94 pool->cur_threads > pool->min_threads) {
95 /* Timed out + no work to do + no need for warm threads = exit. */
96 break;
97 }
98 /*
99 * Even if there was some work to do, check if there aren't
100 * too many worker threads before picking it up.
101 */
102 continue;
d354c7ec
PB
103 }
104
b811203c
SH
105 req = QTAILQ_FIRST(&pool->request_list);
106 QTAILQ_REMOVE(&pool->request_list, req, reqs);
d354c7ec 107 req->state = THREAD_ACTIVE;
b811203c 108 qemu_mutex_unlock(&pool->lock);
d354c7ec
PB
109
110 ret = req->func(req->arg);
111
d354c7ec 112 req->ret = ret;
19d092cf
PB
113 /* Write ret before state. */
114 smp_wmb();
115 req->state = THREAD_DONE;
116
c2e50e3d 117 qemu_bh_schedule(pool->completion_bh);
3c7b72dd 118 qemu_mutex_lock(&pool->lock);
d354c7ec
PB
119 }
120
b811203c 121 pool->cur_threads--;
f7311ccc 122 qemu_cond_signal(&pool->worker_stopped);
b811203c 123 qemu_mutex_unlock(&pool->lock);
900fa208
PB
124
125 /*
126 * Wake up another thread, in case we got a wakeup but decided
127 * to exit due to pool->cur_threads > pool->max_threads.
128 */
129 qemu_cond_signal(&pool->request_cond);
d354c7ec
PB
130 return NULL;
131}
132
b811203c 133static void do_spawn_thread(ThreadPool *pool)
d354c7ec
PB
134{
135 QemuThread t;
136
137 /* Runs with lock taken. */
b811203c 138 if (!pool->new_threads) {
d354c7ec
PB
139 return;
140 }
141
b811203c
SH
142 pool->new_threads--;
143 pool->pending_threads++;
d354c7ec 144
4900116e 145 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
d354c7ec
PB
146}
147
148static void spawn_thread_bh_fn(void *opaque)
149{
b811203c
SH
150 ThreadPool *pool = opaque;
151
152 qemu_mutex_lock(&pool->lock);
153 do_spawn_thread(pool);
154 qemu_mutex_unlock(&pool->lock);
d354c7ec
PB
155}
156
b811203c 157static void spawn_thread(ThreadPool *pool)
d354c7ec 158{
b811203c
SH
159 pool->cur_threads++;
160 pool->new_threads++;
d354c7ec
PB
161 /* If there are threads being created, they will spawn new workers, so
162 * we don't spend time creating many threads in a loop holding a mutex or
163 * starving the current vcpu.
164 *
165 * If there are no idle threads, ask the main thread to create one, so we
166 * inherit the correct affinity instead of the vcpu affinity.
167 */
b811203c
SH
168 if (!pool->pending_threads) {
169 qemu_bh_schedule(pool->new_thread_bh);
d354c7ec
PB
170 }
171}
172
c2e50e3d 173static void thread_pool_completion_bh(void *opaque)
d354c7ec 174{
c2e50e3d 175 ThreadPool *pool = opaque;
d354c7ec
PB
176 ThreadPoolElement *elem, *next;
177
d354c7ec 178restart:
b811203c 179 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
3391f5e5 180 if (elem->state != THREAD_DONE) {
d354c7ec
PB
181 continue;
182 }
1faa5bb7
SH
183
184 trace_thread_pool_complete(pool, elem, elem->common.opaque,
185 elem->ret);
186 QLIST_REMOVE(elem, all);
187
188 if (elem->common.cb) {
19d092cf
PB
189 /* Read state before ret. */
190 smp_rmb();
3c80ca15
SH
191
192 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
193 * wait for another request that completed at the same time.
194 */
195 qemu_bh_schedule(pool->completion_bh);
196
19d092cf 197 elem->common.cb(elem->common.opaque, elem->ret);
b7a745dc
PL
198
199 /* We can safely cancel the completion_bh here regardless of someone
200 * else having scheduled it meanwhile because we reenter the
201 * completion function anyway (goto restart).
202 */
203 qemu_bh_cancel(pool->completion_bh);
204
8007429a 205 qemu_aio_unref(elem);
d354c7ec
PB
206 goto restart;
207 } else {
8007429a 208 qemu_aio_unref(elem);
d354c7ec
PB
209 }
210 }
211}
212
7c84b1b8 213static void thread_pool_cancel(BlockAIOCB *acb)
d354c7ec
PB
214{
215 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
b811203c 216 ThreadPool *pool = elem->pool;
d354c7ec
PB
217
218 trace_thread_pool_cancel(elem, elem->common.opaque);
219
6e8a355d 220 QEMU_LOCK_GUARD(&pool->lock);
900fa208 221 if (elem->state == THREAD_QUEUED) {
b811203c 222 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
c2e50e3d 223 qemu_bh_schedule(pool->completion_bh);
3391f5e5
FZ
224
225 elem->state = THREAD_DONE;
226 elem->ret = -ECANCELED;
d354c7ec 227 }
3391f5e5 228
3391f5e5
FZ
229}
230
7c84b1b8 231static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
3391f5e5
FZ
232{
233 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
234 ThreadPool *pool = elem->pool;
235 return pool->ctx;
d354c7ec
PB
236}
237
d7331bed 238static const AIOCBInfo thread_pool_aiocb_info = {
d354c7ec 239 .aiocb_size = sizeof(ThreadPoolElement),
3391f5e5
FZ
240 .cancel_async = thread_pool_cancel,
241 .get_aio_context = thread_pool_get_aio_context,
d354c7ec
PB
242};
243
aef04fc7
EGE
244BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
245 BlockCompletionFunc *cb, void *opaque)
d354c7ec
PB
246{
247 ThreadPoolElement *req;
aef04fc7
EGE
248 AioContext *ctx = qemu_get_current_aio_context();
249 ThreadPool *pool = aio_get_thread_pool(ctx);
d354c7ec 250
0fdb7311
EGE
251 /* Assert that the thread submitting work is the same running the pool */
252 assert(pool->ctx == qemu_get_current_aio_context());
253
d7331bed 254 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
d354c7ec
PB
255 req->func = func;
256 req->arg = arg;
257 req->state = THREAD_QUEUED;
b811203c 258 req->pool = pool;
d354c7ec 259
b811203c 260 QLIST_INSERT_HEAD(&pool->head, req, all);
d354c7ec 261
b811203c 262 trace_thread_pool_submit(pool, req, arg);
d354c7ec 263
b811203c
SH
264 qemu_mutex_lock(&pool->lock);
265 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
266 spawn_thread(pool);
d354c7ec 267 }
b811203c
SH
268 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
269 qemu_mutex_unlock(&pool->lock);
900fa208 270 qemu_cond_signal(&pool->request_cond);
d354c7ec
PB
271 return &req->common;
272}
273
274typedef struct ThreadPoolCo {
275 Coroutine *co;
276 int ret;
277} ThreadPoolCo;
278
279static void thread_pool_co_cb(void *opaque, int ret)
280{
281 ThreadPoolCo *co = opaque;
282
283 co->ret = ret;
b9e413dd 284 aio_co_wake(co->co);
d354c7ec
PB
285}
286
aef04fc7 287int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)
d354c7ec
PB
288{
289 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
290 assert(qemu_in_coroutine());
aef04fc7 291 thread_pool_submit_aio(func, arg, thread_pool_co_cb, &tpc);
d354c7ec
PB
292 qemu_coroutine_yield();
293 return tpc.ret;
294}
295
aef04fc7 296void thread_pool_submit(ThreadPoolFunc *func, void *arg)
d354c7ec 297{
aef04fc7 298 thread_pool_submit_aio(func, arg, NULL, NULL);
d354c7ec
PB
299}
300
71ad4713
NSJ
301void thread_pool_update_params(ThreadPool *pool, AioContext *ctx)
302{
303 qemu_mutex_lock(&pool->lock);
304
305 pool->min_threads = ctx->thread_pool_min;
306 pool->max_threads = ctx->thread_pool_max;
307
308 /*
309 * We either have to:
310 * - Increase the number available of threads until over the min_threads
311 * threshold.
900fa208 312 * - Bump the worker threads so that they exit, until under the max_threads
71ad4713
NSJ
313 * threshold.
314 * - Do nothing. The current number of threads fall in between the min and
315 * max thresholds. We'll let the pool manage itself.
316 */
317 for (int i = pool->cur_threads; i < pool->min_threads; i++) {
318 spawn_thread(pool);
319 }
320
321 for (int i = pool->cur_threads; i > pool->max_threads; i--) {
900fa208 322 qemu_cond_signal(&pool->request_cond);
71ad4713
NSJ
323 }
324
325 qemu_mutex_unlock(&pool->lock);
326}
327
b811203c
SH
328static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
329{
330 if (!ctx) {
331 ctx = qemu_get_aio_context();
332 }
333
334 memset(pool, 0, sizeof(*pool));
f7311ccc 335 pool->ctx = ctx;
c2e50e3d 336 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
b811203c 337 qemu_mutex_init(&pool->lock);
f7311ccc 338 qemu_cond_init(&pool->worker_stopped);
900fa208 339 qemu_cond_init(&pool->request_cond);
b811203c
SH
340 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
341
342 QLIST_INIT(&pool->head);
343 QTAILQ_INIT(&pool->request_list);
71ad4713
NSJ
344
345 thread_pool_update_params(pool, ctx);
b811203c
SH
346}
347
f7311ccc
SH
348ThreadPool *thread_pool_new(AioContext *ctx)
349{
350 ThreadPool *pool = g_new(ThreadPool, 1);
351 thread_pool_init_one(pool, ctx);
352 return pool;
353}
354
355void thread_pool_free(ThreadPool *pool)
356{
357 if (!pool) {
358 return;
359 }
360
361 assert(QLIST_EMPTY(&pool->head));
362
363 qemu_mutex_lock(&pool->lock);
364
365 /* Stop new threads from spawning */
366 qemu_bh_delete(pool->new_thread_bh);
367 pool->cur_threads -= pool->new_threads;
368 pool->new_threads = 0;
369
370 /* Wait for worker threads to terminate */
232e9255 371 pool->max_threads = 0;
900fa208 372 qemu_cond_broadcast(&pool->request_cond);
f7311ccc 373 while (pool->cur_threads > 0) {
f7311ccc
SH
374 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
375 }
376
377 qemu_mutex_unlock(&pool->lock);
378
c2e50e3d 379 qemu_bh_delete(pool->completion_bh);
900fa208 380 qemu_cond_destroy(&pool->request_cond);
f7311ccc
SH
381 qemu_cond_destroy(&pool->worker_stopped);
382 qemu_mutex_destroy(&pool->lock);
f7311ccc
SH
383 g_free(pool);
384}