From patchwork Tue Dec 18 07:57:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 10735029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2F16414E5 for ; Tue, 18 Dec 2018 08:06:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2065C2A23C for ; Tue, 18 Dec 2018 08:06:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14D2E2A5D2; Tue, 18 Dec 2018 08:06:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6436C2A23C for ; Tue, 18 Dec 2018 08:06:00 +0000 (UTC) Received: from localhost ([::1]:52405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZAO7-00059P-6y for patchwork-qemu-devel@patchwork.kernel.org; Tue, 18 Dec 2018 03:05:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47486) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZAG8-0006PE-6r for qemu-devel@nongnu.org; Tue, 18 Dec 2018 02:57:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZAG4-0003dg-PV for qemu-devel@nongnu.org; Tue, 18 Dec 2018 02:57:43 -0500 Received: from mail-ve1eur03on071d.outbound.protection.outlook.com ([2a01:111:f400:fe09::71d]:6167 helo=EUR03-VE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gZAG4-0003cS-9S; Tue, 18 Dec 2018 02:57:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fvc+iED4iWv1ieJRJLBavPDqDVCZcR3e7vVzDa9xwcU=; b=LGss9fbNfD6W5coN9p+sZeD710spJL79wYNAg7WYAvfB6Zgig0UKpAZEvbcunreSWIPFORqp/yh4lpvK9+M8Rw2BtZiyLp2E0Mo2WfGbNH4uYXgkwWNjrr69H7p+d1ggVkLQDdN48p30VIHvBSKXRiYVLOcPjK4AcOzH7zRrTd8= Received: from AM0PR08MB3347.eurprd08.prod.outlook.com (52.134.94.148) by AM0PR08MB4419.eurprd08.prod.outlook.com (20.179.34.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1425.22; Tue, 18 Dec 2018 07:57:38 +0000 Received: from AM0PR08MB3347.eurprd08.prod.outlook.com ([fe80::cceb:42e9:4742:393]) by AM0PR08MB3347.eurprd08.prod.outlook.com ([fe80::cceb:42e9:4742:393%2]) with mapi id 15.20.1425.023; Tue, 18 Dec 2018 07:57:38 +0000 From: Anton Nefedov To: "qemu-devel@nongnu.org" Thread-Topic: [PATCH v11 05/10] block: treat BDRV_REQ_ALLOCATE as serialising Thread-Index: AQHUlqdYKh2pii7CQEWDNg94f2O9+A== Date: Tue, 18 Dec 2018 07:57:38 +0000 Message-ID: <20181218075707.12006-6-anton.nefedov@virtuozzo.com> References: <20181218075707.12006-1-anton.nefedov@virtuozzo.com> In-Reply-To: <20181218075707.12006-1-anton.nefedov@virtuozzo.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: HE1P191CA0007.EURP191.PROD.OUTLOOK.COM (2603:10a6:3:cf::17) To AM0PR08MB3347.eurprd08.prod.outlook.com (2603:10a6:208:5f::20) authentication-results: spf=none (sender IP is ) smtp.mailfrom=anton.nefedov@virtuozzo.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [185.231.240.5] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; AM0PR08MB4419; 20:Ln1XpZcOD9Bgv6g+jMGzRe4jrVj6OrzrvxynurRaaCF8QLIrNY+U+40m5PURKG707ZzsoWOA0pqs3nYanNgCOTiKddMyPFhQtGO0uhbpghdr/Yq37FldCnLVuBxzKUj5R5gnsA20NXInNYwTtHxRebvmxgjPgEoOXPai5sXYMFY= x-ms-office365-filtering-correlation-id: 44b1eeb7-fd1d-48d2-aa62-08d664be7a3e x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(2017052603328)(7153060)(7193020); SRVR:AM0PR08MB4419; x-ms-traffictypediagnostic: AM0PR08MB4419: x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(3230021)(999002)(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(3231475)(944501520)(52105112)(148016)(149066)(150057)(6041310)(20161123558120)(20161123560045)(20161123562045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(201708071742011)(7699051)(76991095); SRVR:AM0PR08MB4419; BCL:0; PCL:0; RULEID:; SRVR:AM0PR08MB4419; x-forefront-prvs: 08902E536D x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(136003)(396003)(39850400004)(346002)(376002)(366004)(199004)(189003)(8676002)(5660300001)(5640700003)(7736002)(8936002)(4326008)(81166006)(81156014)(14454004)(305945005)(53936002)(97736004)(106356001)(2501003)(68736007)(25786009)(2351001)(105586002)(1076003)(478600001)(386003)(6916009)(66066001)(6506007)(486006)(26005)(76176011)(71190400001)(71200400001)(6486002)(102836004)(575784001)(86362001)(186003)(476003)(6512007)(99286004)(3846002)(6116002)(14444005)(54906003)(2906002)(2616005)(256004)(11346002)(52116002)(107886003)(446003)(6436002)(36756003)(44832011)(316002); DIR:OUT; SFP:1102; SCL:1; SRVR:AM0PR08MB4419; H:AM0PR08MB3347.eurprd08.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: fP9rHSM4lVvuPlJZozAR7pOwTN75OE8LdeqtuYnL2FgWfF4FQyN/c5Xex6iDPblgxzZhZ1ubSLJwTj8JIKRfpYDMEpF0HrRmzBSDXA2g9ZJDExSakz4hblv0um1P44xJBo0mG0R5xOnLlNJBT/tZ8+OgOk0QG28Kj98j+CZBefLRZr8/3NZuC9sfqJ1wNgPlYKWXNpts3O+FzIJwMo6Cyz1aj9rGh/oHU1GXZihwoZhN2nDM3Tardn0xBoSSr1GtB/z9VFuvdaSNdaJ7n+LOgjLd0gZ9bTmS6ifuaiW3rHjpsi9+KoPNBQ0sU2KiWB0Z spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 44b1eeb7-fd1d-48d2-aa62-08d664be7a3e X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Dec 2018 07:57:38.1369 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB4419 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 2a01:111:f400:fe09::71d Subject: [Qemu-devel] [PATCH v11 05/10] block: treat BDRV_REQ_ALLOCATE as serialising 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: "kwolf@redhat.com" , Vladimir Sementsov-Ogievskiy , "berto@igalia.com" , Denis Lunev , "qemu-block@nongnu.org" , "mreitz@redhat.com" , Anton Nefedov Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The idea is that ALLOCATE requests may overlap with other requests. Reuse the existing block layer infrastructure for serialising requests. Use the following approach: - mark ALLOCATE also SERIALISING, so subsequent requests to the area wait - ALLOCATE request itself must never wait if another request is in flight already. Return EAGAIN, let the caller reconsider. Signed-off-by: Anton Nefedov Reviewed-by: Alberto Garcia --- include/block/block.h | 3 +++ block/io.c | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 643d32f4b8..dfc0fc1b8f 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -88,6 +88,9 @@ typedef enum { * efficiently allocate the space so it reads as zeroes, or return an error. * If this flag is set then BDRV_REQ_ZERO_WRITE must also be set. * This flag cannot be set together with BDRV_REQ_MAY_UNMAP. + * This flag implicitly sets BDRV_REQ_SERIALISING meaning it is protected + * from conflicts with overlapping requests. If such conflict is detected, + * -EAGAIN is returned. */ BDRV_REQ_ALLOCATE = 0x100, diff --git a/block/io.c b/block/io.c index 66006a089d..4451714a60 100644 --- a/block/io.c +++ b/block/io.c @@ -720,12 +720,13 @@ void bdrv_dec_in_flight(BlockDriverState *bs) bdrv_wakeup(bs); } -static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) +static bool coroutine_fn find_or_wait_serialising_requests( + BdrvTrackedRequest *self, bool wait) { BlockDriverState *bs = self->bs; BdrvTrackedRequest *req; bool retry; - bool waited = false; + bool found = false; if (!atomic_read(&bs->serialising_in_flight)) { return false; @@ -751,11 +752,14 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) * 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) { + found = true; + if (!wait) { + break; + } self->waiting_for = req; qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock); self->waiting_for = NULL; retry = true; - waited = true; break; } } @@ -763,7 +767,12 @@ static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) qemu_co_mutex_unlock(&bs->reqs_lock); } while (retry); - return waited; + return found; +} + +static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) +{ + return find_or_wait_serialising_requests(self, true); } static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, @@ -1585,7 +1594,7 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes, BdrvTrackedRequest *req, int flags) { BlockDriverState *bs = child->bs; - bool waited; + bool found; int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE); if (bs->read_only) { @@ -1602,9 +1611,13 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes, mark_request_serialising(req, bdrv_get_cluster_size(bs)); } - waited = wait_serialising_requests(req); + found = find_or_wait_serialising_requests(req, + !(flags & BDRV_REQ_ALLOCATE)); + if (found && (flags & BDRV_REQ_ALLOCATE)) { + return -EAGAIN; + } - assert(!waited || !req->serialising || + assert(!found || !req->serialising || is_request_serialising_and_aligned(req)); assert(req->overlap_offset <= offset); assert(offset + bytes <= req->overlap_offset + req->overlap_bytes); @@ -1864,6 +1877,10 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child, assert(!((flags & BDRV_REQ_ALLOCATE) && (flags & BDRV_REQ_MAY_UNMAP))); assert(!((flags & BDRV_REQ_ALLOCATE) && !(flags & BDRV_REQ_ZERO_WRITE))); + if (flags & BDRV_REQ_ALLOCATE) { + flags |= BDRV_REQ_SERIALISING; + } + trace_bdrv_co_pwritev(child->bs, offset, bytes, flags); if (!bs->drv) {