From patchwork Wed Dec 11 21:08:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 300466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D88412C0200 for ; Thu, 12 Dec 2013 09:20:33 +1100 (EST) Received: from localhost ([::1]:60253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vqr5q-00043Y-07 for incoming@patchwork.ozlabs.org; Wed, 11 Dec 2013 16:13:18 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vqr1z-0007gS-8j for qemu-devel@nongnu.org; Wed, 11 Dec 2013 16:09:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vqr1t-0000ly-6x for qemu-devel@nongnu.org; Wed, 11 Dec 2013 16:09:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40801) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vqr1s-0000lq-WC for qemu-devel@nongnu.org; Wed, 11 Dec 2013 16:09:13 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rBBL98ga007685 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 11 Dec 2013 16:09:09 -0500 Received: from dhcp-200-207.str.redhat.com (ovpn-116-104.ams2.redhat.com [10.36.116.104]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rBBL8d8x028102; Wed, 11 Dec 2013 16:09:07 -0500 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Wed, 11 Dec 2013 22:08:21 +0100 Message-Id: <1386796109-15264-15-git-send-email-kwolf@redhat.com> In-Reply-To: <1386796109-15264-1-git-send-email-kwolf@redhat.com> References: <1386796109-15264-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, pbonzini@redhat.com, pl@kamp.de, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 14/22] block: Switch BdrvTrackedRequest to byte granularity X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Kevin Wolf --- block.c | 52 +++++++++++++++++++++++++++++++---------------- block/backup.c | 7 ++++++- include/block/block_int.h | 4 ++-- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/block.c b/block.c index a80db2e..fa888d9 100644 --- a/block.c +++ b/block.c @@ -2037,13 +2037,13 @@ static void tracked_request_end(BdrvTrackedRequest *req) */ static void tracked_request_begin(BdrvTrackedRequest *req, BlockDriverState *bs, - int64_t sector_num, - int nb_sectors, bool is_write) + int64_t offset, + unsigned int bytes, bool is_write) { *req = (BdrvTrackedRequest){ .bs = bs, - .sector_num = sector_num, - .nb_sectors = nb_sectors, + .offset = offset, + .bytes = bytes, .is_write = is_write, .co = qemu_coroutine_self(), }; @@ -2074,25 +2074,43 @@ void bdrv_round_to_clusters(BlockDriverState *bs, } } +static void round_bytes_to_clusters(BlockDriverState *bs, + int64_t offset, unsigned int bytes, + int64_t *cluster_offset, + unsigned int *cluster_bytes) +{ + BlockDriverInfo bdi; + + if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { + *cluster_offset = offset; + *cluster_bytes = bytes; + } else { + *cluster_offset = QEMU_ALIGN_DOWN(offset, bdi.cluster_size); + *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes, + bdi.cluster_size); + } +} + static bool tracked_request_overlaps(BdrvTrackedRequest *req, - int64_t sector_num, int nb_sectors) { + int64_t offset, int bytes) +{ /* aaaa bbbb */ - if (sector_num >= req->sector_num + req->nb_sectors) { + if (offset >= req->offset + req->bytes) { return false; } /* bbbb aaaa */ - if (req->sector_num >= sector_num + nb_sectors) { + if (req->offset >= offset + bytes) { return false; } return true; } static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, - int64_t sector_num, int nb_sectors) + int64_t offset, unsigned int bytes) { BdrvTrackedRequest *req; - int64_t cluster_sector_num; - int cluster_nb_sectors; + int64_t cluster_offset; + unsigned int cluster_bytes; bool retry; /* If we touch the same cluster it counts as an overlap. This guarantees @@ -2101,14 +2119,12 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, * CoR read and write operations are atomic and guest writes cannot * interleave between them. */ - bdrv_round_to_clusters(bs, sector_num, nb_sectors, - &cluster_sector_num, &cluster_nb_sectors); + round_bytes_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_bytes); do { retry = false; QLIST_FOREACH(req, &bs->tracked_requests, list) { - if (tracked_request_overlaps(req, cluster_sector_num, - cluster_nb_sectors)) { + if (tracked_request_overlaps(req, cluster_offset, cluster_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. @@ -2723,10 +2739,10 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, } if (bs->copy_on_read_in_flight) { - wait_for_overlapping_requests(bs, sector_num, nb_sectors); + wait_for_overlapping_requests(bs, offset, bytes); } - tracked_request_begin(&req, bs, sector_num, nb_sectors, false); + tracked_request_begin(&req, bs, offset, bytes, false); if (flags & BDRV_REQ_COPY_ON_READ) { int pnum; @@ -2974,10 +2990,10 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs, assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); if (bs->copy_on_read_in_flight) { - wait_for_overlapping_requests(bs, sector_num, nb_sectors); + wait_for_overlapping_requests(bs, offset, bytes); } - tracked_request_begin(&req, bs, sector_num, nb_sectors, true); + tracked_request_begin(&req, bs, offset, bytes, true); ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); diff --git a/block/backup.c b/block/backup.c index 0198514..15a2e55 100644 --- a/block/backup.c +++ b/block/backup.c @@ -181,8 +181,13 @@ static int coroutine_fn backup_before_write_notify( void *opaque) { BdrvTrackedRequest *req = opaque; + int64_t sector_num = req->offset >> BDRV_SECTOR_BITS; + int nb_sectors = req->bytes >> BDRV_SECTOR_BITS; - return backup_do_cow(req->bs, req->sector_num, req->nb_sectors, NULL); + assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0); + assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0); + + return backup_do_cow(req->bs, sector_num, nb_sectors, NULL); } static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp) diff --git a/include/block/block_int.h b/include/block/block_int.h index 0a01b69..a11e5c9 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -57,8 +57,8 @@ typedef struct BdrvTrackedRequest { BlockDriverState *bs; - int64_t sector_num; - int nb_sectors; + int64_t offset; + unsigned int bytes; bool is_write; QLIST_ENTRY(BdrvTrackedRequest) list; Coroutine *co; /* owner, used for deadlock detection */