On Fri, Apr 16, 2021 at 11:08:45AM +0300, Vladimir Sementsov-Ogievskiy wrote:
Grammar suggestions: > Instead of connect_bh, bh_ctx and wait_connect fields we can live with > only one link to waiting coroutine, protected by mutex. Instead of managing connect_bh, bh_ctx, and wait_connect fields, we can use a single link to the waiting coroutine with proper mutex protection. > > So new logic is: > > nbd_co_establish_connection() sets wait_co under mutex, release the > mutex and do yield(). Note, that wait_co may be scheduled by thread > immediately after unlocking the mutex. Still, in main thread (or > iothread) we'll not reach the code for entering the coroutine until the > yield() so we are safe. nbd_co_establish_connection() sets wait_co under the mutex, releases the mutex, then yield()s. Note that wait_co may be scheduled by the thread immediately after unlocking the mutex. Still, the main thread (or iothread) will not reach the code for entering the coroutine until the yield(), so we are safe. > > Both connect_thread_func() and nbd_co_establish_connection_cancel() do > the following to handle wait_co: > > Under mutex, if thr->wait_co is not NULL, call aio_co_wake() (which > never tries to acquire aio context since previous commit, so we are > safe to do it under thr->mutex) and set thr->wait_co to NULL. > This way we protect ourselves of scheduling it twice. Under the mutex, if thr_wait_co is not NULL, call aio_co_wake() (the previous commit ensures it never tries to acquire the aio context, so we are safe even while under thr->mutex), then sets thr->wait_co to NULL. This way, we avoid scheduling the coroutine twice. > > Also this commit make nbd_co_establish_connection() less connected to > bs (we have generic pointer to the coroutine, not use s->connection_co > directly). So, we are on the way of splitting connection API out of > nbd.c (which is overcomplicated now). Also, this commit reduces the dependence of nbd_co_establish_connection() on the internals of bs (we now use a generic pointer to the coroutine, instead of direct use of s->connection_co). This is a step towards splitting the connection API out of nbd.c. > > Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@virtuozzo.com> > --- > block/nbd.c | 49 +++++++++---------------------------------------- > 1 file changed, 9 insertions(+), 40 deletions(-) > > +++ b/block/nbd.c > @@ -101,10 +95,10 @@ typedef struct NBDConnectThread { > QIOChannelSocket *sioc; > Error *err; > > - /* state and bh_ctx are protected by mutex */ > QemuMutex mutex; > + /* All further fields are protected by mutex */ > NBDConnectThreadState state; /* current state of the thread */ > - AioContext *bh_ctx; /* where to schedule bh (NULL means don't schedule) > */ > + Coroutine *wait_co; /* nbd_co_establish_connection() wait in yield() */ I'm not sure if that comment is the most legible, but I'm not coming up with an alternative. Maybe: /* * if non-NULL, which coroutine to wake in * nbd_co_establish_connection() after yield() */ But the simplification looks nice, and I didn't spot any obvious problems with the refactoring. Reviewed-by: Eric Blake <ebl...@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org