diff mbox series

[v12,16/21] blockjob: protect iostatus field in BlockJob struct

Message ID 20220926093214.506243-17-eesposit@redhat.com (mailing list archive)
State New, archived
Headers show
Series job: replace AioContext lock with job_mutex | expand

Commit Message

Emanuele Giuseppe Esposito Sept. 26, 2022, 9:32 a.m. UTC
iostatus is the only field (together with .job) that needs
protection using the job mutex.

It is set in the main loop (GLOBAL_STATE functions) but read
in I/O code (block_job_error_action).

In order to protect it, change block_job_iostatus_set_err
to block_job_iostatus_set_err_locked(), always called under
job lock.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 block/mirror.c | 7 +++++--
 blockjob.c     | 5 +++--
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Kevin Wolf Oct. 6, 2022, 4:48 p.m. UTC | #1
Am 26.09.2022 um 11:32 hat Emanuele Giuseppe Esposito geschrieben:
> iostatus is the only field (together with .job) that needs
> protection using the job mutex.
> 
> It is set in the main loop (GLOBAL_STATE functions) but read
> in I/O code (block_job_error_action).
> 
> In order to protect it, change block_job_iostatus_set_err
> to block_job_iostatus_set_err_locked(), always called under
> job lock.
> 
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> Reviewed-by: Kevin Wolf <kwolf@redhat.com>
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  block/mirror.c | 7 +++++--
>  blockjob.c     | 5 +++--
>  2 files changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/block/mirror.c b/block/mirror.c
> index c6bf7f40ce..7e32ee1d31 100644
> --- a/block/mirror.c
> +++ b/block/mirror.c
> @@ -893,7 +893,7 @@ static int coroutine_fn mirror_run(Job *job, Error **errp)
>      MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
>      BlockDriverState *bs = s->mirror_top_bs->backing->bs;
>      BlockDriverState *target_bs = blk_bs(s->target);
> -    bool need_drain = true;
> +    bool need_drain = true, iostatus;

iostatus isn't really a bool, it's BlockDeviceIoStatus.

>      int64_t length;
>      int64_t target_length;
>      BlockDriverInfo bdi;
> @@ -1016,8 +1016,11 @@ static int coroutine_fn mirror_run(Job *job, Error **errp)
>           * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is
>           * an error, or when the source is clean, whichever comes first. */
>          delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
> +        WITH_JOB_LOCK_GUARD() {
> +            iostatus = s->common.iostatus;
> +        }
>          if (delta < BLOCK_JOB_SLICE_TIME &&
> -            s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
> +            iostatus == BLOCK_DEVICE_IO_STATUS_OK) {

Your code actually happens to work because the one value that you
compare it against is BLOCK_DEVICE_IO_STATUS_OK, which is 0, so it maps
to false and everything else to true, but... it's still not right. :-)

>              if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
>                  (cnt == 0 && s->in_flight > 0)) {
>                  trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);

Kevin
diff mbox series

Patch

diff --git a/block/mirror.c b/block/mirror.c
index c6bf7f40ce..7e32ee1d31 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -893,7 +893,7 @@  static int coroutine_fn mirror_run(Job *job, Error **errp)
     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
     BlockDriverState *bs = s->mirror_top_bs->backing->bs;
     BlockDriverState *target_bs = blk_bs(s->target);
-    bool need_drain = true;
+    bool need_drain = true, iostatus;
     int64_t length;
     int64_t target_length;
     BlockDriverInfo bdi;
@@ -1016,8 +1016,11 @@  static int coroutine_fn mirror_run(Job *job, Error **errp)
          * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is
          * an error, or when the source is clean, whichever comes first. */
         delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
+        WITH_JOB_LOCK_GUARD() {
+            iostatus = s->common.iostatus;
+        }
         if (delta < BLOCK_JOB_SLICE_TIME &&
-            s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
+            iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
             if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
                 (cnt == 0 && s->in_flight > 0)) {
                 trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);
diff --git a/blockjob.c b/blockjob.c
index d8fb5311c7..d04f804001 100644
--- a/blockjob.c
+++ b/blockjob.c
@@ -363,7 +363,8 @@  BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
     return block_job_query_locked(job, errp);
 }
 
-static void block_job_iostatus_set_err(BlockJob *job, int error)
+/* Called with job lock held */
+static void block_job_iostatus_set_err_locked(BlockJob *job, int error)
 {
     if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
         job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
@@ -577,8 +578,8 @@  BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
                  */
                 job->job.user_paused = true;
             }
+            block_job_iostatus_set_err_locked(job, error);
         }
-        block_job_iostatus_set_err(job, error);
     }
     return action;
 }