From nobody Mon Feb 9 20:46:00 2026 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1576675328; cv=none; d=zohomail.com; s=zohoarc; b=TVbLcBe1ygpsyj9VXFbUg18b00HWqkI9tIQQ82d4PzwlkPvHr4gNprS/O8ourBD7IPSrVA1/HB8myzy/XnH++3dxif/NO5kqQY4Z4CxRTrsCBgG7JBSK53qBP0kMX8YPJ7hNC6WSQWtw4SFbqCm15hSiDYwj8yz0oWBIbDNIqpc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576675328; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=EIhNMqQ5gIhltsUn6hlrMExKL0eOLwZAeR0KE+AGHEo=; b=Z2w2REv0V4Qva2fI+d782jjdOqnJYTkCqJeOPSsPjn3DUitmiitKWuqHkj13vMcGZeiIYW7u8T7gGFMn0E6+9Ms/qNjo9113IzI0ArB396hCJEEMbMrGPLYolL8kiSj7DTzxzrnqibnOggm+hJSf5tGqnvTxmWTP5QideWejnIQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1576675328521598.9944785593522; Wed, 18 Dec 2019 05:22:08 -0800 (PST) Received: from localhost ([::1]:54372 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZHD-0003xI-2A for importer@patchew.org; Wed, 18 Dec 2019 08:22:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58232) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZCU-0006fT-KV for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihZCT-00006Z-1z for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:14 -0500 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:36799) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ihZCS-0008Sv-Ox for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:12 -0500 Received: by mail-wr1-x441.google.com with SMTP id z3so2280141wru.3 for ; Wed, 18 Dec 2019 05:17:12 -0800 (PST) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id t1sm2447980wma.43.2019.12.18.05.17.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Dec 2019 05:17:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=EIhNMqQ5gIhltsUn6hlrMExKL0eOLwZAeR0KE+AGHEo=; b=Q71I9QA3mK0w5OTJgEjIh/TQWWPkIIlNjlGAW6KCUpOSaI5DWkId7KDITA30rm2/cL Ni3Dh0IcwvaZ2/nDFl1CGh98CKz6yzQ+h016yRLpkmYMYC/tixlvUZHMRP7GdEaAq2w7 h35pnGJW7KB/dfFnvJH1qr3H0AnXpKt/x9tCNvNZckNDsZyKa0Cge5k4lXoY9uSSAcZQ 1V3yXmhyNSiqGJbB/WKfYAhslv2rfWq7vIFFOTc3SfIxsYhnocISntLGKLsDGpEWQbBV 2+ppCW8zsmbcaZJuDncKi1xqgqdYM6xV9CNIA9P03Svo1WDr7LLIuRSDjRumJ3pxXqbe 5W8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=EIhNMqQ5gIhltsUn6hlrMExKL0eOLwZAeR0KE+AGHEo=; b=PGRb7tQDXPK9/uhsMJza5efNiqCquxVFeQWkcz9qrEK8/ByGeHENlsc9Fnm/nErFCn 84BF0ARiyMpNtQJg5TTye5Ox2WPzV9nE81w4mDqPs7+e6jVBIEZDvpRTRY4OxznCFX06 UViQ+cpUVBUjN2hYoYJaf+8A/yxL2oBfLqogXQKtOJR6wl633v2tRrjHt7nvBDKhzzy+ JylHr86uonlYGjfLGyTRaD2uCF1YuzvzKtRFb4JTlcTWSxEBLqpuNbL+ltaJFP2wH4dG XNu1TS+p0REPnt3fO8LDMg4XKXC95yBTKRs8cCgP7rgds/pv5mwvgAXPJIcZ17yMl51Q 5nlQ== X-Gm-Message-State: APjAAAVLh92R3ZeQdXw4NUdC4tvIX+APZMU3j9ErIJwKlIW9jX5EdOfB JdwyfAxR7uWFqQw5VKTtn0W0/Hu3 X-Google-Smtp-Source: APXvYqyHf+nGkOYWtQ72z2/HE1SZWKyot7lOEN0x3LiH0Gj8ymZ4Kp9s4Cwt0LFECBxX+bkl/dXerA== X-Received: by 2002:a5d:68cf:: with SMTP id p15mr2872485wrw.31.1576675031543; Wed, 18 Dec 2019 05:17:11 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 3/3] block/io: take bs->reqs_lock in bdrv_mark_request_serialising Date: Wed, 18 Dec 2019 14:17:06 +0100 Message-Id: <1576675026-25046-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1576675026-25046-1-git-send-email-pbonzini@redhat.com> References: <1576675026-25046-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Peter Lieven , qemu-block@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" bdrv_mark_request_serialising is writing the overlap_offset and overlap_bytes fields of BdrvTrackedRequest. Take bs->reqs_lock for the whole direction of it, and not just when waiting for serialising requests, so that tracked_request_overlaps does not look at a half-updated request. The new code does not unlock/relock around retries. This is unnecessary because a retry is always preceded by a CoQueue wait, which already releases and reacquired bs->reqs_lock. Reported-by: Peter Lieven Signed-off-by: Paolo Bonzini Reviewed-by: Kevin Wolf --- block/io.c | 112 ++++++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 63 insertions(+), 49 deletions(-) diff --git a/block/io.c b/block/io.c index e844c4b..166c2c1 100644 --- a/block/io.c +++ b/block/io.c @@ -41,7 +41,6 @@ #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS) =20 static void bdrv_parent_cb_resize(BlockDriverState *bs); -static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest= *self); static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, BdrvRequestFlags flags); =20 @@ -716,12 +715,69 @@ static void tracked_request_begin(BdrvTrackedRequest = *req, qemu_co_mutex_unlock(&bs->reqs_lock); } =20 +static bool tracked_request_overlaps(BdrvTrackedRequest *req, + int64_t offset, uint64_t bytes) +{ + /* aaaa bbbb */ + if (offset >=3D req->overlap_offset + req->overlap_bytes) { + return false; + } + /* bbbb aaaa */ + if (req->overlap_offset >=3D offset + bytes) { + return false; + } + return true; +} + +static bool coroutine_fn +bdrv_wait_serialising_requests_locked(BlockDriverState *bs, + BdrvTrackedRequest *self) +{ + BdrvTrackedRequest *req; + bool retry; + bool waited =3D false; + + do { + retry =3D false; + QLIST_FOREACH(req, &bs->tracked_requests, list) { + if (req =3D=3D self || (!req->serialising && !self->serialisin= g)) { + continue; + } + if (tracked_request_overlaps(req, self->overlap_offset, + self->overlap_bytes)) + { + /* Hitting this means there was a reentrant request, for + * example, a block driver issuing nested requests. This = must + * never happen since it means deadlock. + */ + assert(qemu_coroutine_self() !=3D req->co); + + /* If the request is already (indirectly) waiting for us, = or + * will wait for us as soon as it wakes up, then just go on + * (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, &bs->reqs_lock); + self->waiting_for =3D NULL; + retry =3D true; + waited =3D true; + break; + } + } + } + } while (retry); + return waited; +} + bool bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) { + BlockDriverState *bs =3D req->bs; int64_t overlap_offset =3D req->offset & ~(align - 1); uint64_t overlap_bytes =3D ROUND_UP(req->offset + req->bytes, align) - overlap_offset; + bool waited; =20 + qemu_co_mutex_lock(&bs->reqs_lock); if (!req->serialising) { atomic_inc(&req->bs->serialising_in_flight); req->serialising =3D true; @@ -729,7 +785,9 @@ bool bdrv_mark_request_serialising(BdrvTrackedRequest *= req, uint64_t align) =20 req->overlap_offset =3D MIN(req->overlap_offset, overlap_offset); req->overlap_bytes =3D MAX(req->overlap_bytes, overlap_bytes); - return bdrv_wait_serialising_requests(req); + waited =3D bdrv_wait_serialising_requests_locked(bs, req); + qemu_co_mutex_unlock(&bs->reqs_lock); + return waited; } =20 /** @@ -783,20 +841,6 @@ static int bdrv_get_cluster_size(BlockDriverState *bs) } } =20 -static bool tracked_request_overlaps(BdrvTrackedRequest *req, - int64_t offset, uint64_t bytes) -{ - /* aaaa bbbb */ - if (offset >=3D req->overlap_offset + req->overlap_bytes) { - return false; - } - /* bbbb aaaa */ - if (req->overlap_offset >=3D offset + bytes) { - return false; - } - return true; -} - void bdrv_inc_in_flight(BlockDriverState *bs) { atomic_inc(&bs->in_flight); @@ -816,45 +860,15 @@ void bdrv_dec_in_flight(BlockDriverState *bs) static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest= *self) { BlockDriverState *bs =3D self->bs; - BdrvTrackedRequest *req; - bool retry; bool waited =3D false; =20 if (!atomic_read(&bs->serialising_in_flight)) { return false; } =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; - } - if (tracked_request_overlaps(req, self->overlap_offset, - self->overlap_bytes)) - { - /* Hitting this means there was a reentrant request, for - * example, a block driver issuing nested requests. This = must - * never happen since it means deadlock. - */ - assert(qemu_coroutine_self() !=3D req->co); - - /* If the request is already (indirectly) waiting for us, = or - * will wait for us as soon as it wakes up, then just go on - * (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, &bs->reqs_lock); - self->waiting_for =3D NULL; - retry =3D true; - waited =3D true; - break; - } - } - } - qemu_co_mutex_unlock(&bs->reqs_lock); - } while (retry); + qemu_co_mutex_lock(&bs->reqs_lock); + waited =3D bdrv_wait_serialising_requests_locked(bs, self); + qemu_co_mutex_unlock(&bs->reqs_lock); =20 return waited; } --=20 1.8.3.1