nbd: actually make s->state thread-safe
cover nbd: actually make s->state thread-safe
The main point of this series is patch 6, which removes the dubious and probably wrong use of atomics in block/nbd.c. This in turn is enabled mostly by the cleanups in patches 3-5. Together, they introduce a QemuMutex that synchronizes the NBD client coroutines, the reconnect_delay timer and nbd_cancel_in_flight() as well.
The fixes happen to remove an incorrect use of qemu_co_queue_restart_all and qemu_co_enter_next on the s->free_sema CoQueue, which was not guarded by s->send_mutex.
The rest is bugfixes, simplifying the code a bit, and extra documentation.
Paolo Bonzini (8):
nbd: actually implement reply_possible safeguard
nbd: mark more coroutine_fns
nbd: remove peppering of nbd_client_connected
nbd: keep send_mutex/free_sema handling outside
nbd_co_do_establish_connection
nbd: use a QemuMutex to synchronize reconnection with coroutines
nbd: move s->state under requests_lock
nbd: take receive_mutex when reading requests[].receiving
nbd: document what is protected by the CoMutexes
block/coroutines.h | 4 +- block/nbd.c | 295 +++++++++++++++++++++++---------------------- 2 files changed, 154 insertions(+), 145 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/8 nbd: actually implement reply_possible safeguard
The .reply_possible field of s->requests is never set to false. This is not a big problem as it is only a safeguard to detect protocol errors, but fix it anyway.
--- block/nbd.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/nbd.c +5 -4
@@ -454,15 +454,16 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle)nbd_channel_error (s, -EINVAL );return -EINVAL ; } -if (s->reply.handle == handle) { -/* We are done */ -return 0 ; - } ind2 =HANDLE_TO_INDEX (s, s->reply.handle);if (ind2 >=MAX_NBD_REQUESTS || !s->requests[ind2].reply_possible) {nbd_channel_error (s, -EINVAL );return -EINVAL ; } + s->requests[ind2].reply_possible =nbd_reply_is_structured (&s->reply); +if (s->reply.handle == handle) { +/* We are done */ +return 0 ; + }nbd_recv_coroutine_wake_one (&s->requests[ind2]); } }
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 1/8] nbd: actually implement reply_possible safeguard
Quoted 17 lines · Paolo Bonzini
The .reply_possible field of
s->requestsis never set to false. This is not a big problem as it is only a safeguard to detect protocol errors, but fix it anyway.Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)block/nbd.c +0
-0@@ -454,15 +454,16 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle)nbd_channel_error (s, -EINVAL );return -EINVAL ; }
block/nbd.c static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle) · line 462
Quoted 10 lines
block/nbd.c +1
-4-if (s->reply.handle == handle) { -/* We are done */ -return 0 ; - } ind2 =HANDLE_TO_INDEX (s, s->reply.handle);if (ind2 >=MAX_NBD_REQUESTS || !s->requests[ind2].reply_possible) {nbd_channel_error (s, -EINVAL );return -EINVAL ; } + s->requests[ind2].reply_possible =nbd_reply_is_structured (&s->reply);
If the reply is simple (not structured), then we expect no further replies, so this sets things to false. But if the reply is structured, the answer depends on NBD_REPLY_FLAG_DONE, as in:
s->requests[ind2].reply_possible =
nbd_reply_is_structured(&s->reply) && (s->reply.structured.flags & NBD_REPLY_FLAG_DONE);
Quoted 7 lines
block/nbd.c +4
-0+if (s->reply.handle == handle) { +/* We are done */ +return 0 ; + }nbd_recv_coroutine_wake_one (&s->requests[ind2]); } }
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH for-7.1 1/8] nbd: actually implement reply_possible safeguard
Quoted 9 lines · Eric Blake
Quoted 1 line
+ s->requests[ind2].reply_possible =
nbd_reply_is_structured(&s->reply);If the reply is simple (not structured), then we expect no further replies, so this sets things to false. But if the reply is structured, the answer depends on
NBD_REPLY_FLAG_DONE, as in:s->requests[ind2].reply_possible =
nbd_reply_is_structured(&s->reply)&& (s->reply.structured.flags&NBD_REPLY_FLAG_DONE);
Thinking more about it the field is unnecessary, we can just check .coroutine in the same fashion.
Paolo
PATCH 2/8 nbd: mark more coroutine_fns
In reply to Paolo BonziniSeveral coroutine functions in block/nbd.c are not marked as such. This patch adds a few more markers; it is not exhaustive, but it focuses especially on:
- places that wake other coroutines, because aio_co_wake() has very different semantics inside a coroutine (queuing after yield vs. entering immediately);
- functions with _co_ in their names, to avoid confusion
--- block/nbd.c | 64 ++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 32 deletions(-)
block/nbd.c +32 -32
@@ -133,7 +133,7 @@ static bool nbd_client_connected(BDRVNBDState *s)return qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTED ; } -static bool nbd_recv_coroutine_wake_one (NBDClientRequest *req) +static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req) {if (req->receiving) { req->receiving = false;
@@ -144,7 +144,7 @@ static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req)return false; } -static void nbd_recv_coroutines_wake (BDRVNBDState *s,bool all) +static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) {int i;
@@ -155,7 +155,7 @@ static void nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) } } -static void nbd_channel_error (BDRVNBDState *s,int ret) +static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret) {if (nbd_client_connected (s)) {qio_channel_shutdown (s->ioc,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL );
@@ -468,9 +468,9 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle) } } -static int nbd_co_send_request (BlockDriverState *bs, -NBDRequest *request, -QEMUIOVector *qiov) +static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, +NBDRequest *request, +QEMUIOVector *qiov) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;int rc, i =-1 ;
@@ -724,9 +724,9 @@ static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,return 0 ; } -static int nbd_co_receive_offset_data_payload (BDRVNBDState *s, -uint64_t orig_offset, -QEMUIOVector *qiov,Error **errp) +static int coroutine_fn nbd_co_receive_offset_data_payload(BDRVNBDState *s, +uint64_t orig_offset, +QEMUIOVector *qiov,Error **errp) {QEMUIOVector sub_qiov;uint64_t offset;
@@ -1042,8 +1042,8 @@ break_loop:return false; } -static int nbd_co_receive_return_code (BDRVNBDState *s,uint64_t handle, -int *request_ret,Error **errp) +static int coroutine_fn nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle, +int *request_ret,Error **errp) {NBDReplyChunkIter iter;
@@ -1056,9 +1056,9 @@ static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,return iter.ret; } -static int nbd_co_receive_cmdread_reply (BDRVNBDState *s,uint64_t handle, -uint64_t offset,QEMUIOVector *qiov, -int *request_ret,Error **errp) +static int coroutine_fn nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, +uint64_t offset,QEMUIOVector *qiov, +int *request_ret,Error **errp) {NBDReplyChunkIter iter;NBDReply reply;
@@ -1108,10 +1108,10 @@ static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,return iter.ret; } -static int nbd_co_receive_blockstatus_reply (BDRVNBDState *s, -uint64_t handle,uint64_t length, -NBDExtent *extent, -int *request_ret,Error **errp) +static int coroutine_fn nbd_co_receive_blockstatus_reply(BDRVNBDState *s, +uint64_t handle,uint64_t length, +NBDExtent *extent, +int *request_ret,Error **errp) {NBDReplyChunkIter iter;NBDReply reply;
@@ -1168,8 +1168,8 @@ static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,return iter.ret; } -static int nbd_co_request (BlockDriverState *bs,NBDRequest *request, -QEMUIOVector *write_qiov) +static int coroutine_fn nbd_co_request(BlockDriverState *bs, NBDRequest *request, +QEMUIOVector *write_qiov) {int ret, request_ret;Error *local_err =NULL ;
@@ -1205,9 +1205,9 @@ static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,return ret ? ret : request_ret; } -static int nbd_client_co_preadv (BlockDriverState *bs,int64_t offset, -int64_t bytes,QEMUIOVector *qiov, -BdrvRequestFlags flags) +static int coroutine_fn nbd_client_co_preadv(BlockDriverState *bs, int64_t offset, +int64_t bytes,QEMUIOVector *qiov, +BdrvRequestFlags flags) {int ret, request_ret;Error *local_err =NULL ;
@@ -1264,9 +1264,9 @@ static int nbd_client_co_preadv(BlockDriverState *bs, int64_t offset,return ret ? ret : request_ret; } -static int nbd_client_co_pwritev (BlockDriverState *bs,int64_t offset, -int64_t bytes,QEMUIOVector *qiov, -BdrvRequestFlags flags) +static int coroutine_fn nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset, +int64_t bytes,QEMUIOVector *qiov, +BdrvRequestFlags flags) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;NBDRequest request = {
@@ -1289,8 +1289,8 @@ static int nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset,return nbd_co_request (bs, &request, qiov); } -static int nbd_client_co_pwrite_zeroes (BlockDriverState *bs,int64_t offset, -int64_t bytes,BdrvRequestFlags flags) +static int coroutine_fn nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, +int64_t bytes,BdrvRequestFlags flags) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;NBDRequest request = {
@@ -1324,7 +1324,7 @@ static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,return nbd_co_request (bs, &request,NULL ); } -static int nbd_client_co_flush (BlockDriverState *bs) +static int coroutine_fn nbd_client_co_flush(BlockDriverState *bs) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;NBDRequest request = { .type =NBD_CMD_FLUSH };
@@ -1339,8 +1339,8 @@ static int nbd_client_co_flush(BlockDriverState *bs)return nbd_co_request (bs, &request,NULL ); } -static int nbd_client_co_pdiscard (BlockDriverState *bs,int64_t offset, -int64_t bytes) +static int coroutine_fn nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, +int64_t bytes) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;NBDRequest request = {
@@ -1916,7 +1916,7 @@ fail:return ret; } -static int nbd_co_flush (BlockDriverState *bs) +static int coroutine_fn nbd_co_flush(BlockDriverState *bs) {return nbd_client_co_flush (bs); }
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 2/8] nbd: mark more coroutine_fns
Quoted 9 lines · Paolo Bonzini
Several coroutine functions in
block/nbd.care not marked as such. This patch adds a few more markers; it is not exhaustive, but it focuses especially on:- places that wake other coroutines, because
aio_co_wake()has very different semantics inside a coroutine (queuing after yield vs. entering immediately);- functions with _co_ in their names, to avoid confusion
Should we add _co_ in the names of the three other functions thus marked? As in:
block/nbd.c static bool nbd_client_connected(BDRVNBDState *s) · line 136
Quoted 12 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 64 ++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 32 deletions(-)block/nbd.c +1
-1@@ -133,7 +133,7 @@ static bool nbd_client_connected(BDRVNBDState *s)return qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTED ; } -static bool nbd_recv_coroutine_wake_one (NBDClientRequest *req) +static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req)
This already has _coroutine_ in the name, would it be better as _co_?
block/nbd.c static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req) · line 147
Quoted 9 lines
block/nbd.c +1
-1{if (req->receiving) { req->receiving = false;@@ -144,7 +144,7 @@ static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req)return false; } -static void nbd_recv_coroutines_wake (BDRVNBDState *s,bool all) +static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all)
This already has _coroutines_ in the name, would it be better as _co_?
block/nbd.c static void nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) · line 158
Quoted 9 lines
block/nbd.c +1
-1{int i;@@ -155,7 +155,7 @@ static void nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) } } -static void nbd_channel_error (BDRVNBDState *s,int ret) +static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret)
This has no mention of coroutines, but does call nbd_recv_coroutines_wake. Should we add _co_ in the name?
But as written, your change makes sense to me for adding the label to all functions in this patch.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH for-7.1 2/8] nbd: mark more coroutine_fns
Quoted 11 lines · Eric Blake
block/nbd.c static bool nbd_client_connected(BDRVNBDState *s) · line 136
Quoted 2 lines
block/nbd.c +1
-1-static bool nbd_recv_coroutine_wake_one (NBDClientRequest *req) +static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req)This already
has_coroutine_in the name, would it be betteras_co_?Quoted 7 lines
{ if (req->receiving) { req->receiving = false;block/nbd.c +0
-0@@ -144,7 +144,7 @@ static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req)return false; }
Quoted 3 lines
block/nbd.c static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req) · line 147
Quoted 2 lines
block/nbd.c +1
-1-static void nbd_recv_coroutines_wake (BDRVNBDState *s,bool all) +static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all)This already
has_coroutines_in the name, would it be betteras_co_?
These mean "wake a coroutine", not "I'm in a coroutine", so I'd say they are fine as is.
Paolo
PATCH 3/8 nbd: remove peppering of nbd_client_connected
In reply to Paolo BonziniIt is unnecessary to check nbd_client_connected() because every time s->state is moved out of NBD_CLIENT_CONNECTED the socket is shut down and all coroutines are resumed.
The only case where it was actually needed is when the NBD server disconnects and there is no reconnect-delay. In that case, nbd_receive_replies() does not set s->reply.handle and nbd_co_do_receive_one_chunk() cannot continue. For that one case, check the return value of nbd_receive_replies().
As to the others:
* nbd_receive_replies() can put the current coroutine to sleep if another reply is ongoing; then it will be woken by nbd_channel_error() called by the ongoing reply. Or it can try itself to read a reply header and fail, thus calling nbd_channel_error() itself.
* nbd_co_send_request() will write the body of the request and fail
* nbd_reply_chunk_iter_receive() will call nbd_co_receive_one_chunk() and then nbd_co_do_receive_one_chunk(), which will handle the failure as above; or it will just detect a previous call to nbd_iter_channel_error() via iter->ret < 0.
--- block/nbd.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
block/nbd.c +4 -13
@@ -410,10 +410,6 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle)return 0 ; } -if (!nbd_client_connected (s)) { -return -EIO ; - } -if (s->reply.handle !=0 ) {/* * Some other request is being handled now. It should already be
@@ -515,7 +511,7 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,if (qiov) {qio_channel_set_cork (s->ioc, true); rc =nbd_send_request (s->ioc, request); -if (nbd_client_connected (s) && rc >=0 ) { +if (rc >=0 ) {if (qio_channel_writev_all (s->ioc, qiov->iov, qiov->niov,NULL ) <0 ) { rc = -EIO ;
@@ -832,8 +828,8 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( } *request_ret =0 ; -nbd_receive_replies (s, handle); -if (!nbd_client_connected (s)) { + ret =nbd_receive_replies (s, handle); +if (ret <0 ) {error_setg (errp,"Connection closed" );return -EIO ; }
@@ -985,11 +981,6 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,NBDReply local_reply;NBDStructuredReplyChunk *chunk;Error *local_err =NULL ; -if (!nbd_client_connected (s)) { -error_setg (&local_err,"Connection closed" ); -nbd_iter_channel_error (iter, -EIO , &local_err); - goto break_loop; - }if (iter->done) {/* Previous iteration was last. */
@@ -1010,7 +1001,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, }/* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ -if (nbd_reply_is_simple (reply) || !nbd_client_connected (s)) { +if (nbd_reply_is_simple (reply) || iter->ret <0 ) { goto break_loop; }
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 3/8] nbd: remove peppering of nbd_client_connected
Quoted 24 lines · Paolo Bonzini
It is unnecessary to check
nbd_client_connected()because every times->stateis moved out ofNBD_CLIENT_CONNECTEDthe socket is shut down and all coroutines are resumed.The only case where it was actually needed is when the NBD server disconnects and there is no reconnect-delay. In that case,
nbd_receive_replies()does not sets->reply.handleandnbd_co_do_receive_one_chunk()cannot continue. For that one case, check the return value ofnbd_receive_replies().As to the others:
*
nbd_receive_replies()can put the current coroutine to sleep if another reply is ongoing; then it will be woken bynbd_channel_error()called by the ongoing reply. Or it can try itself to read a reply header and fail, thus callingnbd_channel_error()itself.*
nbd_co_send_request()will write the body of the request and fail*
nbd_reply_chunk_iter_receive()will callnbd_co_receive_one_chunk()and thennbd_co_do_receive_one_chunk(), which will handle the failure as above; or it will just detect a previous call tonbd_iter_channel_error()viaiter->ret< 0.
Quoted 4 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 4/8 nbd: keep send_mutex/free_sema handling outside nbd_co_do_establish_connection
In reply to Paolo BonziniElevate s->in_flight early so that other incoming requests will wait on the CoQueue in nbd_co_send_request; restart them after getting back from nbd_reconnect_attempt. This could be after the reconnect timer or nbd_cancel_in_flight have cancelled the attempt, so there is no need anymore to cancel the requests there.
nbd_co_send_request now handles both stopping and restarting pending requests after a successful connection, and there is no need to hold send_mutex in nbd_co_do_establish_connection. The current setup is confusing because nbd_co_do_establish_connection is called both with send_mutex taken and without it. Before the patch it uses free_sema which (at least in theory...) is protected by send_mutex, after the patch it does not anymore.
--- block/coroutines.h | 4 +-- block/nbd.c | 66 ++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 37 deletions(-)
block/coroutines.h +2 -2
@@ -59,7 +59,7 @@ int coroutine_fn bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); int coroutine_fn -nbd_co_do_establish_connection (BlockDriverState *bs, Error **errp); +nbd_co_do_establish_connection (BlockDriverState *bs,bool blocking,Error **errp);int coroutine_fn
@@ -109,7 +109,7 @@ bdrv_common_block_status_above(BlockDriverState *bs, BlockDriverState **file, int *depth); int generated_co_wrapper -nbd_do_establish_connection (BlockDriverState *bs, Error **errp); +nbd_do_establish_connection (BlockDriverState *bs,bool blocking,Error **errp); int generated_co_wrapper blk_do_preadv(BlockBackend *blk, int64_t offset, int64_t bytes,
block/nbd.c +31 -35
133 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 4/8] nbd: keep send_mutex/free_sema handling outside nbd_co_do_establish_connection
Quoted 43 lines · Paolo Bonzini
Elevate
s->in_flightearly so that other incoming requests will wait on the CoQueue innbd_co_send_request; restart them after getting back fromnbd_reconnect_attempt. This could be after the reconnect timer ornbd_cancel_in_flighthave cancelled the attempt, so there is no need anymore to cancel the requests there.
nbd_co_send_requestnow handles both stopping and restarting pending requests after a successful connection, and there is no need to holdsend_mutexinnbd_co_do_establish_connection. The current setup is confusing becausenbd_co_do_establish_connectionis called both withsend_mutextaken and without it. Before the patch it usesfree_semawhich (at least in theory...) is protected bysend_mutex, after the patch it does not anymore.Signed-off-by Paolo Bonzini <[email protected]>--- block/coroutines.h | 4 +-- block/nbd.c | 66 ++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 37 deletions(-)+++
b/block/nbd.cblock/nbd.c +1
-14@@ -359,25 +354,25 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,/* called under s->send_mutex */ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { -assert (nbd_client_connecting (s)); -assert (s->in_flight ==0 ); - -if (nbd_client_connecting_wait (s) && s->reconnect_delay && - !s->reconnect_delay_timer) - { -/* -* It's first reconnect attempt after switching to -* NBD_CLIENT_CONNECTING_WAIT -*/ -reconnect_delay_timer_init (s, -qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) + - s->reconnect_delay *NANOSECONDS_PER_SECOND ); - } +bool blocking =nbd_client_connecting_wait (s);
block/nbd.c int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, · line 368
Quoted 10 lines
block/nbd.c +6
-0/* * Now we are sure that nobody is accessing the channel, and no one will * try until we set the state to CONNECTED. */ +assert (nbd_client_connecting (s)); +assert (s->in_flight ==1 ); + +if (blocking && !s->reconnect_delay_timer) { +/* +* It's first reconnect attempt after switching to
While moving this, we could add the missing article: "It's the first"
Quoted 22 lines
block/nbd.c +10
-1+* NBD_CLIENT_CONNECTING_WAIT +*/ +g_assert (s->reconnect_delay); +reconnect_delay_timer_init (s, +qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) + + s->reconnect_delay *NANOSECONDS_PER_SECOND ); + }/* Finalize previous connection if any */ if (s->ioc) {@@ -388,7 +383,9 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) s->ioc =NULL ; } -nbd_co_do_establish_connection (s->bs,NULL ); +qemu_co_mutex_unlock (&s->send_mutex); +nbd_co_do_establish_connection (s->bs, blocking,NULL ); +qemu_co_mutex_lock (&s->send_mutex);/* * The reconnect attempt is done (maybe successfully, maybe not), so
block/nbd.c static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, · line 474
Quoted 6 lines
block/nbd.c +1
-2@@ -474,21 +471,21 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,qemu_co_mutex_lock (&s->send_mutex);while (s->in_flight ==MAX_NBD_REQUESTS || - (!nbd_client_connected (s) && s->in_flight >0 )) - { + (!nbd_client_connected (s) && s->in_flight >0 )) {
Mixing in a style change here. Not the end of the world.
The cosmetics are trivial, and the real change of enlarging the scope of in_flight makes sense to me.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 5/8 nbd: use a QemuMutex to synchronize reconnection with coroutines
In reply to Paolo BonziniThe condition for waiting on the s->free_sema queue depends on both s->in_flight and s->state. The latter is currently using atomics, but this is quite dubious and probably wrong.
Because s->state is written in the main thread too, for example by the reconnect timer callback, it cannot be protected by a CoMutex. Introduce a separate lock that can be used by nbd_co_send_request(); later on this lock will also be used for s->state. There will not be any contention on the lock unless there is a reconnect, so this is not performance sensitive.
--- block/nbd.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-)
block/nbd.c +27 -19
@@ -72,17 +72,22 @@ typedef struct BDRVNBDState {QIOChannel *ioc;/* The current I/O channel */ NBDExportInfo info; -CoMutex send_mutex; +/* +* Protects free_sema, in_flight, requests[].coroutine, +* reconnect_delay_timer. +*/ +QemuMutex requests_lock;CoQueue free_sema; - -CoMutex receive_mutex;int in_flight; +NBDClientRequest requests[MAX_NBD_REQUESTS ]; +QEMUTimer *reconnect_delay_timer; + +CoMutex send_mutex; +CoMutex receive_mutex;NBDClientState state; -QEMUTimer *reconnect_delay_timer;QEMUTimer *open_timer; -NBDClientRequest requests[MAX_NBD_REQUESTS ];NBDReply reply;BlockDriverState *bs;
@@ -351,7 +356,7 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,return 0 ; } -/* called under s->send_mutex */ +/* Called with s->requests_lock taken. */ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) {bool blocking =nbd_client_connecting_wait (s);
@@ -383,9 +388,9 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) s->ioc =NULL ; } -qemu_co_mutex_unlock (&s->send_mutex); +qemu_mutex_unlock (&s->requests_lock);nbd_co_do_establish_connection (s->bs, blocking,NULL ); -qemu_co_mutex_lock (&s->send_mutex); +qemu_mutex_lock (&s->requests_lock);/* * The reconnect attempt is done (maybe successfully, maybe not), so
@@ -468,11 +473,10 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,BDRVNBDState *s = (BDRVNBDState *)bs->opaque;int rc, i =-1 ; -qemu_co_mutex_lock (&s->send_mutex); - +qemu_mutex_lock (&s->requests_lock);while (s->in_flight ==MAX_NBD_REQUESTS || (!nbd_client_connected (s) && s->in_flight >0 )) { -qemu_co_queue_wait (&s->free_sema, &s->send_mutex); +qemu_co_queue_wait (&s->free_sema, &s->requests_lock); } s->in_flight++;
@@ -493,14 +497,14 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, } } -g_assert (qemu_in_coroutine ());assert (i <MAX_NBD_REQUESTS ); - s->requests[i].coroutine =qemu_coroutine_self (); s->requests[i].offset = request->from; s->requests[i].receiving = false; s->requests[i].reply_possible = true; +qemu_mutex_unlock (&s->requests_lock); +qemu_co_mutex_lock (&s->send_mutex); request->handle =INDEX_TO_HANDLE (s, i);assert (s->ioc);
@@ -520,17 +524,19 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, }else { rc =nbd_send_request (s->ioc, request); } +qemu_co_mutex_unlock (&s->send_mutex); -err:if (rc <0 ) { +qemu_mutex_lock (&s->requests_lock); +err:nbd_channel_error (s, rc);if (i !=-1 ) { s->requests[i].coroutine =NULL ; } s->in_flight--;qemu_co_queue_next (&s->free_sema); +qemu_mutex_unlock (&s->requests_lock); } -qemu_co_mutex_unlock (&s->send_mutex);return rc; }
@@ -1020,12 +1026,11 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,return true; break_loop: +qemu_mutex_lock (&s->requests_lock); s->requests[HANDLE_TO_INDEX (s, handle)].coroutine =NULL ; - -qemu_co_mutex_lock (&s->send_mutex); s->in_flight--;qemu_co_queue_next (&s->free_sema); -qemu_co_mutex_unlock (&s->send_mutex); +qemu_mutex_unlock (&s->requests_lock);return false; }
@@ -1858,8 +1863,9 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,BDRVNBDState *s = (BDRVNBDState *)bs->opaque; s->bs = bs; -qemu_co_mutex_init (&s->send_mutex); +qemu_mutex_init (&s->requests_lock);qemu_co_queue_init (&s->free_sema); +qemu_co_mutex_init (&s->send_mutex);qemu_co_mutex_init (&s->receive_mutex);if (!yank_register_instance (BLOCKDEV_YANK_INSTANCE (bs->node_name), errp)) {
@@ -2046,9 +2052,11 @@ static void nbd_cancel_in_flight(BlockDriverState *bs)reconnect_delay_timer_del (s); +qemu_mutex_lock (&s->requests_lock);if (s->state ==NBD_CLIENT_CONNECTING_WAIT ) { s->state =NBD_CLIENT_CONNECTING_NOWAIT ; } +qemu_mutex_unlock (&s->requests_lock);nbd_co_establish_connection_cancel (s->conn); }
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 5/8] nbd: use a QemuMutex to synchronize reconnection with coroutines
Quoted 2 lines · Paolo Bonzini
Because
s->stateis written in the main thread too, for example by the reconnect timer callback, it cannot be protected by a CoMutex.
Actually by the yank callback, not the timer (which runs in the "right" AioContext).
Paolo
Re: [PATCH for-7.1 5/8] nbd: use a QemuMutex to synchronize reconnection with coroutines
In reply to Paolo BonziniQuoted 35 lines · Paolo Bonzini
The condition for waiting on the
s->free_semaqueue depends on boths->in_flightands->state. The latter is currently using atomics, but this is quite dubious and probably wrong.Because
s->stateis written in the main thread too, for example by the reconnect timer callback, it cannot be protected by a CoMutex. Introduce a separate lock that can be used bynbd_co_send_request(); later on this lock will also be used fors->state. There will not be any contention on the lock unless there is a reconnect, so this is not performance sensitive.Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-)block/nbd.c +6
-3@@ -72,17 +72,22 @@ typedef struct BDRVNBDState {QIOChannel *ioc;/* The current I/O channel */ NBDExportInfo info; -CoMutex send_mutex; +/* +* Protects free_sema, in_flight, requests[].coroutine, +* reconnect_delay_timer. +*/ +QemuMutex requests_lock;CoQueue free_sema; - -CoMutex receive_mutex;int in_flight; +NBDClientRequest requests[MAX_NBD_REQUESTS ];
block/nbd.c typedef struct BDRVNBDState { · line 85
Quoted 10 lines
block/nbd.c +4
-2+QEMUTimer *reconnect_delay_timer; + +CoMutex send_mutex; +CoMutex receive_mutex;NBDClientState state; -QEMUTimer *reconnect_delay_timer;QEMUTimer *open_timer; -NBDClientRequest requests[MAX_NBD_REQUESTS ];
Reordering of the struct makes sense.
Quoted 15 lines
block/nbd.c +2
-3@@ -468,11 +473,10 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,BDRVNBDState *s = (BDRVNBDState *)bs->opaque;int rc, i =-1 ; -qemu_co_mutex_lock (&s->send_mutex); - +qemu_mutex_lock (&s->requests_lock);while (s->in_flight ==MAX_NBD_REQUESTS || (!nbd_client_connected (s) && s->in_flight >0 )) { -qemu_co_queue_wait (&s->free_sema, &s->send_mutex); +qemu_co_queue_wait (&s->free_sema, &s->requests_lock); } s->in_flight++;
block/nbd.c static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, · line 496
Quoted 4 lines
block/nbd.c +0
-1@@ -493,14 +497,14 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, } } -g_assert (qemu_in_coroutine ());
Why is this assert dropped? Is it because we've marked the function with coroutine_fn? If so, should we drop it earlier in the series, when you added the label?
Otherwise, the patch makes sense to me.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH for-7.1 5/8] nbd: use a QemuMutex to synchronize reconnection with coroutines
Quoted 4 lines · Eric Blake
Quoted 1 line
- g_assert(qemu_in_coroutine());
Why is this assert dropped? Is it because we've marked the function with
coroutine_fn? If so, should we drop it earlier in the series, when you added the label?
The label doesn't guarantee much, but in this case it's pretty clear-cut that we must be in a coroutine, the code just doesn't make sense otherwise.
We're also about to take a CoMutex (and before the patch we had already taken it) so if anywhere the place for the assertion would be qemu_co_mutex_lock().
Quoted 1 line
Otherwise, the patch makes sense to me.
PATCH 6/8 nbd: move s->state under requests_lock
In reply to Paolo BonziniRemove the confusing, and most likely wrong, atomics. The only function that used to be somewhat in a hot path was nbd_client_connected(), but it is not anymore after the previous patches.
The function nbd_client_connecting_wait() was used mostly to check if a request had to be reissued (outside requests_lock), but also under requests_lock in nbd_client_connecting_wait(). The two uses have to be separated; for the former we rename it to nbd_client_will_reconnect() and make it take s->requests_lock; for the latter the access can simply be inlined. The new name is clearer, and ensures that a missing conversion is caught by the compiler.
--- block/nbd.c | 88 +++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 40 deletions(-)
block/nbd.c +48 -40
@@ -35,7 +35,6 @@#include "qemu/option.h" #include "qemu/cutils.h" #include "qemu/main-loop.h" -#include "qemu/atomic.h" #include "qapi/qapi-visit-sockets.h" #include "qapi/qmp/qstring.h"
@@ -73,10 +72,11 @@ typedef struct BDRVNBDState {NBDExportInfo info;/* -* Protects free_sema, in_flight, requests[].coroutine, +* Protects state, free_sema, in_flight, requests[].coroutine, * reconnect_delay_timer. */ QemuMutex requests_lock; +NBDClientState state;CoQueue free_sema;int in_flight;NBDClientRequest requests[MAX_NBD_REQUESTS ];
@@ -84,7 +84,6 @@ typedef struct BDRVNBDState {CoMutex send_mutex;CoMutex receive_mutex; -NBDClientState state;QEMUTimer *open_timer;
@@ -133,11 +132,6 @@ static void nbd_clear_bdrvstate(BlockDriverState *bs) s->x_dirty_bitmap =NULL ; } -static bool nbd_client_connected (BDRVNBDState *s) -{ -return qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTED ; -} - static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req) {if (req->receiving) {
@@ -160,14 +154,15 @@ static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) } } -static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret) +/* Called with s->requests_lock held. */ +static void coroutine_fn nbd_channel_error_locked(BDRVNBDState *s, int ret) { -if (nbd_client_connected (s)) { +if (s->state ==NBD_CLIENT_CONNECTED ) {qio_channel_shutdown (s->ioc,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); }if (ret == -EIO ) { -if (nbd_client_connected (s)) { +if (s->state ==NBD_CLIENT_CONNECTED ) { s->state = s->reconnect_delay ?NBD_CLIENT_CONNECTING_WAIT :NBD_CLIENT_CONNECTING_NOWAIT ; }
@@ -178,6 +173,12 @@ static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret)nbd_recv_coroutines_wake (s, true); } +static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret) +{ +QEMU_LOCK_GUARD (&s->requests_lock); +nbd_channel_error_locked (s, ret); +} +static void reconnect_delay_timer_del (BDRVNBDState *s) {if (s->reconnect_delay_timer) {
@@ -190,20 +191,18 @@ static void reconnect_delay_timer_cb(void *opaque) {BDRVNBDState *s = opaque; -if (qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTING_WAIT ) { - s->state =NBD_CLIENT_CONNECTING_NOWAIT ; -nbd_co_establish_connection_cancel (s->conn); - } -reconnect_delay_timer_del (s); + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { +if (s->state !=NBD_CLIENT_CONNECTING_WAIT ) { +return ; + } + s->state =NBD_CLIENT_CONNECTING_NOWAIT ; + } +nbd_co_establish_connection_cancel (s->conn); }static void reconnect_delay_timer_init (BDRVNBDState *s,uint64_t expire_time_ns) { -if (qatomic_load_acquire (&s->state) !=NBD_CLIENT_CONNECTING_WAIT ) { -return ; - } -assert (!s->reconnect_delay_timer); s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME,
@@ -226,7 +225,9 @@ static void nbd_teardown_connection(BlockDriverState *bs) s->ioc =NULL ; } - s->state =NBD_CLIENT_QUIT ; + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + s->state =NBD_CLIENT_QUIT ; + } }static void open_timer_del (BDRVNBDState *s)
@@ -255,16 +256,13 @@ static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)timer_mod (s->open_timer, expire_time_ns); } -static bool nbd_client_connecting (BDRVNBDState *s) +static bool nbd_client_will_reconnect (BDRVNBDState *s) { -NBDClientState state =qatomic_load_acquire (&s->state); -return state ==NBD_CLIENT_CONNECTING_WAIT || - state ==NBD_CLIENT_CONNECTING_NOWAIT ; -} - -static bool nbd_client_connecting_wait (BDRVNBDState *s) -{ -return qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTING_WAIT ; +/* +* Called only after a socket error, so this is not performance sensitive. +*/ +QEMU_LOCK_GUARD (&s->requests_lock); +return s->state ==NBD_CLIENT_CONNECTING_WAIT ; }/*
@@ -351,15 +349,24 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,qio_channel_attach_aio_context (s->ioc,bdrv_get_aio_context (bs));/* successfully connected */ - s->state =NBD_CLIENT_CONNECTED ; + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + s->state =NBD_CLIENT_CONNECTED ; + }return 0 ; } +/* Called with s->requests_lock held. */ +static bool nbd_client_connecting (BDRVNBDState *s) +{ +return s->state ==NBD_CLIENT_CONNECTING_WAIT || + s->state ==NBD_CLIENT_CONNECTING_NOWAIT ; +} +/* Called with s->requests_lock taken. */ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { -bool blocking =nbd_client_connecting_wait (s); +bool blocking = s->state ==NBD_CLIENT_CONNECTING_WAIT ;/* * Now we are sure that nobody is accessing the channel, and no one will
@@ -475,17 +482,17 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,qemu_mutex_lock (&s->requests_lock);while (s->in_flight ==MAX_NBD_REQUESTS || - (!nbd_client_connected (s) && s->in_flight >0 )) { + (s->state !=NBD_CLIENT_CONNECTED && s->in_flight >0 )) {qemu_co_queue_wait (&s->free_sema, &s->requests_lock); } s->in_flight++; -if (!nbd_client_connected (s)) { +if (s->state !=NBD_CLIENT_CONNECTED ) {if (nbd_client_connecting (s)) {nbd_reconnect_attempt (s);qemu_co_queue_restart_all (&s->free_sema); } -if (!nbd_client_connected (s)) { +if (s->state !=NBD_CLIENT_CONNECTED ) { rc = -EIO ; goto err; }
@@ -529,7 +536,7 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,if (rc <0 ) {qemu_mutex_lock (&s->requests_lock); err: -nbd_channel_error (s, rc); +nbd_channel_error_locked (s, rc);if (i !=-1 ) { s->requests[i].coroutine =NULL ; }
@@ -1193,7 +1200,7 @@ static int coroutine_fn nbd_co_request(BlockDriverState *bs, NBDRequest *requesterror_free (local_err); local_err =NULL ; } - }while (ret <0 &&nbd_client_connecting_wait (s)); + }while (ret <0 &&nbd_client_will_reconnect (s));return ret ? ret : request_ret; }
@@ -1252,7 +1259,7 @@ static int coroutine_fn nbd_client_co_preadv(BlockDriverState *bs, int64_t offseerror_free (local_err); local_err =NULL ; } - }while (ret <0 &&nbd_client_connecting_wait (s)); + }while (ret <0 &&nbd_client_will_reconnect (s));return ret ? ret : request_ret; }
@@ -1410,7 +1417,7 @@ static int coroutine_fn nbd_client_co_block_status(error_free (local_err); local_err =NULL ; } - }while (ret <0 &&nbd_client_connecting_wait (s)); + }while (ret <0 &&nbd_client_will_reconnect (s));if (ret <0 || request_ret <0 ) {return ret ? ret : request_ret;
@@ -1442,8 +1449,9 @@ static void nbd_yank(void *opaque)BlockDriverState *bs = opaque;BDRVNBDState *s = (BDRVNBDState *)bs->opaque; -qatomic_store_release (&s->state,NBD_CLIENT_QUIT ); +QEMU_LOCK_GUARD (&s->requests_lock);qio_channel_shutdown (QIO_CHANNEL (s->ioc),QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); + s->state =NBD_CLIENT_QUIT ; }static void nbd_client_close (BlockDriverState *bs)
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 6/8] nbd: move s->state under requests_lock
Quoted 7 lines · Paolo Bonzini
Remove the confusing, and most likely wrong, atomics. The only function that used to be somewhat in a hot path was
nbd_client_connected(), but it is not anymore after the previous patches.The function
nbd_client_connecting_wait()was used mostly to check if a request had to be reissued (outsiderequests_lock), but also underrequests_lockinnbd_client_connecting_wait(). The two uses have to
"Function A was used mostly..., but also under requests_lock in function A." Reading the rest of the patch, I think...[1]
Quoted 4 lines
be separated; for the former we rename it to
nbd_client_will_reconnect()and make it takes->requests_lock; for the latter the access can simply be inlined. The new name is clearer, and ensures that a missing conversion is caught by the compiler.
I take it your experiments with C++ coroutines helped find this ;)
Quoted 7 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 88 +++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 40 deletions(-)
block/nbd.c static void nbd_teardown_connection(BlockDriverState *bs) · line 231
Quoted 8 lines
block/nbd.c +3
-1@@ -226,7 +225,9 @@ static void nbd_teardown_connection(BlockDriverState *bs) s->ioc =NULL ; } - s->state =NBD_CLIENT_QUIT ; + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + s->state =NBD_CLIENT_QUIT ; + } }
This style for protecting s->state at the end of the function takes 3 lines thanks to WITH_QEMU_LOCK_GUARD...[2]
block/nbd.c static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) · line 259
Quoted 8 lines
block/nbd.c +1
-1static void open_timer_del (BDRVNBDState *s)@@ -255,16 +256,13 @@ static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)timer_mod (s->open_timer, expire_time_ns); } -static bool nbd_client_connecting (BDRVNBDState *s) +static bool nbd_client_will_reconnect (BDRVNBDState *s)
This part of the diff got hard to read, since you mixed shuffling functions with a rename. On a closer read, I see that nbd_client_connecting() was merely moved later[3], while the new name nbd_client_will_reconnect()...[4]
block/nbd.c static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) · line 265
Quoted 7 lines
block/nbd.c +0
-6{ -NBDClientState state =qatomic_load_acquire (&s->state); -return state ==NBD_CLIENT_CONNECTING_WAIT || - state ==NBD_CLIENT_CONNECTING_NOWAIT ; -} - -static bool nbd_client_connecting_wait (BDRVNBDState *s)
[4]...is indeed happening to nbd_client_connecting_wait(), as promised in the commit message. Which means:
[1]...so it looks like the first 'function A' did indeed want to be nbd_client_connecting_wait() which got renamed (since nbd_client_connecting() was moved later in the file), while...[1]
block/nbd.c static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) · line 266
Quoted 8 lines
block/nbd.c +5
-2-{ -return qatomic_load_acquire (&s->state) ==NBD_CLIENT_CONNECTING_WAIT ; +/* +* Called only after a socket error, so this is not performance sensitive. +*/ +QEMU_LOCK_GUARD (&s->requests_lock); +return s->state ==NBD_CLIENT_CONNECTING_WAIT ; }
[2]...while here, you only needed two lines, using QEMU_LOCK_GUARD. Both styles work, but it seems like we should be consistent, and I would favor the shorter style when all that is being guarded is a single line.
block/nbd.c int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, · line 357
Quoted 13 lines
block/nbd.c +3
-1/* @@ -351,15 +349,24 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,qio_channel_attach_aio_context (s->ioc,bdrv_get_aio_context (bs));/* successfully connected */ - s->state =NBD_CLIENT_CONNECTED ; + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + s->state =NBD_CLIENT_CONNECTED ; + }return 0 ; }
Another place where the shorter QEMU_LOCK_GUARD() would work.
block/nbd.c int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, · line 360
Quoted 3 lines
block/nbd.c +2
-0+/* Called with s->requests_lock held. */ +static bool nbd_client_connecting (BDRVNBDState *s)
[3]...here's where the moved function threw me off. Perhaps splitting out a preliminary patch to just move the function before converting it to be under s->requests_lock, so that the rename of a different function doesn't cause a hard-to-grok diff, would be wise.
block/nbd.c int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, · line 369
Quoted 10 lines
block/nbd.c +6
-1+{ +return s->state ==NBD_CLIENT_CONNECTING_WAIT || + s->state ==NBD_CLIENT_CONNECTING_NOWAIT ; +} +/* Called with s->requests_lock taken. */ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { -bool blocking =nbd_client_connecting_wait (s); +bool blocking = s->state ==NBD_CLIENT_CONNECTING_WAIT ;
[1]...and the second instance of 'function A' in the commit message should have been nbd_reconnect_attempt().
As messy as the diff was, I still think I understood it. With the necessary correction to the commit message according to [1], I could be comfortable with
although the suggestion in [3] to split out the function motion to a separate patch may result in the v2 series looking different enough that you may want to leave off my R-b to ensure I still review things carefully.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH for-7.1 6/8] nbd: move s->state under requests_lock
Quoted 7 lines · Eric Blake
Quoted 4 lines
The function
nbd_client_connecting_wait()was used mostly to check if a request had to be reissued (outsiderequests_lock), but also underrequests_lockinnbd_client_connecting_wait(). The two uses have to"Function A was used mostly..., but also under
requests_lockin function A." Reading the rest of the patch, I think...[1]
Quoted 6 lines
Quoted 4 lines
be separated; for the former we rename it to
nbd_client_will_reconnect()and make it takes->requests_lock; for the latter the access can simply be inlined. The new name is clearer, and ensures that a missing conversion is caught by the compiler.I take it your experiments with C++ coroutines helped find this;)
No, they never went that far. :) Rather, these atomics have always bugged me, and after Emanuele pointed me to the enter_all without lock, I noticed that they can be fixed with the same hammer.
Quoted 9 lines
block/nbd.c static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) · line 266
Quoted 3 lines
block/nbd.c +2
-0+QEMU_LOCK_GUARD (&s->requests_lock); +return s->state ==NBD_CLIENT_CONNECTING_WAIT ; }[2]...while here, you only needed two lines, using
QEMU_LOCK_GUARD. Both styles work, but it seems like we should be consistent, and I would favor the shorter style when all that is being guarded is a single line.
QEMU_LOCK_GUARD() is a declaration in some sense (well, it is also a declaration when you expand the macro) and QEMU in general doesn't do declaration-after-statement.
Also, QEMU_LOCK_GUARD() emphasizes that the whole function is guarded, while WITH_QEMU_LOCK_GUARD() has the opposite effect on the reader.
Quoted 4 lines
although the suggestion in [3] to split out the function motion to a separate patch may result in the v2 series looking different enough that you may want to leave off my R-b to ensure I still review things carefully.
Will do.
Paolo
PATCH 7/8 nbd: take receive_mutex when reading requests[].receiving
In reply to Paolo Bonzinirequests[].receiving is set by nbd_receive_replies() under the receive_mutex; Read it under the same mutex as well. Waking up receivers on errors happens after each reply finishes processing, in nbd_co_receive_one_chunk(). If there is no currently-active reply, there are two cases:
* either there is no active request at all, in which case no element of request[] can have .receiving = true
* or nbd_receive_replies() must be running and waiting for receive_mutex; in that case it will get back to nbd_co_receive_one_chunk() because the socket has been shutdown, and all waiting coroutines will wake up in turn.
--- block/nbd.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
block/nbd.c +7 -8
@@ -132,6 +132,7 @@ static void nbd_clear_bdrvstate(BlockDriverState *bs) s->x_dirty_bitmap =NULL ; } +/* Called with s->receive_mutex taken. */ static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req) {if (req->receiving) {
@@ -143,12 +144,13 @@ static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req)return false; } -static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) +static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s) {int i; +QEMU_LOCK_GUARD (&s->receive_mutex);for (i =0 ; i <MAX_NBD_REQUESTS ; i++) { -if (nbd_recv_coroutine_wake_one (&s->requests[i]) && !all) { +if (nbd_recv_coroutine_wake_one (&s->requests[i])) {return ; } }
@@ -169,8 +171,6 @@ static void coroutine_fn nbd_channel_error_locked(BDRVNBDState *s, int ret) }else { s->state =NBD_CLIENT_QUIT ; } - -nbd_recv_coroutines_wake (s, true); } static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret)
@@ -433,11 +433,10 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle)qemu_coroutine_yield ();/* -* We may be woken for 3 reasons: +* We may be woken for 2 reasons: * 1. From this function, executing in parallel coroutine, when our * handle is received. -* 2. From nbd_channel_error(), when connection is lost. -* 3. From nbd_co_receive_one_chunk(), when previous request is +* 2. From nbd_co_receive_one_chunk(), when previous request is * finished and s->reply.handle set to 0. * Anyway, it's OK to lock the mutex and go to the next iteration. */
@@ -931,7 +930,7 @@ static coroutine_fn int nbd_co_receive_one_chunk( } s->reply.handle =0 ; -nbd_recv_coroutines_wake (s, false); +nbd_recv_coroutines_wake (s);return ret; }
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 7/8] nbd: take receive_mutex when reading requests[].receiving
Quoted 13 lines · Paolo Bonzini
requests[].receiving is set by
nbd_receive_replies()under thereceive_mutex; Read it under the same mutex as well. Waking up receivers on errors happens after each reply finishes processing, innbd_co_receive_one_chunk(). If there is no currently-active reply, there are two cases:* either there is no active request at all, in which case no element of request[] can have .receiving = true
* or
nbd_receive_replies()must be running and waiting forreceive_mutex; in that case it will get back tonbd_co_receive_one_chunk()because the socket has been shutdown, and all waiting coroutines will wake up in turn.
Quoted 5 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 8/8 nbd: document what is protected by the CoMutexes
In reply to Paolo Bonzini--- block/nbd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
block/nbd.c +7 -1
@@ -82,12 +82,18 @@ typedef struct BDRVNBDState {NBDClientRequest requests[MAX_NBD_REQUESTS ];QEMUTimer *reconnect_delay_timer; +/* Protects sending data on the socket. */ CoMutex send_mutex; + +/* +* Protects receiving reply headers from the socket, as well as the +* fields reply, requests[].receiving and requests[].reply_possible +*/ CoMutex receive_mutex; +NBDReply reply;QEMUTimer *open_timer; -NBDReply reply;BlockDriverState *bs;/* Connection parameters */
Signature · 1 line
2.35.1
Re: [PATCH for-7.1 8/8] nbd: document what is protected by the CoMutexes
Quoted 17 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini <[email protected]>--- block/nbd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)block/nbd.c +3
-0@@ -82,12 +82,18 @@ typedef struct BDRVNBDState {NBDClientRequest requests[MAX_NBD_REQUESTS ];QEMUTimer *reconnect_delay_timer; +/* Protects sending data on the socket. */ CoMutex send_mutex; + +/*
block/nbd.c typedef struct BDRVNBDState { · line 97
Quoted 10 lines
block/nbd.c +4
-1+* Protects receiving reply headers from the socket, as well as the +* fields reply, requests[].receiving and requests[].reply_possible +*/ CoMutex receive_mutex; +NBDReply reply;QEMUTimer *open_timer; -NBDReply reply;BlockDriverState *bs;
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org