qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@gmail.com>
To: Max Reitz <mreitz@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Fam Zheng <famz@redhat.com>,
	qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 05/11] blockjob: add block_job_defer_to_main_loop()
Date: Thu, 2 Oct 2014 15:58:53 +0100	[thread overview]
Message-ID: <20141002145853.GE6250@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <542C50BC.8010305@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2435 bytes --]

On Wed, Oct 01, 2014 at 09:06:36PM +0200, Max Reitz wrote:
> On 01.10.2014 19:01, Stefan Hajnoczi wrote:
> >+typedef struct {
> >+    BlockJob *job;
> >+    QEMUBH *bh;
> >+    AioContext *aio_context;
> >+    BlockJobDeferToMainLoopFn *fn;
> >+    void *opaque;
> >+} BlockJobDeferToMainLoopData;
> >+
> >+static void block_job_defer_to_main_loop_bh(void *opaque)
> >+{
> >+    BlockJobDeferToMainLoopData *data = opaque;
> >+
> >+    qemu_bh_delete(data->bh);
> >+
> >+    aio_context_acquire(data->aio_context);
> >+    data->fn(data->job, data->opaque);
> >+    aio_context_release(data->aio_context);
> >+
> >+    g_free(data);
> >+}
> >+
> >+void block_job_defer_to_main_loop(BlockJob *job,
> >+                                  BlockJobDeferToMainLoopFn *fn,
> >+                                  void *opaque)
> >+{
> >+    BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
> >+    data->job = job;
> >+    data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
> >+    data->aio_context = bdrv_get_aio_context(job->bs);
> >+    data->fn = fn;
> >+    data->opaque = opaque;
> >+
> >+    qemu_bh_schedule(data->bh);
> >+}
> 
> I'm not so sure whether it'd be possible to change the BDS's AIO context (in
> another thread) after the call to bdrv_get_aio_context() in
> block_job_defer_to_main_loop() and before block_job_defer_to_main_loop_bh()
> is run. If so, this might create race conditions.
> 
> Other than that, this patch looks good.

Yes, I think you are correct.

The solution is a little tricky, we need to hold both AioContexts:

  static void block_job_defer_to_main_loop_bh(void *opaque)
  {
      BlockJobDeferToMainLoopData *data = opaque;
      AioContext aio_context;

      qemu_bh_delete(data->bh);

      /* Prevent race with block_job_defer_to_main_loop() */
      aio_context_acquire(data->aio_context);

      /* Fetch BDS AioContext again, in case it has changed */
      aio_context = bdrv_get_aio_context(data->job->bs);
      aio_context_acquire(aio_context);

      data->fn(data->job, data->opaque);

      aio_context_release(aio_context);

      aio_context_release(data->aio_context);

      g_free(data);
  }

Tada!  Because this executes in the main loop it is safe to do this - no lock
ordering problems.  And because aio_context_acquire() is recursive we can lock
twice without problems.

Stefan

[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]

  reply	other threads:[~2014-10-02 14:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-01 17:01 [Qemu-devel] [PATCH 00/11] block: allow blockjobs to coexist with dataplane Stefan Hajnoczi
2014-10-01 17:01 ` [Qemu-devel] [PATCH 01/11] block: acquire AioContext in generic blockjob QMP commands Stefan Hajnoczi
2014-10-01 18:27   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 02/11] blockdev: acquire AioContext in do_qmp_query_block_jobs_one() Stefan Hajnoczi
2014-10-01 18:32   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 03/11] blockdev: acquire AioContext in blockdev_mark_auto_del() Stefan Hajnoczi
2014-10-01 18:39   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 04/11] blockdev: add note that block_job_cb() must be thread-safe Stefan Hajnoczi
2014-10-01 18:42   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 05/11] blockjob: add block_job_defer_to_main_loop() Stefan Hajnoczi
2014-10-01 19:06   ` Max Reitz
2014-10-02 14:58     ` Stefan Hajnoczi [this message]
2014-10-01 17:01 ` [Qemu-devel] [PATCH 06/11] block: add bdrv_drain() Stefan Hajnoczi
2014-10-01 19:15   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 07/11] block: let backup blockjob run in BDS AioContext Stefan Hajnoczi
2014-10-01 19:38   ` Max Reitz
2014-10-02 15:08     ` Stefan Hajnoczi
2014-10-04 19:54       ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 08/11] block: let stream " Stefan Hajnoczi
2014-10-04 20:48   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 09/11] block: let mirror " Stefan Hajnoczi
2014-10-04 21:07   ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 10/11] block: let commit " Stefan Hajnoczi
2014-10-04 21:28   ` Max Reitz
2014-10-06  9:30     ` Stefan Hajnoczi
2014-10-07 15:18       ` Max Reitz
2014-10-01 17:01 ` [Qemu-devel] [PATCH 11/11] block: declare blockjobs and dataplane friends! Stefan Hajnoczi
2014-10-04 21:30   ` Max Reitz

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=20141002145853.GE6250@stefanha-thinkpad.redhat.com \
    --to=stefanha@gmail.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.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).