From nobody Wed Nov 5 19:39:01 2025 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 149639178764169.6450993206978; Fri, 2 Jun 2017 01:23:07 -0700 (PDT) Received: from localhost ([::1]:48475 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGhrO-0003Mu-11 for importer@patchew.org; Fri, 02 Jun 2017 04:23:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGhgr-0002Dm-RQ for qemu-devel@nongnu.org; Fri, 02 Jun 2017 04:12:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGhgq-0002TR-9a for qemu-devel@nongnu.org; Fri, 02 Jun 2017 04:12:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51522) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGhgp-0002TB-W3 for qemu-devel@nongnu.org; Fri, 02 Jun 2017 04:12:12 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 06C658123A; Fri, 2 Jun 2017 08:12:11 +0000 (UTC) Received: from lemon.redhat.com (ovpn-12-49.pek2.redhat.com [10.72.12.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6860C60A99; Fri, 2 Jun 2017 08:12:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 06C658123A Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=famz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 06C658123A From: Fam Zheng To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 16:11:15 +0800 Message-Id: <20170602081120.3497-18-famz@redhat.com> In-Reply-To: <20170602081120.3497-1-famz@redhat.com> References: <20170602081120.3497-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 02 Jun 2017 08:12:11 +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] [PULL v2 17/22] 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: peter.maydell@linaro.org, Stefan Hajnoczi 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" From: Paolo Bonzini Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini Message-Id: <20170525163225.29954-14-pbonzini@redhat.com> Signed-off-by: Fam Zheng --- block.c | 1 + block/io.c | 16 ++++++++++++++-- include/block/block_int.h | 14 +++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index 361005c..a5cbd45 100644 --- a/block.c +++ b/block.c @@ -320,6 +320,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 036f5a4..91611ff 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; @@ -2291,14 +2297,17 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs) goto early_exit; } =20 + qemu_co_mutex_lock(&bs->reqs_lock); current_gen =3D atomic_read(&bs->write_gen); =20 /* Wait until any previous flushes are completed */ while (bs->active_flush_req) { - qemu_co_queue_wait(&bs->flush_queue, NULL); + qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock); } =20 + /* Flushes reach this point in nondecreasing current_gen order. */ 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) { @@ -2370,9 +2379,12 @@ out: if (ret =3D=3D 0) { bs->flushed_gen =3D current_gen; } + + qemu_co_mutex_lock(&bs->reqs_lock); 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 8a9bc0b..31fb364 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -609,11 +609,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 */ @@ -647,6 +642,15 @@ struct BlockDriverState { /* Accessed with atomic ops. */ int quiesce_counter; unsigned int write_gen; /* Current data generation */ + + /* Protected by reqs_lock. */ + CoMutex reqs_lock; + QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; + CoQueue flush_queue; /* Serializing flush queue */ + bool active_flush_req; /* Flush request in flight? */ + + /* Only read/written by whoever has set active_flush_req to true. */ + unsigned int flushed_gen; /* Flushed write generation */ }; =20 struct BlockBackendRootState { --=20 2.9.4