qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
	qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Wen Congyang <wencongyang2@huawei.com>,
	Xie Changlong <xiechanglong.d@gmail.com>,
	Markus Armbruster <armbru@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>, Fam Zheng <fam@euphon.net>,
	qemu-devel@nongnu.org
Subject: Re: [PATCH v11 13/21] jobs: protect job.aio_context with BQL and job_mutex
Date: Sun, 18 Sep 2022 18:54:48 +0200	[thread overview]
Message-ID: <4e96d073-f968-bffe-87e8-6e78d3798752@redhat.com> (raw)
In-Reply-To: <6c7d735c-b473-c540-5bf6-db29794450d0@yandex-team.ru>



Am 14/09/2022 um 15:25 schrieb Vladimir Sementsov-Ogievskiy:
> On 8/26/22 16:20, Emanuele Giuseppe Esposito wrote:
>> 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>
>> ---
>>   block/replication.c |  7 +++++--
>>   blockjob.c          |  3 ++-
>>   include/qemu/job.h  | 23 ++++++++++++++++++++---
>>   job.c               | 12 ++++++++++++
>>   4 files changed, 39 insertions(+), 6 deletions(-)
>>
>> diff --git a/block/replication.c b/block/replication.c
>> index 55c8f894aa..6e02d98126 100644
>> --- a/block/replication.c
>> +++ b/block/replication.c
>> @@ -142,14 +142,17 @@ static void replication_close(BlockDriverState *bs)
>>   {
>>       BDRVReplicationState *s = bs->opaque;
>>       Job *commit_job;
>> +    GLOBAL_STATE_CODE();
>>         if (s->stage == BLOCK_REPLICATION_RUNNING) {
>>           replication_stop(s->rs, false, NULL);
>>       }
>>       if (s->stage == BLOCK_REPLICATION_FAILOVER) {
>>           commit_job = &s->commit_job->job;
>> -        assert(commit_job->aio_context ==
>> qemu_get_current_aio_context());
>> -        job_cancel_sync(commit_job, false);
>> +        WITH_JOB_LOCK_GUARD() {
>> +            assert(commit_job->aio_context ==
>> qemu_get_current_aio_context());
>> +            job_cancel_sync_locked(commit_job, false);
>> +        }
> 
> As Kevin said, this hunk seems not needed.. Why to add locking for
> reading aio_context, when we have GLOBAL_STATE_CODE()?

Ok, getting rid of it.
> 
>>       }
>>         if (s->mode == REPLICATION_MODE_SECONDARY) {
>> diff --git a/blockjob.c b/blockjob.c
>> index 96fb9d9f73..c8919cef9b 100644
>> --- a/blockjob.c
>> +++ b/blockjob.c
>> @@ -162,12 +162,13 @@ static void child_job_set_aio_ctx(BdrvChild *c,
>> AioContext *ctx,
>>           bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
>>       }
>>   -    job->job.aio_context = ctx;
>> +    job_set_aio_context(&job->job, ctx);
>>   }
>>     static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
>>   {
>>       BlockJob *job = c->opaque;
>> +    GLOBAL_STATE_CODE();
>>         return job->job.aio_context;
>>   }
>> diff --git a/include/qemu/job.h b/include/qemu/job.h
>> index 5709e8d4a8..cede227e67 100644
>> --- a/include/qemu/job.h
>> +++ b/include/qemu/job.h
>> @@ -74,11 +74,17 @@ typedef struct Job {
>>       /* ProgressMeter API is thread-safe */
>>       ProgressMeter progress;
>>   +    /**
>> +     * AioContext to run the job coroutine in.
>> +     * The job Aiocontext can be read when holding *either*
>> +     * the BQL (so we are in the main loop) or the job_mutex.
>> +     * It can only be written when we hold *both* BQL
>> +     * and the job_mutex.
>> +     */
>> +    AioContext *aio_context;
>>   -    /** Protected by AioContext lock */
>>   -    /** AioContext to run the job coroutine in */
>> -    AioContext *aio_context;
>> +    /** Protected by AioContext lock */
>>         /** Reference count of the block job */
>>       int refcnt;
>> @@ -741,4 +747,15 @@ int job_finish_sync(Job *job, void (*finish)(Job
>> *, Error **errp),
>>   int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error
>> **errp),
>>                              Error **errp);
>>   +/**
>> + * Sets the @job->aio_context.
>> + * Called with job_mutex *not* held.
>> + *
>> + * This function must run in the main thread to protect against
>> + * concurrent read in job_finish_sync_locked(), takes the job_mutex
>> + * lock to protect against the read in job_do_yield_locked(), and must
>> + * be called when the coroutine is quiescent.
> 
> May be "job is quiscent" or "job is doing nothing", "no in-flight io
> operations in job".
> 
> For example, backup has several running coroutines in contest of
> block_copy process, and main coroutine of the job
> is almost always "quescent"..

"job is quiescent" seems ok

> 
>> + */
>> +void job_set_aio_context(Job *job, AioContext *ctx);
>> +
>>   #endif
>> diff --git a/job.c b/job.c
>> index 85ae843f03..9f2fb2e73b 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 coroutine is quiescent while the AioContext is
>> changed */
> 
> same not here.

Ok

Thank you,
Emanuele
> 
>> +    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);
>>   
> 
> 



  reply	other threads:[~2022-09-18 16:55 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 13:20 [PATCH v11 00/21] job: replace AioContext lock with job_mutex Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 01/21] job.c: make job_mutex and job_lock/unlock() public Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 02/21] job.h: categorize fields in struct Job Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 03/21] job.c: API functions not used outside should be static Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 04/21] aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 05/21] job.c: add job_lock/unlock while keeping job.h intact Emanuele Giuseppe Esposito
2022-09-15 15:12   ` Vladimir Sementsov-Ogievskiy
2022-08-26 13:20 ` [PATCH v11 06/21] job: move and update comments from blockjob.c Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 07/21] blockjob: introduce block_job _locked() APIs Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 08/21] jobs: add job lock in find_* functions Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 09/21] jobs: use job locks also in the unit tests Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 10/21] block/mirror.c: use of job helpers in drivers Emanuele Giuseppe Esposito
2022-09-14 12:51   ` Vladimir Sementsov-Ogievskiy
2022-08-26 13:20 ` [PATCH v11 11/21] jobs: group together API calls under the same job lock Emanuele Giuseppe Esposito
2022-09-14 12:36   ` Vladimir Sementsov-Ogievskiy
2022-09-18 16:51     ` Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 12/21] job: detect change of aiocontext within job coroutine Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 13/21] jobs: protect job.aio_context with BQL and job_mutex Emanuele Giuseppe Esposito
2022-09-14 13:25   ` Vladimir Sementsov-Ogievskiy
2022-09-18 16:54     ` Emanuele Giuseppe Esposito [this message]
2022-08-26 13:20 ` [PATCH v11 14/21] blockjob.h: categorize fields in struct BlockJob Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 15/21] blockjob: rename notifier callbacks as _locked Emanuele Giuseppe Esposito
2022-08-26 13:20 ` [PATCH v11 16/21] blockjob: protect iostatus field in BlockJob struct Emanuele Giuseppe Esposito
2022-09-14 13:52   ` Vladimir Sementsov-Ogievskiy
2022-08-26 13:21 ` [PATCH v11 17/21] job.h: categorize JobDriver callbacks that need the AioContext lock Emanuele Giuseppe Esposito
2022-09-14 14:05   ` Vladimir Sementsov-Ogievskiy
2022-08-26 13:21 ` [PATCH v11 18/21] job.c: enable job lock/unlock and remove Aiocontext locks Emanuele Giuseppe Esposito
2022-09-15 14:52   ` Vladimir Sementsov-Ogievskiy
2022-09-18 17:12     ` Emanuele Giuseppe Esposito
2022-09-22 14:42       ` Emanuele Giuseppe Esposito
2022-09-23 12:00         ` Paolo Bonzini
2022-09-26 12:21       ` Vladimir Sementsov-Ogievskiy
2022-09-27 21:12         ` Paolo Bonzini
2022-08-26 13:21 ` [PATCH v11 19/21] block_job_query: remove atomic read Emanuele Giuseppe Esposito
2022-08-26 13:21 ` [PATCH v11 20/21] blockjob: remove unused functions Emanuele Giuseppe Esposito
2022-09-14 14:14   ` Vladimir Sementsov-Ogievskiy
2022-08-26 13:21 ` [PATCH v11 21/21] job: " Emanuele Giuseppe Esposito
2022-09-14 14:28   ` Vladimir Sementsov-Ogievskiy
2022-09-18 17:22     ` Emanuele Giuseppe Esposito
2022-09-22 14:39       ` 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=4e96d073-f968-bffe-87e8-6e78d3798752@redhat.com \
    --to=eesposit@redhat.com \
    --cc=armbru@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=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    --cc=vsementsov@yandex-team.ru \
    --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).