From nobody Sat May 4 21:59:20 2024 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=1576675835; cv=none; d=zohomail.com; s=zohoarc; b=buGo52KTA+q2j/kWfFXQqs+FYm3xdNrObzUYMcxZiSHEXeQXPVPCyroa2HgesfL/BP1shOcqtsqsU7plT6tHT8ovD7Ig03ZxZYySA83WWmQtqMsVDjLQUUQwnOFKu9hg+5T3pShR3k94vsEspU0xmlmk4U66QFuENayPQlN2Xag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576675835; 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=LiKPIkxf4u448xcJDFJUScrzTd5CAwun/iplBg2FxZw=; b=CokLQ1812yKC+ueIIfXleRgNDSMscMTBP6Xu8qSw6PSicA9spfP6cnaunr9jQbY7kGLySa8A8HW9rmN+A7b1KI+liIyfiZraU/nA5GnkCtYbfN4SQgcK2DxZLij47kZBnqn5zll6wsHE7v23BsuxgFuULB9tjlGHBrnz/rH1GlA= 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 1576675835461791.0258912969257; Wed, 18 Dec 2019 05:30:35 -0800 (PST) Received: from localhost ([::1]:54540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZPO-0002tl-Bk for importer@patchew.org; Wed, 18 Dec 2019 08:30:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57875) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZCS-0006d8-TJ for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihZCR-0008OE-Ed for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:12 -0500 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:42338) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ihZCR-0008IQ-1y for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:11 -0500 Received: by mail-wr1-x443.google.com with SMTP id q6so2233931wro.9 for ; Wed, 18 Dec 2019 05:17:10 -0800 (PST) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id t1sm2447980wma.43.2019.12.18.05.17.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Dec 2019 05:17:09 -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=LiKPIkxf4u448xcJDFJUScrzTd5CAwun/iplBg2FxZw=; b=pXV6sZgMB0+2Em8h2/ws+/4SdmKHKKp6aztznYIe1KsBilsvLf0hdp9TlqsdE/sgxb A0KLZ9GVLeM8uNGztfWByu1zrIzG5Qk5t5lZwMRoWuAqZDB6idhSdfOPI2UJK7UCcPlr lIqDatk4WGS3QKemSFdzljfK2HkoPS+DY9Fn4ZuzL3bOjuPuWJh2gyxx0hdFKywgLMaN mDx2akzvZNQBa5sV7/Y/IlcCR1klJxZDS0rt40VbBET1/l8aT/guC/F9cQKM5FHJtMqO RQZ0rcoT43Kq2TzHxWE5Y2J6zM+LEp7Gqh4X3705bUkpFWgcmTtyB6m5eh/BsV+LutEK ETTQ== 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=LiKPIkxf4u448xcJDFJUScrzTd5CAwun/iplBg2FxZw=; b=qDbqe9lT7FrdjyNpAxEHyk5AfGOEoGbYn7No9zU0QMNQmNwebqXi+j3dFR8tuZ9Jog v2nYsJe6+ZWoGbeJu0cFngDhH46NhdJY4F3PHnuTmRgkUJA8m58BPSXpD7dkYdwc31/g v8M677KftK0+Mm3+PD+6HP8Vj4PqT9AJpO31cUWxXRu5hIXpEZuQiRLWYWLhbH8QnQx+ wzWiQ8j+CtxvNLs1RYFfRemIFRi56/+PUlkir0BQIlDRBWy/CYt0vOjDb8PKU7VDiHdF t2qvB1MOSwAyuCccTa+lN1mvPxtMJgTafVtdXaWV0/DcKhreYi33q0QK3Pc1CJxuMzss V8Iw== X-Gm-Message-State: APjAAAVKZWrGWjeOE545Pzxn4ol/LHIPwizA141U7X8BnGkjFVHUHWvv ttsiDjbAedR5qkSmDgA5GyFq3OXR X-Google-Smtp-Source: APXvYqxQ62WfkgbO1rWH/lcaLvaoPh7dlOVzGEkJqADVUIQwce61/FukPik6mv9F4p59eE7cdMDZ6A== X-Received: by 2002:a5d:6a02:: with SMTP id m2mr2777061wru.52.1576675029597; Wed, 18 Dec 2019 05:17:09 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 1/3] block: eliminate BDRV_REQ_NO_SERIALISING Date: Wed, 18 Dec 2019 14:17:04 +0100 Message-Id: <1576675026-25046-2-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::443 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" It is unused since commit 00e30f0 ("block/backup: use backup-top instead of write notifiers", 2019-10-01), drop it to simplify the code. While at it, drop redundant assertions on flags. Signed-off-by: Paolo Bonzini --- block/io.c | 18 ++++-------------- include/block/block.h | 12 ------------ 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/block/io.c b/block/io.c index f75777f..b3a67fe 100644 --- a/block/io.c +++ b/block/io.c @@ -1445,8 +1445,7 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChild= *child, * potential fallback support, if we ever implement any read flags * to pass through to drivers. For now, there aren't any * passthrough flags. */ - assert(!(flags & ~(BDRV_REQ_NO_SERIALISING | BDRV_REQ_COPY_ON_READ | - BDRV_REQ_PREFETCH))); + assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH))); =20 /* Handle Copy on Read and associated serialisation */ if (flags & BDRV_REQ_COPY_ON_READ) { @@ -1458,12 +1457,7 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChil= d *child, bdrv_mark_request_serialising(req, bdrv_get_cluster_size(bs)); } =20 - /* BDRV_REQ_SERIALISING is only for write operation */ - assert(!(flags & BDRV_REQ_SERIALISING)); - - if (!(flags & BDRV_REQ_NO_SERIALISING)) { - bdrv_wait_serialising_requests(req); - } + bdrv_wait_serialising_requests(req); =20 if (flags & BDRV_REQ_COPY_ON_READ) { int64_t pnum; @@ -1711,7 +1705,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child, bdrv_inc_in_flight(bs); =20 /* Don't do copy-on-read if we read data before write operation */ - if (atomic_read(&bs->copy_on_read) && !(flags & BDRV_REQ_NO_SERIALISIN= G)) { + if (atomic_read(&bs->copy_on_read)) { flags |=3D BDRV_REQ_COPY_ON_READ; } =20 @@ -1852,8 +1846,6 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t o= ffset, uint64_t bytes, return -EPERM; } =20 - /* BDRV_REQ_NO_SERIALISING is only for read operation */ - assert(!(flags & BDRV_REQ_NO_SERIALISING)); assert(!(bs->open_flags & BDRV_O_INACTIVE)); assert((bs->open_flags & BDRV_O_NO_IO) =3D=3D 0); assert(!(flags & ~BDRV_REQ_MASK)); @@ -3222,9 +3214,7 @@ static int coroutine_fn bdrv_co_copy_range_internal( =20 /* BDRV_REQ_SERIALISING is only for write operation */ assert(!(read_flags & BDRV_REQ_SERIALISING)); - if (!(read_flags & BDRV_REQ_NO_SERIALISING)) { - bdrv_wait_serialising_requests(&req); - } + bdrv_wait_serialising_requests(&req); =20 ret =3D src->bs->drv->bdrv_co_copy_range_from(src->bs, src, src_offset, diff --git a/include/block/block.h b/include/block/block.h index 1df9848..9d43c02 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -51,18 +51,6 @@ typedef enum { */ BDRV_REQ_MAY_UNMAP =3D 0x4, =20 - /* - * The BDRV_REQ_NO_SERIALISING flag is only valid for reads and means = that - * we don't want wait_serialising_requests() during the read operation. - * - * This flag is used for backup copy-on-write operations, when we need= to - * read old data before write (write notifier triggered). It is okay s= ince - * we already waited for other serializing requests in the initiating = write - * (see bdrv_aligned_pwritev), and it is necessary if the initiating w= rite - * is already serializing (without the flag, the read would deadlock - * waiting for the serialising write to complete). - */ - BDRV_REQ_NO_SERIALISING =3D 0x8, BDRV_REQ_FUA =3D 0x10, BDRV_REQ_WRITE_COMPRESSED =3D 0x20, =20 --=20 1.8.3.1 From nobody Sat May 4 21:59:20 2024 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=1576675164; cv=none; d=zohomail.com; s=zohoarc; b=W9tjWmeP+AIbG87jWH7aEdkWrkWWSvtoEkZ13B7p5yyqois+uPckutCXo/IOjrkIR8wmTbyxQfnpyEPefRPQHZstexq/BrG1qQx0yL1U1Ih2tMUSindTX52LN4GgMmFKHh+SlbcDz/puhVun81oRoR6iTVVwvD6SmvEoC3655vU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576675164; 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=PF8I7FZW+7c2/GhkZAcchyjFmjb92r8OfobN2d5ZEso=; b=P7Z+gBoBQmWvAI1ueAuaPJclQSkLiffFFXn0L33f5uiwvTfgeqbcleNVpjnsbAmsK2C8uwvT2NqxrDZ9vj2jaWskAoOUATGh5nRQcV+afZv/Z10XIK0Y3UvjkhtF0pjh6NCh2TRbdTIS6LcwpUMb2z79bABA3ojId2FAKfKgysw= 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 157667516485419.409343881090308; Wed, 18 Dec 2019 05:19:24 -0800 (PST) Received: from localhost ([::1]:54294 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZEY-0000Ij-QE for importer@patchew.org; Wed, 18 Dec 2019 08:19:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58068) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihZCT-0006eP-N7 for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihZCS-0008SS-3k for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:13 -0500 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:42339) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ihZCR-0008N8-Ov for qemu-devel@nongnu.org; Wed, 18 Dec 2019 08:17:11 -0500 Received: by mail-wr1-x444.google.com with SMTP id q6so2233958wro.9 for ; Wed, 18 Dec 2019 05:17:11 -0800 (PST) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id t1sm2447980wma.43.2019.12.18.05.17.09 (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=PF8I7FZW+7c2/GhkZAcchyjFmjb92r8OfobN2d5ZEso=; b=EW0AQ5C1YKJ/ezeaRBPj08rHHhXExVrOBK53W7G+XvKb+Bb7XaM3MBxYx+9lkWEFF6 TTs1qzRSyEOr7tIfHghvab6EFkuIIoTBvnlH+t+ofKGRvyqv3b3uWLGwCGbuvGIw2ls2 G3XrUZEPq5SqxNxKWidAdXzmSFlIyI/MPxZkYL1vrueJU+KYkiCZ8YBEFIOGZhJizuT6 VOn2fPxdtYHvMSk95vpw+1T/xmHH+/DDD12TcBBbT83r4Nc1BIZyf8sl+ZJZtJa7d3Dn 7i8ZMVCKK1Cs0MMrc+vtCz944gF+BJjONhh1VBwjIijPrY2HX5wdkQgszcKL90nyTbbk lOzQ== 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=PF8I7FZW+7c2/GhkZAcchyjFmjb92r8OfobN2d5ZEso=; b=qL5BSWRfXVcqMvtVY6KjqD+dmVkU3yGaCET5FoqSEyTmjLNKWYtrhBtjyw8R4iVsLl fSnJjtA0uMIdL3bs6ImnYVrRQWlw9UUZXFHJ1i2O5FlufkKJW/X+6efqwvna9VodEKAo QYMVTAMoPa2sOP7BzRyAWnA/iZTBgkI529J12yailR++N2i7XeK8I6QnhzK9UmSDXqUK bGmlwZRZPjKY3FQq5fBFnDpVxOJBfOTJsqD0xmgbPIyv2wJdaMk7FTXCrrc9tgDJKDHY IoZBHoh9e95LeO1J79HcJdPrv0rcMuKtK/fzskr1O9wIXDPa49iLeOfLx6Q18ypYxBsJ 3pRg== X-Gm-Message-State: APjAAAWgAF20XNUqnAcaBmrFPjCLTjyKqJoOO7B5ESMtK1lddKo5I9ss VQfLZRB2KYYJ7LM3ZplW6Jel10fh X-Google-Smtp-Source: APXvYqy2T5dzAYCmYEame9xa2bfhwxWRXiIvme4LVmKUsAF4yNyt2hVSw5kVjAdi8+l9aRD/AwOtPg== X-Received: by 2002:a5d:538e:: with SMTP id d14mr2912584wrv.358.1576675030489; Wed, 18 Dec 2019 05:17:10 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 2/3] block/io: wait for serialising requests when a request becomes serialising Date: Wed, 18 Dec 2019 14:17:05 +0100 Message-Id: <1576675026-25046-3-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::444 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" Marking without waiting would not result in actual serialising behavior. Thus, make a call bdrv_mark_request_serialising sufficient for serialisation to happen. Signed-off-by: Paolo Bonzini --- block/file-posix.c | 1 - block/io.c | 39 ++++++++++++++++----------------------- include/block/block_int.h | 3 +-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 1b805bd..2b08b02 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2753,7 +2753,6 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t of= fset, int bytes, req->overlap_bytes =3D req->bytes; =20 bdrv_mark_request_serialising(req, bs->bl.request_alignment); - bdrv_wait_serialising_requests(req); } #endif =20 diff --git a/block/io.c b/block/io.c index b3a67fe..e844c4b 100644 --- a/block/io.c +++ b/block/io.c @@ -41,6 +41,7 @@ #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 @@ -715,7 +716,7 @@ static void tracked_request_begin(BdrvTrackedRequest *r= eq, qemu_co_mutex_unlock(&bs->reqs_lock); } =20 -void bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) +bool bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) { int64_t overlap_offset =3D req->offset & ~(align - 1); uint64_t overlap_bytes =3D ROUND_UP(req->offset + req->bytes, align) @@ -728,18 +729,7 @@ void 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); -} - -static bool is_request_serialising_and_aligned(BdrvTrackedRequest *req) -{ - /* - * If the request is serialising, overlap_offset and overlap_bytes are= set, - * so we can check if the request is aligned. Otherwise, don't care and - * return false. - */ - - return req->serialising && (req->offset =3D=3D req->overlap_offset) && - (req->bytes =3D=3D req->overlap_bytes); + return bdrv_wait_serialising_requests(req); } =20 /** @@ -823,7 +813,7 @@ void bdrv_dec_in_flight(BlockDriverState *bs) bdrv_wakeup(bs); } =20 -bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self) +static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest= *self) { BlockDriverState *bs =3D self->bs; BdrvTrackedRequest *req; @@ -1455,10 +1445,10 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChi= ld *child, * it ensures that the CoR read and write operations are atomic and * guest writes cannot interleave between them. */ bdrv_mark_request_serialising(req, bdrv_get_cluster_size(bs)); + } else { + bdrv_wait_serialising_requests(req); } =20 - bdrv_wait_serialising_requests(req); - if (flags & BDRV_REQ_COPY_ON_READ) { int64_t pnum; =20 @@ -1851,13 +1841,18 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t= offset, uint64_t bytes, assert(!(flags & ~BDRV_REQ_MASK)); =20 if (flags & BDRV_REQ_SERIALISING) { - bdrv_mark_request_serialising(req, bdrv_get_cluster_size(bs)); + waited =3D bdrv_mark_request_serialising(req, bdrv_get_cluster_siz= e(bs)); + /* + * For a misaligned request, we should have already waited + * in bdrv_padding_rmw_read and locked out concurrent writers. + */ + assert(!waited || + (req->offset =3D=3D req->overlap_offset && + req->bytes =3D=3D req->overlap_bytes)); + } else { + bdrv_wait_serialising_requests(req); } =20 - waited =3D bdrv_wait_serialising_requests(req); - - assert(!waited || !req->serialising || - is_request_serialising_and_aligned(req)); assert(req->overlap_offset <=3D offset); assert(offset + bytes <=3D req->overlap_offset + req->overlap_bytes); assert(end_sector <=3D bs->total_sectors || child->perm & BLK_PERM_RES= IZE); @@ -2019,7 +2014,6 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvC= hild *child, padding =3D bdrv_init_padding(bs, offset, bytes, &pad); if (padding) { bdrv_mark_request_serialising(req, align); - bdrv_wait_serialising_requests(req); =20 bdrv_padding_rmw_read(child, req, &pad, true); =20 @@ -2122,7 +2116,6 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *chil= d, =20 if (bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, &pad)) { bdrv_mark_request_serialising(&req, align); - bdrv_wait_serialising_requests(&req); bdrv_padding_rmw_read(child, &req, &pad, false); } =20 diff --git a/include/block/block_int.h b/include/block/block_int.h index dd033d0..640fb82 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -999,8 +999,7 @@ extern unsigned int bdrv_drain_all_count; void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_pare= nt); void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_pa= rent); =20 -bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self); -void bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align= ); +bool coroutine_fn bdrv_mark_request_serialising(BdrvTrackedRequest *req, u= int64_t align); BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState= *bs); =20 int get_tmp_filename(char *filename, int size); --=20 1.8.3.1 From nobody Sat May 4 21:59:20 2024 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