qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org,
	Wen Congyang <wencongyang2@huawei.com>,
	Xie Changlong <xiechanglong.d@gmail.com>,
	Markus Armbruster <armbru@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>
Subject: Re: [RFC PATCH 5/6] job: use global job_mutex to protect struct Job
Date: Thu, 8 Jul 2021 13:56:08 +0100	[thread overview]
Message-ID: <YOb16JltX56P88Vo@stefanha-x1.localdomain> (raw)
In-Reply-To: <20210707165813.55361-6-eesposit@redhat.com>

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

On Wed, Jul 07, 2021 at 06:58:12PM +0200, Emanuele Giuseppe Esposito wrote:
> This lock is going to replace most of the AioContext locks
> in the job and blockjob, so that a Job can run in an arbitrary
> AioContext.
> 
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> ---
>  include/block/blockjob_int.h |   1 +
>  include/qemu/job.h           |   2 +
>  block/backup.c               |   4 +
>  block/mirror.c               |  11 +-
>  blockdev.c                   |  62 ++++----
>  blockjob.c                   |  67 +++++++--
>  job-qmp.c                    |  55 +++----
>  job.c                        | 284 +++++++++++++++++++++++++++--------
>  qemu-img.c                   |  15 +-
>  9 files changed, 350 insertions(+), 151 deletions(-)
> 
> diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
> index 6633d83da2..8b91126506 100644
> --- a/include/block/blockjob_int.h
> +++ b/include/block/blockjob_int.h
> @@ -53,6 +53,7 @@ struct BlockJobDriver {
>       */
>      void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
>  
> +    /* Called with job mutex *not* held. */
>      void (*set_speed)(BlockJob *job, int64_t speed);
>  };
>  
> diff --git a/include/qemu/job.h b/include/qemu/job.h
> index 4421d08d93..359f4e6b3a 100644
> --- a/include/qemu/job.h
> +++ b/include/qemu/job.h
> @@ -49,6 +49,8 @@ typedef struct Job {
>      /**
>       * The type of this job.
>       * Set it in job_create and just read.
> +     * All calls to the driver function must be not locked by job_mutex,
> +     * to avoid deadlocks.
>       */
>      const JobDriver *driver;
>  
> diff --git a/block/backup.c b/block/backup.c
> index bd3614ce70..80ce956299 100644
> --- a/block/backup.c
> +++ b/block/backup.c
> @@ -315,6 +315,10 @@ static void coroutine_fn backup_pause(Job *job)
>      }
>  }
>  
> +/*
> + * Called with job mutex *not* held (we don't want to call block_copy_kick
> + * with the lock held!)
> + */
>  static void coroutine_fn backup_set_speed(BlockJob *job, int64_t speed)
>  {
>      BackupBlockJob *s = container_of(job, BackupBlockJob, common);
> diff --git a/block/mirror.c b/block/mirror.c
> index 49aaaafffa..deefaa6a39 100644
> --- a/block/mirror.c
> +++ b/block/mirror.c
> @@ -1150,9 +1150,11 @@ static void mirror_complete(Job *job, Error **errp)
>      s->should_complete = true;
>  
>      /* If the job is paused, it will be re-entered when it is resumed */
> +    job_lock();
>      if (!job_is_paused(job)) {
> -        job_enter(job);
> +        job_enter_locked(job);
>      }
> +    job_unlock();
>  }
>  
>  static void coroutine_fn mirror_pause(Job *job)
> @@ -1171,10 +1173,13 @@ static bool mirror_drained_poll(BlockJob *job)
>       * from one of our own drain sections, to avoid a deadlock waiting for
>       * ourselves.
>       */
> -    if (!job_is_paused(&s->common.job) && !job_is_cancelled(&s->common.job) &&
> -        !s->in_drain) {
> +    job_lock();
> +    if (!job_is_paused(&s->common.job) &&
> +        !job_is_cancelled_locked(&s->common.job) && !s->in_drain) {
> +        job_unlock();
>          return true;
>      }
> +    job_unlock();
>  
>      return !!s->in_flight;
>  }
> diff --git a/blockdev.c b/blockdev.c
> index 8e2c15370e..9255aea6a2 100644
> --- a/blockdev.c
> +++ b/blockdev.c
> @@ -150,9 +150,11 @@ void blockdev_mark_auto_del(BlockBackend *blk)
>              AioContext *aio_context = job_get_aiocontext(&job->job);
>              aio_context_acquire(aio_context);
>  
> +            job_lock();
>              job_cancel(&job->job, false);
>  
>              aio_context_release(aio_context);
> +            job_unlock();

This looks strange. The way it's written suggests there is a reason why
job_unlock() has to be called after aio_context_release(). Can
job_unlock() be called immediately after job_cancel()?

>          }
>      }
>  
> @@ -3309,48 +3311,44 @@ out:
>      aio_context_release(aio_context);
>  }
>  
> -/* Get a block job using its ID and acquire its AioContext */
> -static BlockJob *find_block_job(const char *id, AioContext **aio_context,
> -                                Error **errp)
> +/* Get a block job using its ID and acquire its job_lock */

"its" suggests job_lock is per-Job. I suggest saying something like
"Returns with job_lock held on success" instead.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2021-07-08 13:04 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-07 16:58 [RFC PATCH 0/6] job: replace AioContext lock with job_mutex Emanuele Giuseppe Esposito
2021-07-07 16:58 ` [RFC PATCH 1/6] job: use getter/setters instead of accessing the Job fields directly Emanuele Giuseppe Esposito
2021-07-07 16:58 ` [RFC PATCH 2/6] job: _locked functions and public job_lock/unlock for next patch Emanuele Giuseppe Esposito
2021-07-08 10:50   ` Stefan Hajnoczi
2021-07-12  8:43     ` Emanuele Giuseppe Esposito
2021-07-13 13:32       ` Stefan Hajnoczi
2021-07-07 16:58 ` [RFC PATCH 3/6] job: minor changes to simplify locking Emanuele Giuseppe Esposito
2021-07-08 10:55   ` Stefan Hajnoczi
2021-07-12  8:43     ` Emanuele Giuseppe Esposito
2021-07-13 17:56   ` Eric Blake
2021-07-07 16:58 ` [RFC PATCH 4/6] job.h: categorize job fields Emanuele Giuseppe Esposito
2021-07-08 11:02   ` Stefan Hajnoczi
2021-07-12  8:43     ` Emanuele Giuseppe Esposito
2021-07-07 16:58 ` [RFC PATCH 5/6] job: use global job_mutex to protect struct Job Emanuele Giuseppe Esposito
2021-07-08 12:56   ` Stefan Hajnoczi [this message]
2021-07-12  8:43     ` Emanuele Giuseppe Esposito
2021-07-07 16:58 ` [RFC PATCH 6/6] jobs: remove unnecessary AioContext aquire/release pairs Emanuele Giuseppe Esposito
2021-07-08 10:36 ` [RFC PATCH 0/6] job: replace AioContext lock with job_mutex Stefan Hajnoczi
2021-07-08 11:32   ` Paolo Bonzini
2021-07-08 12:14     ` Kevin Wolf
2021-07-08 13:04     ` Stefan Hajnoczi
2021-07-12  8:41       ` Emanuele Giuseppe Esposito
2021-07-13 13:10         ` Stefan Hajnoczi
2021-07-13 15:18           ` Vladimir Sementsov-Ogievskiy
2021-07-13 16:38             ` Stefan Hajnoczi
2021-07-15 12:35               ` Vladimir Sementsov-Ogievskiy
2021-07-15 13:29                 ` Stefan Hajnoczi
2021-07-16 15:23           ` Kevin Wolf
2021-07-19  9:29             ` Stefan Hajnoczi
2021-07-19 14:54               ` Kevin Wolf
2021-07-08 13:09 ` Stefan Hajnoczi
2021-07-12  8:42   ` Emanuele Giuseppe Esposito
2021-07-13 13:27     ` Stefan Hajnoczi

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=YOb16JltX56P88Vo@stefanha-x1.localdomain \
    --to=stefanha@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eesposit@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    --cc=wencongyang2@huawei.com \
    --cc=xiechanglong.d@gmail.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).