From nobody Tue Feb 10 01:15:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492690615457109.38936414900047; Thu, 20 Apr 2017 05:16:55 -0700 (PDT) Received: from localhost ([::1]:53623 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1B13-00024y-PR for importer@patchew.org; Thu, 20 Apr 2017 08:16:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38891) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1Amv-0007nb-IE for qemu-devel@nongnu.org; Thu, 20 Apr 2017 08:02:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1Ami-00034b-PS for qemu-devel@nongnu.org; Thu, 20 Apr 2017 08:02:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34540) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d1Alz-0002UW-Ba; Thu, 20 Apr 2017 08:01:19 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2CBA2341563; Thu, 20 Apr 2017 12:01:18 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-9.ams2.redhat.com [10.36.117.9]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v3KC0w42002037; Thu, 20 Apr 2017 08:01:17 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2CBA2341563 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2CBA2341563 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 20 Apr 2017 14:00:53 +0200 Message-Id: <20170420120058.28404-13-pbonzini@redhat.com> In-Reply-To: <20170420120058.28404-1-pbonzini@redhat.com> References: <20170420120058.28404-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 20 Apr 2017 12:01:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 12/17] block: protect tracked_requests and flush_queue with reqs_lock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini --- block.c | 1 + block/io.c | 20 +++++++++++++++++--- include/block/block_int.h | 12 +++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index f1aec36..3b2ed29 100644 --- a/block.c +++ b/block.c @@ -234,6 +234,7 @@ BlockDriverState *bdrv_new(void) QLIST_INIT(&bs->op_blockers[i]); } notifier_with_return_list_init(&bs->before_write_notifiers); + qemu_co_mutex_init(&bs->reqs_lock); bs->refcnt =3D 1; bs->aio_context =3D qemu_get_aio_context(); =20 diff --git a/block/io.c b/block/io.c index d17564b..7af9d47 100644 --- a/block/io.c +++ b/block/io.c @@ -378,8 +378,10 @@ static void tracked_request_end(BdrvTrackedRequest *re= q) atomic_dec(&req->bs->serialising_in_flight); } =20 + qemu_co_mutex_lock(&req->bs->reqs_lock); QLIST_REMOVE(req, list); qemu_co_queue_restart_all(&req->wait_queue); + qemu_co_mutex_unlock(&req->bs->reqs_lock); } =20 /** @@ -404,7 +406,9 @@ static void tracked_request_begin(BdrvTrackedRequest *r= eq, =20 qemu_co_queue_init(&req->wait_queue); =20 + qemu_co_mutex_lock(&bs->reqs_lock); QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); + qemu_co_mutex_unlock(&bs->reqs_lock); } =20 static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t ali= gn) @@ -526,6 +530,7 @@ static bool coroutine_fn wait_serialising_requests(Bdrv= TrackedRequest *self) =20 do { retry =3D false; + qemu_co_mutex_lock(&bs->reqs_lock); QLIST_FOREACH(req, &bs->tracked_requests, list) { if (req =3D=3D self || (!req->serialising && !self->serialisin= g)) { continue; @@ -544,7 +549,7 @@ static bool coroutine_fn wait_serialising_requests(Bdrv= TrackedRequest *self) * (instead of producing a deadlock in the former case). */ if (!req->waiting_for) { self->waiting_for =3D req; - qemu_co_queue_wait(&req->wait_queue, NULL); + qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock); self->waiting_for =3D NULL; retry =3D true; waited =3D true; @@ -552,6 +557,7 @@ static bool coroutine_fn wait_serialising_requests(Bdrv= TrackedRequest *self) } } } + qemu_co_mutex_unlock(&bs->reqs_lock); } while (retry); =20 return waited; @@ -2302,11 +2308,13 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs) current_gen =3D atomic_read(&bs->write_gen); =20 /* Wait until any previous flushes are completed */ + qemu_co_mutex_lock(&bs->reqs_lock); while (bs->active_flush_req) { - qemu_co_queue_wait(&bs->flush_queue, NULL); + qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock); } =20 bs->active_flush_req =3D true; + qemu_co_mutex_unlock(&bs->reqs_lock); =20 /* Write back all layers by calling one driver function */ if (bs->drv->bdrv_co_flush) { @@ -2328,10 +2336,14 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs) goto flush_parent; } =20 - /* Check if we really need to flush anything */ + /* Check if we really need to flush anything + * TODO: use int and atomic access */ + qemu_co_mutex_lock(&bs->reqs_lock); if (bs->flushed_gen =3D=3D current_gen) { + qemu_co_mutex_unlock(&bs->reqs_lock); goto flush_parent; } + qemu_co_mutex_unlock(&bs->reqs_lock); =20 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); if (bs->drv->bdrv_co_flush_to_disk) { @@ -2375,12 +2387,14 @@ flush_parent: ret =3D bs->file ? bdrv_co_flush(bs->file->bs) : 0; out: /* Notify any pending flushes that we have completed */ + qemu_co_mutex_lock(&bs->reqs_lock); if (ret =3D=3D 0) { bs->flushed_gen =3D current_gen; } bs->active_flush_req =3D false; /* Return value is ignored - it's ok if wait queue is empty */ qemu_co_queue_next(&bs->flush_queue); + qemu_co_mutex_unlock(&bs->reqs_lock); =20 early_exit: bdrv_dec_in_flight(bs); diff --git a/include/block/block_int.h b/include/block/block_int.h index 552680c..42b49f5 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -595,11 +595,6 @@ struct BlockDriverState { uint64_t write_threshold_offset; NotifierWithReturn write_threshold_notifier; =20 - QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; - CoQueue flush_queue; /* Serializing flush queue */ - bool active_flush_req; /* Flush request in flight? */ - unsigned int flushed_gen; /* Flushed write generation */ - QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps; =20 /* Offset after the highest byte written to */ @@ -630,6 +625,13 @@ struct BlockDriverState { /* Accessed with atomic ops. */ int quiesce_counter; unsigned int write_gen; /* Current data generation */ + + /* Protected by reqs_lock. */ + QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; + CoQueue flush_queue; /* Serializing flush queue */ + bool active_flush_req; /* Flush request in flight? */ + unsigned int flushed_gen; /* Flushed write generation */ + CoMutex reqs_lock; }; =20 struct BlockBackendRootState { --=20 2.9.3