qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: Hanna Reitz <hreitz@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, John Snow <jsnow@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>,
	qemu-block@nongnu.org
Subject: Re: [RFC PATCH 7/8] block: use the new _change_ API instead of _can_set_ and _set_
Date: Tue, 19 Jul 2022 11:57:01 +0200	[thread overview]
Message-ID: <88e07fa2-60c9-0fb6-fc1b-b03a2f7d37f5@redhat.com> (raw)
In-Reply-To: <ea2b8793-532d-e7c1-c34f-a9ef89f8586d@redhat.com>



Am 18/07/2022 um 18:39 schrieb Paolo Bonzini:
> On 7/12/22 23:19, Emanuele Giuseppe Esposito wrote:
>> diff --git a/block/block-backend.c b/block/block-backend.c
>> index 674eaaa2bf..6e90ac3a6a 100644
>> --- a/block/block-backend.c
>> +++ b/block/block-backend.c
>> @@ -2184,8 +2184,12 @@ static int blk_do_set_aio_context(BlockBackend
>> *blk, AioContext *new_context,
>>           bdrv_ref(bs);
>>             if (update_root_node) {
>> -            ret = bdrv_child_try_set_aio_context(bs, new_context,
>> blk->root,
>> -                                                 errp);
>> +            /*
>> +             * update_root_node MUST be false for
>> blk_root_set_aio_ctx_commit(),
>> +             * as we are already in the commit function of a
>> transaction.
>> +             */
>> +            ret = bdrv_child_try_change_aio_context(bs, new_context,
>> blk->root,
>> +                                                    errp);
>>               if (ret < 0) {
>>                   bdrv_unref(bs);
>>                   return ret;
> 
> 
> Looking further at blk_do_set_aio_context:
> 
>         if (tgm->throttle_state) {
>             bdrv_drained_begin(bs);
>             throttle_group_detach_aio_context(tgm);
>             throttle_group_attach_aio_context(tgm, new_context);
>             bdrv_drained_end(bs);
>         }
> 
> Perhaps the drained_begin/drained_end pair can be moved to
> blk_set_aio_context?  It shouldn't be needed from the change_aio_ctx
> callback, because bs is already drained.  If so, blk_do_set_aio_context
> would become just:
> 
>      if (tgm->throttle_state) {
>          throttle_group_detach_aio_context(tgm);
>          throttle_group_attach_aio_context(tgm, new_context);
>      }
>      blk->ctx = new_context;
> 
> and blk_set_aio_context would be something like:
> 
>     if (bs) {
>         bdrv_ref(bs);
>         ret = bdrv_child_try_set_aio_context(bs, new_context, blk->root,
>                                              errp);
>         if (ret < 0) {
>             goto out_no_drain;
>         }
>         bdrv_drained_begin(bs);
>     }
>     ret = blk_do_set_aio_context(blk, new_context, errp);
>     if (bs) {
>         bdrv_drained_end(bs);
> out_no_drain;
>         bdrv_unref(bs);
>     }
>     return ret;
> 
> Paolo
> 

This is another example on how things here do not take into account many
other use cases outside the common ones: if I use the above suggestion,
test-block-iothread fails.

The reason why it fails is simple: now we drain regardless of
tgm->throttle_state being true or false. And this requires yet another
aiocontext lock.

Which means that if we are calling blk_set_aio_context where the bs
exists and tgm->throttle_state is true, we have a bug.

Test is test_attach_blockjob and function is line 435
blk_set_aio_context(blk, ctx, &error_abort);

The reason is that bs is first switched to the new aiocontext and then
we try to drain it without holding the lock.

Wrapping the new drains in aio_context_acquire/release(new_context) is
not so much helpful either, since apparently the following
blk_set_aio_context makes aio_poll() hang.
I am not sure why, any ideas?


Code:

static int blk_do_set_aio_context(BlockBackend *blk, AioContext
*new_context,
                                  Error **errp)
{
    BlockDriverState *bs = blk_bs(blk);
    ThrottleGroupMember *tgm = &blk->public.throttle_group_member;

    if (bs) {
        bdrv_ref(bs);

        if (tgm->throttle_state) {
            throttle_group_detach_aio_context(tgm);
            throttle_group_attach_aio_context(tgm, new_context);
        }

        bdrv_unref(bs);
    }

    blk->ctx = new_context;
    return 0;
}


int blk_set_aio_context(BlockBackend *blk, AioContext *new_context,
                        Error **errp)
{
    BlockDriverState *bs = blk_bs(blk);
    int ret;
    GLOBAL_STATE_CODE();

    if (bs) {
        bdrv_ref(bs);
        ret = bdrv_child_try_change_aio_context(bs, new_context, blk->root,
                                                errp);
        if (ret < 0) {
            goto out_no_drain;
        }
        if (new_context != qemu_get_aio_context()) {
            aio_context_acquire(new_context);
        }
        bdrv_drained_begin(bs); // <-------------------- hangs here!
    }

    ret = blk_do_set_aio_context(blk, new_context, errp);

    if (bs) {
        bdrv_drained_end(bs);
        if (new_context != qemu_get_aio_context()) {
            aio_context_release(new_context);
        }
out_no_drain:
        bdrv_unref(bs);
    }

    return ret;
}

Emanuele



  reply	other threads:[~2022-07-19 10:19 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-12 21:19 [RFC PATCH 0/8] Refactor bdrv_try_set_aio_context using transactions Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 1/8] block.c: assert bs->aio_context is written under BQL and drains Emanuele Giuseppe Esposito
2022-07-14 14:03   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 2/8] transactions: add tran_add_back Emanuele Giuseppe Esposito
2022-07-14 15:13   ` Hanna Reitz
2022-07-18 16:20     ` Paolo Bonzini
2022-07-20 13:36       ` Vladimir Sementsov-Ogievskiy
2022-07-12 21:19 ` [RFC PATCH 3/8] RFC: block: use transactions as a replacement of ->{can_}set_aio_context() Emanuele Giuseppe Esposito
2022-07-14 16:45   ` Hanna Reitz
2022-07-18  8:45     ` Emanuele Giuseppe Esposito
2022-07-18  9:09     ` Emanuele Giuseppe Esposito
2022-07-20 14:09   ` Vladimir Sementsov-Ogievskiy
2022-07-25  8:35     ` Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 4/8] blockjob: implement .change_aio_ctx in child_job Emanuele Giuseppe Esposito
2022-07-15 11:14   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 5/8] block: implement .change_aio_ctx in child_of_bds Emanuele Giuseppe Esposito
2022-07-15 11:17   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 6/8] block-backend: implement .change_aio_ctx in child_root Emanuele Giuseppe Esposito
2022-07-15 11:34   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 7/8] block: use the new _change_ API instead of _can_set_ and _set_ Emanuele Giuseppe Esposito
2022-07-15 13:32   ` Hanna Reitz
2022-07-18 16:30   ` Paolo Bonzini
2022-07-18 16:39   ` Paolo Bonzini
2022-07-19  9:57     ` Emanuele Giuseppe Esposito [this message]
2022-07-19 18:07       ` Paolo Bonzini
2022-07-20 11:47         ` Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 8/8] block: remove all unused ->can_set_aio_ctx and ->set_aio_ctx callbacks Emanuele Giuseppe Esposito
2022-07-15 14:34   ` Hanna Reitz
2022-07-18  8:45     ` Emanuele Giuseppe Esposito
2022-07-18 14:39       ` Emanuele Giuseppe Esposito

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=88e07fa2-60c9-0fb6-fc1b-b03a2f7d37f5@redhat.com \
    --to=eesposit@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=v.sementsov-og@mail.ru \
    /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).