From nobody Fri Apr 11 23:11:42 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1665144055; cv=none; d=zohomail.com; s=zohoarc; b=dVVLcHSZhTP0ZaHiwr41hFY8flZh7fSxUm+JgsvgZYYOx13lNdG3ahy95B0gEBJxc0IbkST0iO3jqBEVFWNQCwhFXRxTSQW/yWm2zc6pSbf03pJgAEIg7L69wizSRqwxF0zlTAQ0KGMkBFbRCI3VRUrWiIGK+MDlds1i+MiUPwY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665144055; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ua0jko/6n4cZlaBamjrAI8+x0sWv7ho1aBCyiAgf6Sc=; b=J82pSbsSoncMO6PiE21FAsIs99zd3Dl8D7maAXGTV1zhkISWUqWOhGORni/coWqK4tKZi7rzzNtK2B9uNkAv4Ik+txyB1RpsKNnWS1tOXZHYDuTDI8p9ZDu0Pnb1BSCvOy+UWRnMx4mbXrqFwej+EYzkKC4NDIj+KDnc7ALra2k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1665144055207751.8060907958543; Fri, 7 Oct 2022 05:00:55 -0700 (PDT) Received: from localhost ([::1]:45650 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ogm29-0004kl-7c for importer@patchew.org; Fri, 07 Oct 2022 08:00:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38214) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogkuM-0005u9-0I for qemu-devel@nongnu.org; Fri, 07 Oct 2022 06:48:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:46611) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogkuJ-0003I5-Bo for qemu-devel@nongnu.org; Fri, 07 Oct 2022 06:48:45 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-597-CLhz4oDRMHasQazBoJk_cQ-1; Fri, 07 Oct 2022 06:48:39 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4D151185A78B; Fri, 7 Oct 2022 10:48:39 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.192.109]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8B6FC2028DC1; Fri, 7 Oct 2022 10:48:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665139722; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ua0jko/6n4cZlaBamjrAI8+x0sWv7ho1aBCyiAgf6Sc=; b=D07+yUqsCiCko+xCx5lm+JttPlU8+3INqh1p8+w4OGC6AbDESkKl1biV7olfh26UH3hQ2t IxQkYjuQT0i/tyg18cTK5JWav8R5vowV0YgBtlAL+VRGApqG06cC+Jh6waqOfwuQyz7zVn K4web59OkDgI074hoSv0GBh0DYCn0DQ= X-MC-Unique: CLhz4oDRMHasQazBoJk_cQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, qemu-devel@nongnu.org Subject: [PULL 39/50] jobs: group together API calls under the same job lock Date: Fri, 7 Oct 2022 12:47:41 +0200 Message-Id: <20221007104752.141361-40-kwolf@redhat.com> In-Reply-To: <20221007104752.141361-1-kwolf@redhat.com> References: <20221007104752.141361-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1665144057011100001 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito Now that the API offers also _locked() functions, take advantage of it and give also the caller control to take the lock and call _locked functions. This makes sense especially when we have for loops, because it makes no sense to have: for(job =3D job_next(); ...) where each job_next() takes the lock internally. Instead we want JOB_LOCK_GUARD(); for(job =3D job_next_locked(); ...) In addition, protect also direct field accesses, by either creating a new critical section or widening the existing ones. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20220926093214.506243-12-eesposit@redhat.com> Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- block.c | 17 ++++++++++------- blockdev.c | 14 ++++++++++---- blockjob.c | 35 ++++++++++++++++++++++------------- job-qmp.c | 9 ++++++--- monitor/qmp-cmds.c | 7 +++++-- qemu-img.c | 15 ++++++++++----- 6 files changed, 63 insertions(+), 34 deletions(-) diff --git a/block.c b/block.c index 9b0fae5c1e..1fbf6b9e69 100644 --- a/block.c +++ b/block.c @@ -4981,8 +4981,8 @@ static void bdrv_close(BlockDriverState *bs) =20 void bdrv_close_all(void) { - assert(job_next(NULL) =3D=3D NULL); GLOBAL_STATE_CODE(); + assert(job_next(NULL) =3D=3D NULL); =20 /* Drop references from requests still in flight, such as canceled blo= ck * jobs whose AIO context has not been polled yet */ @@ -6168,13 +6168,16 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **e= rrp) } } =20 - for (job =3D block_job_next(NULL); job; job =3D block_job_next(job)) { - GSList *el; + WITH_JOB_LOCK_GUARD() { + for (job =3D block_job_next_locked(NULL); job; + job =3D block_job_next_locked(job)) { + GSList *el; =20 - xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, - job->job.id); - for (el =3D job->nodes; el; el =3D el->next) { - xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); + xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK= _JOB, + job->job.id); + for (el =3D job->nodes; el; el =3D el->next) { + xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); + } } } =20 diff --git a/blockdev.c b/blockdev.c index 2e941e2979..46090bb0aa 100644 --- a/blockdev.c +++ b/blockdev.c @@ -150,12 +150,15 @@ void blockdev_mark_auto_del(BlockBackend *blk) return; } =20 - for (job =3D block_job_next(NULL); job; job =3D block_job_next(job)) { + JOB_LOCK_GUARD(); + + for (job =3D block_job_next_locked(NULL); job; + job =3D block_job_next_locked(job)) { if (block_job_has_bdrv(job, blk_bs(blk))) { AioContext *aio_context =3D job->job.aio_context; aio_context_acquire(aio_context); =20 - job_cancel(&job->job, false); + job_cancel_locked(&job->job, false); =20 aio_context_release(aio_context); } @@ -3756,7 +3759,10 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) BlockJobInfoList *head =3D NULL, **tail =3D &head; BlockJob *job; =20 - for (job =3D block_job_next(NULL); job; job =3D block_job_next(job)) { + JOB_LOCK_GUARD(); + + for (job =3D block_job_next_locked(NULL); job; + job =3D block_job_next_locked(job)) { BlockJobInfo *value; AioContext *aio_context; =20 @@ -3765,7 +3771,7 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) } aio_context =3D block_job_get_aio_context(job); aio_context_acquire(aio_context); - value =3D block_job_query(job, errp); + value =3D block_job_query_locked(job, errp); aio_context_release(aio_context); if (!value) { qapi_free_BlockJobInfoList(head); diff --git a/blockjob.c b/blockjob.c index 0d59aba439..96fb9d9f73 100644 --- a/blockjob.c +++ b/blockjob.c @@ -111,8 +111,10 @@ static bool child_job_drained_poll(BdrvChild *c) /* An inactive or completed job doesn't have any pending requests. Jobs * with !job->busy are either already paused or have a pause point aft= er * being reentered, so no job driver code will run before they pause. = */ - if (!job->busy || job_is_completed(job)) { - return false; + WITH_JOB_LOCK_GUARD() { + if (!job->busy || job_is_completed_locked(job)) { + return false; + } } =20 /* Otherwise, assume that it isn't fully stopped yet, but allow the jo= b to @@ -475,13 +477,15 @@ void *block_job_create(const char *job_id, const Bloc= kJobDriver *driver, job->ready_notifier.notify =3D block_job_event_ready; job->idle_notifier.notify =3D block_job_on_idle; =20 - notifier_list_add(&job->job.on_finalize_cancelled, - &job->finalize_cancelled_notifier); - notifier_list_add(&job->job.on_finalize_completed, - &job->finalize_completed_notifier); - notifier_list_add(&job->job.on_pending, &job->pending_notifier); - notifier_list_add(&job->job.on_ready, &job->ready_notifier); - notifier_list_add(&job->job.on_idle, &job->idle_notifier); + WITH_JOB_LOCK_GUARD() { + notifier_list_add(&job->job.on_finalize_cancelled, + &job->finalize_cancelled_notifier); + notifier_list_add(&job->job.on_finalize_completed, + &job->finalize_completed_notifier); + notifier_list_add(&job->job.on_pending, &job->pending_notifier); + notifier_list_add(&job->job.on_ready, &job->ready_notifier); + notifier_list_add(&job->job.on_idle, &job->idle_notifier); + } =20 error_setg(&job->blocker, "block device is in use by block job: %s", job_type_str(&job->job)); @@ -558,10 +562,15 @@ BlockErrorAction block_job_error_action(BlockJob *job= , BlockdevOnError on_err, action); } if (action =3D=3D BLOCK_ERROR_ACTION_STOP) { - if (!job->job.user_paused) { - job_pause(&job->job); - /* make the pause user visible, which will be resumed from QMP= . */ - job->job.user_paused =3D true; + WITH_JOB_LOCK_GUARD() { + if (!job->job.user_paused) { + job_pause_locked(&job->job); + /* + * make the pause user visible, which will be + * resumed from QMP. + */ + job->job.user_paused =3D true; + } } block_job_iostatus_set_err(job, error); } diff --git a/job-qmp.c b/job-qmp.c index b1c456482a..393d3a5b81 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -164,7 +164,8 @@ void qmp_job_dismiss(const char *id, Error **errp) aio_context_release(aio_context); } =20 -static JobInfo *job_query_single(Job *job, Error **errp) +/* Called with job_mutex held. */ +static JobInfo *job_query_single_locked(Job *job, Error **errp) { JobInfo *info; uint64_t progress_current; @@ -194,7 +195,9 @@ JobInfoList *qmp_query_jobs(Error **errp) JobInfoList *head =3D NULL, **tail =3D &head; Job *job; =20 - for (job =3D job_next(NULL); job; job =3D job_next(job)) { + JOB_LOCK_GUARD(); + + for (job =3D job_next_locked(NULL); job; job =3D job_next_locked(job))= { JobInfo *value; AioContext *aio_context; =20 @@ -203,7 +206,7 @@ JobInfoList *qmp_query_jobs(Error **errp) } aio_context =3D job->aio_context; aio_context_acquire(aio_context); - value =3D job_query_single(job, errp); + value =3D job_query_single_locked(job, errp); aio_context_release(aio_context); if (!value) { qapi_free_JobInfoList(head); diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index 7314cd813d..81c8fdadf8 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -135,8 +135,11 @@ void qmp_cont(Error **errp) blk_iostatus_reset(blk); } =20 - for (job =3D block_job_next(NULL); job; job =3D block_job_next(job)) { - block_job_iostatus_reset(job); + WITH_JOB_LOCK_GUARD() { + for (job =3D block_job_next_locked(NULL); job; + job =3D block_job_next_locked(job)) { + block_job_iostatus_reset_locked(job); + } } =20 /* Continuing after completed migration. Images have been inactivated = to diff --git a/qemu-img.c b/qemu-img.c index cab9776f42..e0a30b1f4c 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -912,9 +912,11 @@ static void run_block_job(BlockJob *job, Error **errp) int ret =3D 0; =20 aio_context_acquire(aio_context); - job_ref(&job->job); + job_lock(); + job_ref_locked(&job->job); do { float progress =3D 0.0f; + job_unlock(); aio_poll(aio_context, true); =20 progress_get_snapshot(&job->job.progress, &progress_current, @@ -923,14 +925,17 @@ static void run_block_job(BlockJob *job, Error **errp) progress =3D (float)progress_current / progress_total * 100.f; } qemu_progress_print(progress, 0); - } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); + job_lock(); + } while (!job_is_ready_locked(&job->job) && + !job_is_completed_locked(&job->job)); =20 - if (!job_is_completed(&job->job)) { - ret =3D job_complete_sync(&job->job, errp); + if (!job_is_completed_locked(&job->job)) { + ret =3D job_complete_sync_locked(&job->job, errp); } else { ret =3D job->job.ret; } - job_unref(&job->job); + job_unref_locked(&job->job); + job_unlock(); aio_context_release(aio_context); =20 /* publish completion progress only when success */ --=20 2.37.3