summaryrefslogtreecommitdiffstats
path: root/job.c
diff options
context:
space:
mode:
authorEmanuele Giuseppe Esposito2022-09-26 11:32:06 +0200
committerKevin Wolf2022-10-07 12:11:41 +0200
commit3ed4f708fe12537066d21f3dd111af013f7a6b8c (patch)
tree356bc8ca58f2ba05692c3a7c117f95a91661d8a8 /job.c
parentjob: detect change of aiocontext within job coroutine (diff)
downloadqemu-3ed4f708fe12537066d21f3dd111af013f7a6b8c.tar.gz
qemu-3ed4f708fe12537066d21f3dd111af013f7a6b8c.tar.xz
qemu-3ed4f708fe12537066d21f3dd111af013f7a6b8c.zip
jobs: protect job.aio_context with BQL and job_mutex
In order to make it thread safe, implement a "fake rwlock", where we allow reads under BQL *or* job_mutex held, but writes only under BQL *and* job_mutex. The only write we have is in child_job_set_aio_ctx, which always happens under drain (so the job is paused). For this reason, introduce job_set_aio_context and make sure that the context is set under BQL, job_mutex and drain. Also make sure all other places where the aiocontext is read are protected. The reads in commit.c and mirror.c are actually safe, because always done under BQL. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Suggested-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com> Message-Id: <20220926093214.506243-14-eesposit@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'job.c')
-rw-r--r--job.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/job.c b/job.c
index 3ef5028751..3e6f61c523 100644
--- a/job.c
+++ b/job.c
@@ -396,6 +396,17 @@ Job *job_get(const char *id)
return job_get_locked(id);
}
+void job_set_aio_context(Job *job, AioContext *ctx)
+{
+ /* protect against read in job_finish_sync_locked and job_start */
+ GLOBAL_STATE_CODE();
+ /* protect against read in job_do_yield_locked */
+ JOB_LOCK_GUARD();
+ /* ensure the job is quiescent while the AioContext is changed */
+ assert(job->paused || job_is_completed_locked(job));
+ job->aio_context = ctx;
+}
+
/* Called with job_mutex *not* held. */
static void job_sleep_timer_cb(void *opaque)
{
@@ -1379,6 +1390,7 @@ int job_finish_sync_locked(Job *job,
{
Error *local_err = NULL;
int ret;
+ GLOBAL_STATE_CODE();
job_ref_locked(job);