AioContext removal: LinuxAioState and ThreadPool
v2 cover AioContext removal: LinuxAioState and ThreadPool
Just remove some AioContext lock in LinuxAioState and ThreadPool. Not related to anything specific, so I decided to send it as a separate patch.
These patches are taken from Paolo's old draft series.
---
v2:
* assertion in thread_pool
* remove useless BlockDriverState * param in patch 1 and 2
* io_uring cleaned too
Emanuele Giuseppe Esposito (2):
io_uring: use LuringState from the running thread
thread-pool: use ThreadPool from the running thread
Paolo Bonzini (1):
linux-aio: use LinuxAioState from the running thread
block/file-posix.c | 43 ++++++++++++++++------------------------- block/file-win32.c | 2 +- block/io_uring.c | 26 ++++++++++++++----------- block/linux-aio.c | 34 +++++++++++++++++--------------- block/qcow2-threads.c | 2 +- include/block/aio.h | 8 -------- include/block/raw-aio.h | 18 ++++++++--------- util/thread-pool.c | 5 ++++- 8 files changed, 64 insertions(+), 74 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/3 linux-aio: use LinuxAioState from the running thread
From: Paolo Bonzini <[email protected]>
Remove usage of aio_context_acquire by always submitting asynchronous AIO to the current thread's LinuxAioState.
In order to prevent mistakes from the caller side, avoid passing LinuxAioState in laio_io_{plug/unplug} and laio_co_submit.
--- block/file-posix.c | 10 +++------- block/linux-aio.c | 34 ++++++++++++++++++---------------- include/block/aio.h | 4 ---- include/block/raw-aio.h | 10 ++++------ 4 files changed, 25 insertions(+), 33 deletions(-)
block/file-posix.c +3 -7
@@ -2099,10 +2099,8 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,#endif #ifdef CONFIG_LINUX_AIO }else if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs));assert (qiov->size == bytes); -return laio_co_submit (bs, aio, s->fd, offset, qiov, type, - s->aio_max_batch); +return laio_co_submit (s->fd, offset, qiov, type, s->aio_max_batch);#endif }
@@ -2142,8 +2140,7 @@ static void raw_aio_plug(BlockDriverState *bs)BDRVRawState __attribute__((unused)) *s = bs->opaque;#ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs)); -laio_io_plug (bs, aio); +laio_io_plug (); }#endif #ifdef CONFIG_LINUX_IO_URING
@@ -2159,8 +2156,7 @@ static void raw_aio_unplug(BlockDriverState *bs)BDRVRawState __attribute__((unused)) *s = bs->opaque;#ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs)); -laio_io_unplug (bs, aio, s->aio_max_batch); +laio_io_unplug (s->aio_max_batch); }#endif #ifdef CONFIG_LINUX_IO_URING
block/linux-aio.c +18 -16
@@ -16,6 +16,9 @@#include "qemu/coroutine.h" #include "qapi/error.h" +/* Only used for assertions. */ +#include "qemu/coroutine_int.h" +#include <libaio.h> /*
@@ -56,10 +59,8 @@ struct LinuxAioState {io_context_t ctx;EventNotifier e; -/* io queue for submit at batch. Protected by AioContext lock. */ +/* All data is only used in one I/O thread. */ LaioQueue io_q; - -/* I/O completion processing. Only runs in I/O thread. */ QEMUBH *completion_bh;int event_idx;int event_max;
@@ -102,9 +103,8 @@ static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)* later. Coroutines cannot be entered recursively so avoid doing * that! */ -if (!qemu_coroutine_entered (laiocb->co)) { -aio_co_wake (laiocb->co); - } +assert (laiocb->co->ctx == laiocb->ctx->aio_context); +qemu_coroutine_enter_if_inactive (laiocb->co); }/**
@@ -232,13 +232,11 @@ static void qemu_laio_process_completions(LinuxAioState *s)static void qemu_laio_process_completions_and_submit (LinuxAioState *s) { -aio_context_acquire (s->aio_context);qemu_laio_process_completions (s);if (!s->io_q.plugged && !QSIMPLEQ_EMPTY (&s->io_q.pending)) {ioq_submit (s); } -aio_context_release (s->aio_context); }static void qemu_laio_completion_bh (void *opaque)
@@ -354,14 +352,18 @@ static uint64_t laio_max_batch(LinuxAioState *s, uint64_t dev_max_batch)return max_batch; } -void laio_io_plug (BlockDriverState *bs,LinuxAioState *s) -{ +void laio_io_plug (void ){ +AioContext *ctx =qemu_get_current_aio_context (); +LinuxAioState *s =aio_get_linux_aio (ctx); + s->io_q.plugged++; } -void laio_io_unplug (BlockDriverState *bs,LinuxAioState *s, -uint64_t dev_max_batch) +void laio_io_unplug (uint64_t dev_max_batch) { +AioContext *ctx =qemu_get_current_aio_context (); +LinuxAioState *s =aio_get_linux_aio (ctx); +assert (s->io_q.plugged); s->io_q.plugged--;
@@ -411,15 +413,15 @@ static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,return 0 ; } -int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd, -uint64_t offset,QEMUIOVector *qiov,int type, -uint64_t dev_max_batch) +int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qiov, +int type,uint64_t dev_max_batch) {int ret; +AioContext *ctx =qemu_get_current_aio_context ();struct qemu_laiocb laiocb = { .co =qemu_coroutine_self (), .nbytes = qiov->size, - .ctx = s, + .ctx =aio_get_linux_aio (ctx), .ret = -EINPROGRESS , .is_read = (type ==QEMU_AIO_READ ), .qiov = qiov,
include/block/aio.h +0 -4
@@ -200,10 +200,6 @@ struct AioContext {struct ThreadPool *thread_pool;#ifdef CONFIG_LINUX_AIO -/* -* State for native Linux AIO. Uses aio_context_acquire/release for -* locking. -*/ struct LinuxAioState *linux_aio;#endif #ifdef CONFIG_LINUX_IO_URING
include/block/raw-aio.h +4 -6
@@ -50,14 +50,12 @@typedef struct LinuxAioState LinuxAioState ;LinuxAioState *laio_init (Error **errp);void laio_cleanup (LinuxAioState *s); -int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd, -uint64_t offset,QEMUIOVector *qiov,int type, -uint64_t dev_max_batch); +int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qiov, +int type,uint64_t dev_max_batch);void laio_detach_aio_context (LinuxAioState *s,AioContext *old_context);void laio_attach_aio_context (LinuxAioState *s,AioContext *new_context); -void laio_io_plug (BlockDriverState *bs,LinuxAioState *s); -void laio_io_unplug (BlockDriverState *bs,LinuxAioState *s, -uint64_t dev_max_batch); +void laio_io_plug (void ); +void laio_io_unplug (uint64_t dev_max_batch);#endif /* io_uring.c - Linux io_uring implementation */ #ifdef CONFIG_LINUX_IO_URING
Signature · 1 line
2.31.1
Re: [PATCH v2 1/3] linux-aio: use LinuxAioState from the running thread
Quoted 80 lines · Emanuele Giuseppe Esposito
From: Paolo Bonzini <[email protected]>
Remove usage of
aio_context_acquireby always submitting asynchronous AIO to the current thread's LinuxAioState.In order to prevent mistakes from the caller side, avoid passing LinuxAioState in laio_io_{plug/unplug} and
laio_co_submit.Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/file-posix.c | 10 +++------- block/linux-aio.c | 34 ++++++++++++++++++---------------- include/block/aio.h | 4 ---- include/block/raw-aio.h | 10 ++++------ 4 files changed, 25 insertions(+), 33 deletions(-)block/file-posix.c +3
-7@@ -2099,10 +2099,8 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,#endif #ifdef CONFIG_LINUX_AIO }else if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs));assert (qiov->size == bytes); -return laio_co_submit (bs, aio, s->fd, offset, qiov, type, - s->aio_max_batch); +return laio_co_submit (s->fd, offset, qiov, type, s->aio_max_batch);#endif }@@ -2142,8 +2140,7 @@ static void raw_aio_plug(BlockDriverState *bs)BDRVRawState __attribute__((unused)) *s = bs->opaque;#ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs)); -laio_io_plug (bs, aio); +laio_io_plug (); }#endif #ifdef CONFIG_LINUX_IO_URING @@ -2159,8 +2156,7 @@ static void raw_aio_unplug(BlockDriverState *bs)BDRVRawState __attribute__((unused)) *s = bs->opaque;#ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { -LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs)); -laio_io_unplug (bs, aio, s->aio_max_batch); +laio_io_unplug (s->aio_max_batch); }#endif #ifdef CONFIG_LINUX_IO_URING block/linux-aio.c +4
-3@@ -16,6 +16,9 @@#include "qemu/coroutine.h" #include "qapi/error.h" +/* Only used for assertions. */ +#include "qemu/coroutine_int.h" +#include <libaio.h> /* @@ -56,10 +59,8 @@ struct LinuxAioState {io_context_t ctx;EventNotifier e; -/* io queue for submit at batch. Protected by AioContext lock. */ +/* All data is only used in one I/O thread. */ LaioQueue io_q; - -/* I/O completion processing. Only runs in I/O thread. */ QEMUBH *completion_bh;int event_idx;int event_max;
block/linux-aio.c static void qemu_laio_process_completion(struct qemu_laiocb *laiocb) · line 107
Quoted 8 lines
block/linux-aio.c +2
-3@@ -102,9 +103,8 @@ static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)* later. Coroutines cannot be entered recursively so avoid doing * that! */ -if (!qemu_coroutine_entered (laiocb->co)) { -aio_co_wake (laiocb->co); - } +assert (laiocb->co->ctx == laiocb->ctx->aio_context); +qemu_coroutine_enter_if_inactive (laiocb->co);
This is wrong, it misses the aio_context_acquire/aio_context_release pair in aio_co_enter() (which is called by aio_co_wake()).
Likewise in patch 2.
Paolo
Re: [PATCH v2 1/3] linux-aio: use LinuxAioState from the running thread
Am 28/10/2022 um 13:51 schrieb Paolo Bonzini:
Quoted 96 lines · Emanuele Giuseppe Esposito
Quoted 82 lines
From: Paolo Bonzini <[email protected]>
Remove usage of
aio_context_acquireby always submitting asynchronous AIO to the current thread's LinuxAioState.In order to prevent mistakes from the caller side, avoid passing LinuxAioState
in laio_io_{plug/unplug} andlaio_co_submit.Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Emanuele Giuseppe Esposito <[email protected]>---
block/file-posix.c | 10 +++------- block/linux-aio.c | 34 ++++++++++++++++++---------------- include/block/aio.h | 4 ----include/block/raw-aio.h| 10 ++++------4 files changed, 25 insertions(+), 33 deletions(-)block/file-posix.c +1
-3@@ -2099,10 +2099,8 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, #endif #ifdef CONFIG_LINUX_AIO } else if (s->use_linux_aio) { - LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); assert(qiov->size == bytes); - return laio_co_submit(bs, aio, s->fd, offset, qiov, type, - s->aio_max_batch); + return laio_co_submit(s->fd, offset, qiov, type, s->aio_max_batch);#endif
}
@@ -2142,8 +2140,7 @@ static voidraw_aio_plug(BlockDriverState *bs)BDRVRawState __attribute__((unused)) *s =bs->opaque; #ifdefCONFIG_LINUX_AIO
if (s->use_linux_aio) {
- LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
-laio_io_plug(bs, aio);
+laio_io_plug();
}
#endif
#ifdefCONFIG_LINUX_IO_URINGblock/file-posix.c +1
-2@@ -2159,8 +2156,7 @@ static void raw_aio_unplug(BlockDriverState *bs) BDRVRawState __attribute__((unused)) *s = bs->opaque; #ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { - LinuxAioState *aio =aio_get_linux_aio (bdrv_get_aio_context (bs)); - laio_io_unplug(bs, aio, s->aio_max_batch); + laio_io_unplug(s->aio_max_batch); } #endif#ifdef
CONFIG_LINUX_IO_URINGblock/linux-aio.c +0
-0@@ -16,6 +16,9 @@ #include "qemu/coroutine.h" #include "qapi/error.h"+/* Only used for assertions. */
block/linux-aio.c +2
-0+#include "qemu/coroutine_int.h" +#include <
libaio.h>
/*block/linux-aio.c +0
-0@@ -56,10 +59,8 @@ struct LinuxAioState { io_context_t ctx; EventNotifier e;- /* io queue for submit at batch. Protected by AioContext lock. */ + /* All data is only used in one I/O thread. */ LaioQueue
io_q;
-
- /* I/O completion processing. Only runs in I/O thread. */ QEMUBH*completion_bh;
intevent_idx;
intevent_max;block/linux-aio.c static void qemu_laio_process_completion(struct · line 108
Quoted 9 lines
block/linux-aio.c +2
-3@@ -102,9 +103,8 @@ static void qemu_laio_process_completion(structqemu_laiocb *laiocb) * later. Coroutines cannot be entered recursively so avoid doing * that! */ - if (!qemu_coroutine_entered(laiocb->co)) { - aio_co_wake(laiocb->co); - } + assert(laiocb->co->ctx == laiocb->ctx->aio_context); + qemu_coroutine_enter_if_inactive(laiocb->co);This is wrong, it misses the aio_context_acquire/aio_context_release pair in
aio_co_enter()(which is called byaio_co_wake()).
Quoted 1 line
Likewise in patch 2.
Makes sense, I'll drop this hunk in v3.
Emanuele
PATCH v2 2/3 io_uring: use LuringState from the running thread
In reply to Emanuele Giuseppe EspositoRemove usage of aio_context_acquire by always submitting asynchronous AIO to the current thread's LuringState.
In order to prevent mistakes from the caller side, avoid passing LuringState in luring_io_{plug/unplug} and luring_co_submit.
--- block/file-posix.c | 12 ++++-------- block/io_uring.c | 26 +++++++++++++++----------- include/block/aio.h | 4 ---- include/block/raw-aio.h | 8 ++++---- 4 files changed, 23 insertions(+), 27 deletions(-)
block/file-posix.c +4 -8
@@ -2093,9 +2093,8 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, type |=QEMU_AIO_MISALIGNED ; #ifdef CONFIG_LINUX_IO_URING }else if (s->use_linux_io_uring) { -LuringState *aio =aio_get_linux_io_uring (bdrv_get_aio_context (bs));assert (qiov->size == bytes); -return luring_co_submit (bs, aio, s->fd, offset, qiov, type); +return luring_co_submit (bs, s->fd, offset, qiov, type);#endif #ifdef CONFIG_LINUX_AIO }else if (s->use_linux_aio) {
@@ -2145,8 +2144,7 @@ static void raw_aio_plug(BlockDriverState *bs)#endif #ifdef CONFIG_LINUX_IO_URING if (s->use_linux_io_uring) { -LuringState *aio =aio_get_linux_io_uring (bdrv_get_aio_context (bs)); -luring_io_plug (bs, aio); +luring_io_plug (); }#endif }
@@ -2161,8 +2159,7 @@ static void raw_aio_unplug(BlockDriverState *bs)#endif #ifdef CONFIG_LINUX_IO_URING if (s->use_linux_io_uring) { -LuringState *aio =aio_get_linux_io_uring (bdrv_get_aio_context (bs)); -luring_io_unplug (bs, aio); +luring_io_unplug (); }#endif }
@@ -2186,8 +2183,7 @@ static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)#ifdef CONFIG_LINUX_IO_URING if (s->use_linux_io_uring) { -LuringState *aio =aio_get_linux_io_uring (bdrv_get_aio_context (bs)); -return luring_co_submit (bs, aio, s->fd,0 ,NULL ,QEMU_AIO_FLUSH ); +return luring_co_submit (bs, s->fd,0 ,NULL ,QEMU_AIO_FLUSH ); }#endif return raw_thread_pool_submit (bs, handle_aiocb_flush, &acb);
block/io_uring.c +15 -11
@@ -19,6 +19,8 @@#include "qapi/error.h" #include "trace.h" +/* Only used for assertions. */ +#include "qemu/coroutine_int.h" /* io_uring ring size */ #define MAX_ENTRIES 128
@@ -52,10 +54,9 @@ typedef struct LuringState {struct io_uring ring; -/* io queue for submit at batch. Protected by AioContext lock. */ +/* All data is only used in one I/O thread. */ LuringQueue io_q; -/* I/O completion processing. Only runs in I/O thread. */ QEMUBH *completion_bh; } LuringState;
@@ -211,9 +212,8 @@ end:* eventually runs later. Coroutines cannot be entered recursively * so avoid doing that! */ -if (!qemu_coroutine_entered (luringcb->co)) { -aio_co_wake (luringcb->co); - } +assert (luringcb->co->ctx == luringcb->aio_context); +qemu_coroutine_enter_if_inactive (luringcb->co); }qemu_bh_cancel (s->completion_bh); }
@@ -264,13 +264,11 @@ static int ioq_submit(LuringState *s)static void luring_process_completions_and_submit (LuringState *s) { -aio_context_acquire (s->aio_context);luring_process_completions (s);if (!s->io_q.plugged && s->io_q.in_queue >0 ) {ioq_submit (s); } -aio_context_release (s->aio_context); }static void qemu_luring_completion_bh (void *opaque)
@@ -308,14 +306,18 @@ static void ioq_init(LuringQueue *io_q) io_q->blocked = false; } -void luring_io_plug (BlockDriverState *bs,LuringState *s) +void luring_io_plug (void ) { +AioContext *ctx =qemu_get_current_aio_context (); +LuringState *s =aio_get_linux_io_uring (ctx);trace_luring_io_plug (s); s->io_q.plugged++; } -void luring_io_unplug (BlockDriverState *bs,LuringState *s) +void luring_io_unplug (void ) { +AioContext *ctx =qemu_get_current_aio_context (); +LuringState *s =aio_get_linux_io_uring (ctx);assert (s->io_q.plugged);trace_luring_io_unplug (s, s->io_q.blocked, s->io_q.plugged, s->io_q.in_queue, s->io_q.in_flight);
@@ -375,10 +377,12 @@ static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,return 0 ; } -int coroutine_fn luring_co_submit(BlockDriverState *bs, LuringState *s, int fd, -uint64_t offset,QEMUIOVector *qiov,int type) +int coroutine_fn luring_co_submit(BlockDriverState *bs, int fd, uint64_t offset, +QEMUIOVector *qiov,int type) {int ret; +AioContext *ctx =qemu_get_current_aio_context (); +LuringState *s =aio_get_linux_io_uring (ctx);LuringAIOCB luringcb = { .co =qemu_coroutine_self (), .ret = -EINPROGRESS ,
include/block/aio.h +0 -4
@@ -203,10 +203,6 @@ struct AioContext {struct LinuxAioState *linux_aio;#endif #ifdef CONFIG_LINUX_IO_URING -/* -* State for Linux io_uring. Uses aio_context_acquire/release for -* locking. -*/ struct LuringState *linux_io_uring;/* State for file descriptor monitoring using Linux io_uring */
include/block/raw-aio.h +4 -4
@@ -62,12 +62,12 @@ void laio_io_unplug(uint64_t dev_max_batch);typedef struct LuringState LuringState ;LuringState *luring_init (Error **errp);void luring_cleanup (LuringState *s); -int coroutine_fn luring_co_submit(BlockDriverState *bs, LuringState *s, int fd, -uint64_t offset,QEMUIOVector *qiov,int type); +int coroutine_fn luring_co_submit(BlockDriverState *bs, int fd, uint64_t offset, +QEMUIOVector *qiov,int type);void luring_detach_aio_context (LuringState *s,AioContext *old_context);void luring_attach_aio_context (LuringState *s,AioContext *new_context); -void luring_io_plug (BlockDriverState *bs,LuringState *s); -void luring_io_unplug (BlockDriverState *bs,LuringState *s); +void luring_io_plug (void ); +void luring_io_unplug (void );#endif #ifdef _WIN32
Signature · 1 line
2.31.1
PATCH v2 3/3 thread-pool: use ThreadPool from the running thread
In reply to Emanuele Giuseppe EspositoUse qemu_get_current_aio_context() where possible, since we always submit work to the current thread anyways.
We want to also be sure that the thread submitting the work is the same as the one processing the pool, to avoid adding synchronization to the pool list.
--- block/file-posix.c | 21 ++++++++++----------- block/file-win32.c | 2 +- block/qcow2-threads.c | 2 +- util/thread-pool.c | 5 ++++- 4 files changed, 16 insertions(+), 14 deletions(-)
block/file-posix.c +10 -11
@@ -2044,11 +2044,10 @@ out:return result; } -static int coroutine_fn raw_thread_pool_submit(BlockDriverState *bs, -ThreadPoolFunc func,void *arg) +static int coroutine_fn raw_thread_pool_submit(ThreadPoolFunc func, void *arg) {/* @bs can be NULL, bdrv_get_aio_context() returns the main context then */ -ThreadPool *pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); +ThreadPool *pool =aio_get_thread_pool (qemu_get_current_aio_context ());return thread_pool_submit_co (pool, func, arg); }
@@ -2116,7 +2115,7 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, };assert (qiov->size == bytes); -return raw_thread_pool_submit (bs, handle_aiocb_rw, &acb); +return raw_thread_pool_submit (handle_aiocb_rw, &acb); } static int coroutine_fn raw_co_preadv(BlockDriverState *bs, int64_t offset,
@@ -2186,7 +2185,7 @@ static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)return luring_co_submit (bs, s->fd,0 ,NULL ,QEMU_AIO_FLUSH ); }#endif -return raw_thread_pool_submit (bs, handle_aiocb_flush, &acb); +return raw_thread_pool_submit (handle_aiocb_flush, &acb); } static void raw_aio_attach_aio_context(BlockDriverState *bs,
@@ -2248,7 +2247,7 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset, }, }; -return raw_thread_pool_submit (bs, handle_aiocb_truncate, &acb); +return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
@@ -2998,7 +2997,7 @@ raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes, acb.aio_type |=QEMU_AIO_BLKDEV ; } - ret =raw_thread_pool_submit (bs, handle_aiocb_discard, &acb); + ret =raw_thread_pool_submit (handle_aiocb_discard, &acb);raw_account_discard (s, bytes, ret);return ret; }
@@ -3073,7 +3072,7 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, handler = handle_aiocb_write_zeroes; } -return raw_thread_pool_submit (bs, handler, &acb); +return raw_thread_pool_submit (handler, &acb); } static int coroutine_fn raw_co_pwrite_zeroes(
@@ -3284,7 +3283,7 @@ static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, }, }; -return raw_thread_pool_submit (bs, handle_aiocb_copy_range, &acb); +return raw_thread_pool_submit (handle_aiocb_copy_range, &acb); }BlockDriver bdrv_file = {
@@ -3614,7 +3613,7 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)struct sg_io_hdr *io_hdr = buf;if (io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_OUT || io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_IN ) { -return pr_manager_execute (s->pr_mgr,bdrv_get_aio_context (bs), +return pr_manager_execute (s->pr_mgr,qemu_get_current_aio_context (), s->fd, io_hdr); } }
@@ -3630,7 +3629,7 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) }, }; -return raw_thread_pool_submit (bs, handle_aiocb_ioctl, &acb); +return raw_thread_pool_submit (handle_aiocb_ioctl, &acb); }#endif /* linux */
block/file-win32.c +1 -1
@@ -167,7 +167,7 @@ static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, acb->aio_offset = offset;trace_file_paio_submit (acb, opaque, offset, count, type); - pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); + pool =aio_get_thread_pool (qemu_get_current_aio_context ());return thread_pool_submit_aio (pool, aio_worker, acb, cb, opaque); }
block/qcow2-threads.c +1 -1
@@ -42,7 +42,7 @@ qcow2_co_process(BlockDriverState *bs, ThreadPoolFunc *func, void *arg) {int ret;BDRVQcow2State *s = bs->opaque; -ThreadPool *pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); +ThreadPool *pool =aio_get_thread_pool (qemu_get_current_aio_context ());qemu_co_mutex_lock (&s->lock);while (s->nb_threads >=QCOW2_MAX_THREADS ) {
util/thread-pool.c +4 -1
@@ -48,7 +48,7 @@ struct ThreadPoolElement {/* Access to this list is protected by lock. */ QTAILQ_ENTRY (ThreadPoolElement) reqs; -/* Access to this list is protected by the global mutex. */ +/* This list is only written by the thread pool's mother thread. */ QLIST_ENTRY (ThreadPoolElement) all; };
@@ -251,6 +251,9 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, {ThreadPoolElement *req; +/* Assert that the thread submitting work is the same running the pool */ +assert (pool->ctx ==qemu_get_current_aio_context ()); + req =qemu_aio_get (&thread_pool_aiocb_info,NULL , cb, opaque); req->func = func; req->arg = arg;
Signature · 1 line
2.31.1
Re: [PATCH v2 3/3] thread-pool: use ThreadPool from the running thread
Quoted 146 lines · Emanuele Giuseppe Esposito
Use
qemu_get_current_aio_context()where possible, since we always submit work to the current thread anyways.We want to also be sure that the thread submitting the work is the same as the one processing the pool, to avoid adding synchronization to the pool list.
Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Emanuele Giuseppe Esposito <[email protected]>--- block/file-posix.c | 21 ++++++++++----------- block/file-win32.c | 2 +- block/qcow2-threads.c | 2 +- util/thread-pool.c | 5 ++++- 4 files changed, 16 insertions(+), 14 deletions(-)block/file-posix.c +10
-11@@ -2044,11 +2044,10 @@ out:return result; } -static int coroutine_fn raw_thread_pool_submit(BlockDriverState *bs, -ThreadPoolFunc func,void *arg) +static int coroutine_fn raw_thread_pool_submit(ThreadPoolFunc func, void *arg) {/* @bs can be NULL, bdrv_get_aio_context() returns the main context then */ -ThreadPool *pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); +ThreadPool *pool =aio_get_thread_pool (qemu_get_current_aio_context ());return thread_pool_submit_co (pool, func, arg); }@@ -2116,7 +2115,7 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, };assert (qiov->size == bytes); -return raw_thread_pool_submit (bs, handle_aiocb_rw, &acb); +return raw_thread_pool_submit (handle_aiocb_rw, &acb); } static int coroutine_fn raw_co_preadv(BlockDriverState *bs, int64_t offset,@@ -2186,7 +2185,7 @@ static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)return luring_co_submit (bs, s->fd,0 ,NULL ,QEMU_AIO_FLUSH ); }#endif -return raw_thread_pool_submit (bs, handle_aiocb_flush, &acb); +return raw_thread_pool_submit (handle_aiocb_flush, &acb); } static void raw_aio_attach_aio_context(BlockDriverState *bs,@@ -2248,7 +2247,7 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset, }, }; -return raw_thread_pool_submit (bs, handle_aiocb_truncate, &acb); +return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,@@ -2998,7 +2997,7 @@ raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes, acb.aio_type |=QEMU_AIO_BLKDEV ; } - ret =raw_thread_pool_submit (bs, handle_aiocb_discard, &acb); + ret =raw_thread_pool_submit (handle_aiocb_discard, &acb);raw_account_discard (s, bytes, ret);return ret; }@@ -3073,7 +3072,7 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, handler = handle_aiocb_write_zeroes; } -return raw_thread_pool_submit (bs, handler, &acb); +return raw_thread_pool_submit (handler, &acb); } static int coroutine_fn raw_co_pwrite_zeroes(@@ -3284,7 +3283,7 @@ static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, }, }; -return raw_thread_pool_submit (bs, handle_aiocb_copy_range, &acb); +return raw_thread_pool_submit (handle_aiocb_copy_range, &acb); }BlockDriver bdrv_file = {@@ -3614,7 +3613,7 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)struct sg_io_hdr *io_hdr = buf;if (io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_OUT || io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_IN ) { -return pr_manager_execute (s->pr_mgr,bdrv_get_aio_context (bs), +return pr_manager_execute (s->pr_mgr,qemu_get_current_aio_context (), s->fd, io_hdr); } }@@ -3630,7 +3629,7 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) }, }; -return raw_thread_pool_submit (bs, handle_aiocb_ioctl, &acb); +return raw_thread_pool_submit (handle_aiocb_ioctl, &acb); }#endif /* linux */ block/file-win32.c +1
-1@@ -167,7 +167,7 @@ static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, acb->aio_offset = offset;trace_file_paio_submit (acb, opaque, offset, count, type); - pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); + pool =aio_get_thread_pool (qemu_get_current_aio_context ());return thread_pool_submit_aio (pool, aio_worker, acb, cb, opaque); }block/qcow2-threads.c +1
-1@@ -42,7 +42,7 @@ qcow2_co_process(BlockDriverState *bs, ThreadPoolFunc *func, void *arg) {int ret;BDRVQcow2State *s = bs->opaque; -ThreadPool *pool =aio_get_thread_pool (bdrv_get_aio_context (bs)); +ThreadPool *pool =aio_get_thread_pool (qemu_get_current_aio_context ());qemu_co_mutex_lock (&s->lock);while (s->nb_threads >=QCOW2_MAX_THREADS ) {util/thread-pool.c +1
-1@@ -48,7 +48,7 @@ struct ThreadPoolElement {/* Access to this list is protected by lock. */ QTAILQ_ENTRY (ThreadPoolElement) reqs; -/* Access to this list is protected by the global mutex. */ +/* This list is only written by the thread pool's mother thread. */ QLIST_ENTRY (ThreadPoolElement) all; };
util/thread-pool.c BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, · line 259
Quoted 9 lines
util/thread-pool.c +3
-0@@ -251,6 +251,9 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, {ThreadPoolElement *req; +/* Assert that the thread submitting work is the same running the pool */ +assert (pool->ctx ==qemu_get_current_aio_context ()); + req =qemu_aio_get (&thread_pool_aiocb_info,NULL , cb, opaque); req->func = func; req->arg = arg;
Paolo