qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Maxim Levitsky <mlevitsk@redhat.com>
To: Max Reitz <mreitz@redhat.com>, qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [Qemu-block] [PATCH 1/7] block/nbd: Fix hang in .bdrv_close()
Date: Tue, 16 Jul 2019 16:08:33 +0300	[thread overview]
Message-ID: <8e029ef60c0318774e14d487a0ddc3766da37228.camel@redhat.com> (raw)
In-Reply-To: <20190712173600.14554-2-mreitz@redhat.com>

On Fri, 2019-07-12 at 19:35 +0200, Max Reitz wrote:
> When nbd_close() is called from a coroutine, the connection_co never
> gets to run, and thus nbd_teardown_connection() hangs.
> 
> This is because aio_co_enter() only puts the connection_co into the main
> coroutine's wake-up queue, so this main coroutine needs to yield and
> wait for connection_co to terminate.

After diving into NBD's co-routines (this is 2nd time I do this) and speaking about this
with Max Reitz on IRC, could I suggest to extend the explanation a bit, something like that:


When nbd_close() is called from a coroutine, the connection_co never
gets to run, and thus nbd_teardown_connection() hangs.

This happens because the connection_co is woken up by nbd_teardown_connection() by closing the socket, 
which wakes up the IO channel on which the connection_co is blocked.

However connection_co is waken up by aio_co_wake, which has an assumption that if the caller is already in
a coroutine, the caller doesn't switch immediately to the woken coroutine, but rather it adds the coroutine to list
of coroutines to wake immediately after the current co-routine yields/terminates (self->co_queue_wakeup)
Since we instead do aio_poll, that never happens.


The patch itself looks fine, so
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>

Best regards,
	Maxim Levitsky

> 
> Suggested-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/nbd.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/block/nbd.c b/block/nbd.c
> index 81edabbf35..8f5ee86842 100644
> --- a/block/nbd.c
> +++ b/block/nbd.c
> @@ -61,6 +61,7 @@ typedef struct BDRVNBDState {
>      CoMutex send_mutex;
>      CoQueue free_sema;
>      Coroutine *connection_co;
> +    Coroutine *teardown_co;
>      int in_flight;
>  
>      NBDClientRequest requests[MAX_NBD_REQUESTS];
> @@ -135,7 +136,15 @@ static void nbd_teardown_connection(BlockDriverState *bs)
>      qio_channel_shutdown(s->ioc,
>                           QIO_CHANNEL_SHUTDOWN_BOTH,
>                           NULL);
> -    BDRV_POLL_WHILE(bs, s->connection_co);
> +    if (qemu_in_coroutine()) {
> +        s->teardown_co = qemu_coroutine_self();
> +        /* connection_co resumes us when it terminates */
> +        qemu_coroutine_yield();
> +        s->teardown_co = NULL;
> +    } else {
> +        BDRV_POLL_WHILE(bs, s->connection_co);
> +    }
> +    assert(!s->connection_co);
>  
>      nbd_client_detach_aio_context(bs);
>      object_unref(OBJECT(s->sioc));
> @@ -207,6 +216,9 @@ static coroutine_fn void nbd_connection_entry(void *opaque)
>      bdrv_dec_in_flight(s->bs);
>  
>      s->connection_co = NULL;
> +    if (s->teardown_co) {
> +        aio_co_wake(s->teardown_co);
> +    }
>      aio_wait_kick();
>  }
>  




  reply	other threads:[~2019-07-16 13:09 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-12 17:35 [Qemu-devel] [PATCH 0/7] block: Generic file creation fallback Max Reitz
2019-07-12 17:35 ` [Qemu-devel] [PATCH 1/7] block/nbd: Fix hang in .bdrv_close() Max Reitz
2019-07-16 13:08   ` Maxim Levitsky [this message]
2019-07-12 17:35 ` [Qemu-devel] [PATCH 2/7] block: Add blk_truncate_for_formatting() Max Reitz
2019-07-16 13:08   ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-16 15:45     ` Maxim Levitsky
2019-07-16 16:03       ` Max Reitz
2019-07-12 17:35 ` [Qemu-devel] [PATCH 3/7] block: Use blk_truncate_for_formatting() Max Reitz
2019-07-16 13:08   ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 4/7] block: Generic file creation fallback Max Reitz
2019-07-16 13:09   ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 5/7] file-posix: Drop hdev_co_create_opts() Max Reitz
2019-07-16 13:09   ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 6/7] iscsi: Drop iscsi_co_create_opts() Max Reitz
2019-07-16 13:08   ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:36 ` [Qemu-devel] [PATCH 7/7] iotests: Add test for image creation fallback Max Reitz
2019-07-15  9:31   ` Thomas Huth
2019-07-15  9:48     ` Max Reitz
2019-07-16 14:10       ` Eric Blake
2019-09-05 13:30 ` [Qemu-devel] [Qemu-block] [PATCH 0/7] block: Generic file " Maxim Levitsky
2019-09-10  9:16   ` Max Reitz
2019-09-10 10:40     ` Maxim Levitsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8e029ef60c0318774e14d487a0ddc3766da37228.camel@redhat.com \
    --to=mlevitsk@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).