From: Stefan Hajnoczi <stefanha@redhat.com>
To: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>,
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
qemu-block@nongnu.org, qemu-devel@nongnu.org,
Hanna Reitz <hreitz@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>
Subject: Re: [RFC PATCH v2 0/8] Removal of AioContext lock, bs->parents and ->children: new rwlock
Date: Sat, 30 Apr 2022 06:17:11 +0100 [thread overview]
Message-ID: <YmzGV8Evmet8RXUh@stefanha-x1.localdomain> (raw)
In-Reply-To: <3b156b87-11d5-3eb7-f58a-94939f65ea8f@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 3685 bytes --]
On Thu, Apr 28, 2022 at 11:56:09PM +0200, Emanuele Giuseppe Esposito wrote:
>
>
> Am 28/04/2022 um 12:45 schrieb Stefan Hajnoczi:
> > On Wed, Apr 27, 2022 at 08:55:35AM +0200, Emanuele Giuseppe Esposito wrote:
> >>
> >>
> >> Am 26/04/2022 um 10:51 schrieb Emanuele Giuseppe Esposito:
> >>> Luckly, most of the cases where we recursively go through a graph are
> >>> the BlockDriverState callback functions in block_int-common.h
> >>> In order to understand what to protect, I categorized the callbacks in
> >>> block_int-common.h depending on the type of function that calls them:
> >>>
> >>> 1) If the caller is a generated_co_wrapper, this function must be
> >>> protected by rdlock. The reason is that generated_co_wrapper create
> >>> coroutines that run in the given bs AioContext, so it doesn't matter
> >>> if we are running in the main loop or not, the coroutine might run
> >>> in an iothread.
> >>> 2) If the caller calls it directly, and has the GLOBAL_STATE_CODE() macro,
> >>> then the function is safe. The main loop is the writer and thus won't
> >>> read and write at the same time.
> >>> 3) If the caller calls it directly, but has not the GLOBAL_STATE_CODE()
> >>> macro, then we need to check the callers and see case-by-case if the
> >>> caller is in the main loop, if it needs to take the lock, or delegate
> >>> this duty to its caller (to reduce the places where to take it).
> >>>
> >>> I used the vrc script (https://github.com/bonzini/vrc) to get help finding
> >>> all the callers of a callback. Using its filter function, I can
> >>> omit all functions protected by the added lock to avoid having duplicates
> >>> when querying for new callbacks.
> >>
> >> I was wondering, if a function is in category (3) and runs in an
> >> Iothread but the function itself is not (currently) recursive, meaning
> >> it doesn't really traverse the graph or calls someone that traverses it,
> >> should I add the rdlock anyways or not?
> >>
> >> Example: bdrv_co_drain_end
> >>
> >> Pros:
> >> + Covers if in future a new recursive callback for a new/existing
> >> BlockDriver is implemented.
> >> + Covers also the case where I or someone missed the recursive part.
> >>
> >> Cons:
> >> - Potentially introducing an unnecessary critical section.
> >>
> >> What do you think?
> >
> > ->bdrv_co_drain_end() is a callback function. Do you mean whether its
> > caller, bdrv_drain_invoke_entry(), should take the rdlock around
> > ->bdrv_co_drain_end()?
>
> Yes. The problem is that the coroutine is created in bs AioContext, so
> it might be in an iothread.
>
> >
> > Going up further in the call chain (and maybe switching threads),
> > bdrv_do_drained_end() has QLIST_FOREACH(child, &bs->children, next) so
> > it needs protection. If the caller of bdrv_do_drained_end() holds then
> > rdlock then I think none of the child functions (including
> > ->bdrv_co_drain_end()) need to take it explicitly.
>
> Regarding bdrv_do_drained_end and similar, they are either running in
> the main loop (or they will be, if coming from a coroutine) or in the
> iothread running the AioContext of the bs involved.
>
> I think that most of the drains except for mirror.c are coming from main
> loop. I protected mirror.c in patch 8, even though right now I am not
> really sure that what I did is necessary, since the bh will be scheduled
> in the main loop.
>
> Therefore we don't really need locks around drains.
Are you saying rdlock isn't necessary in the main loop because nothing
can take the wrlock while our code is executing in the main loop?
Stefan
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2022-04-30 5:18 UTC|newest]
Thread overview: 67+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-26 8:51 [RFC PATCH v2 0/8] Removal of AioContext lock, bs->parents and ->children: new rwlock Emanuele Giuseppe Esposito
2022-04-26 8:51 ` [RFC PATCH v2 1/8] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
2022-04-28 11:09 ` Stefan Hajnoczi
2022-04-29 8:06 ` Emanuele Giuseppe Esposito
2022-04-30 5:21 ` Stefan Hajnoczi
2022-04-29 8:12 ` Paolo Bonzini
2022-04-26 8:51 ` [RFC PATCH v2 2/8] coroutine-lock: release lock when restarting all coroutines Emanuele Giuseppe Esposito
2022-04-26 14:59 ` Paolo Bonzini
2022-04-28 11:21 ` Stefan Hajnoczi
2022-04-28 22:14 ` Paolo Bonzini
2022-04-29 9:35 ` Emanuele Giuseppe Esposito
2022-04-26 8:51 ` [RFC PATCH v2 3/8] block: introduce a lock to protect graph operations Emanuele Giuseppe Esposito
2022-04-26 15:00 ` Paolo Bonzini
2022-04-28 13:45 ` Stefan Hajnoczi
2022-04-29 8:37 ` Emanuele Giuseppe Esposito
2022-04-30 5:48 ` Stefan Hajnoczi
2022-05-02 7:54 ` Emanuele Giuseppe Esposito
2022-05-03 10:50 ` Stefan Hajnoczi
2022-04-26 8:51 ` [RFC PATCH v2 4/8] async: register/unregister aiocontext in graph lock list Emanuele Giuseppe Esposito
2022-04-28 13:46 ` Stefan Hajnoczi
2022-04-28 22:19 ` Paolo Bonzini
2022-04-29 8:37 ` Emanuele Giuseppe Esposito
2022-04-26 8:51 ` [RFC PATCH v2 5/8] block.c: wrlock in bdrv_replace_child_noperm Emanuele Giuseppe Esposito
2022-04-26 15:07 ` Paolo Bonzini
2022-04-28 13:55 ` Stefan Hajnoczi
2022-04-29 8:41 ` Emanuele Giuseppe Esposito
2022-04-26 8:51 ` [RFC PATCH v2 6/8] block: assert that graph read and writes are performed correctly Emanuele Giuseppe Esposito
2022-04-28 14:43 ` Stefan Hajnoczi
2022-04-26 8:51 ` [RFC PATCH v2 7/8] graph-lock: implement WITH_GRAPH_RDLOCK_GUARD and GRAPH_RDLOCK_GUARD macros Emanuele Giuseppe Esposito
2022-04-28 15:00 ` Stefan Hajnoczi
2022-04-26 8:51 ` [RFC PATCH v2 8/8] mirror: protect drains in coroutine with rdlock Emanuele Giuseppe Esposito
2022-04-27 6:55 ` [RFC PATCH v2 0/8] Removal of AioContext lock, bs->parents and ->children: new rwlock Emanuele Giuseppe Esposito
2022-04-28 10:45 ` Stefan Hajnoczi
2022-04-28 21:56 ` Emanuele Giuseppe Esposito
2022-04-30 5:17 ` Stefan Hajnoczi [this message]
2022-05-02 8:02 ` Emanuele Giuseppe Esposito
2022-05-02 13:15 ` Paolo Bonzini
2022-05-03 8:24 ` Kevin Wolf
2022-05-03 11:04 ` Stefan Hajnoczi
2022-04-28 10:34 ` Stefan Hajnoczi
2022-04-29 8:06 ` Emanuele Giuseppe Esposito
2022-05-04 13:39 ` Stefan Hajnoczi
2022-05-17 10:59 ` Stefan Hajnoczi
2022-05-18 12:28 ` Emanuele Giuseppe Esposito
2022-05-18 12:43 ` Paolo Bonzini
2022-05-18 14:57 ` Stefan Hajnoczi
2022-05-18 16:14 ` Kevin Wolf
2022-05-19 11:27 ` Stefan Hajnoczi
2022-05-19 12:52 ` Kevin Wolf
2022-05-22 15:06 ` Stefan Hajnoczi
2022-05-23 8:48 ` Emanuele Giuseppe Esposito
2022-05-23 13:15 ` Stefan Hajnoczi
2022-05-23 13:54 ` Emanuele Giuseppe Esposito
2022-05-23 13:02 ` Kevin Wolf
2022-05-23 15:13 ` Stefan Hajnoczi
2022-05-23 16:04 ` Kevin Wolf
2022-05-23 16:45 ` Stefan Hajnoczi
2022-05-24 7:55 ` Paolo Bonzini
2022-05-24 8:08 ` Stefan Hajnoczi
2022-05-24 9:17 ` Paolo Bonzini
2022-05-24 10:20 ` Stefan Hajnoczi
2022-05-24 17:25 ` Paolo Bonzini
2022-05-24 10:36 ` Kevin Wolf
2022-05-25 7:41 ` Paolo Bonzini
2022-05-18 14:27 ` Stefan Hajnoczi
2022-05-24 12:10 ` Kevin Wolf
2022-05-25 8:27 ` 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=YmzGV8Evmet8RXUh@stefanha-x1.localdomain \
--to=stefanha@redhat.com \
--cc=eesposit@redhat.com \
--cc=fam@euphon.net \
--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=vsementsov@virtuozzo.com \
/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).