From patchwork Fri Nov 18 10:24:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olaf Hering X-Patchwork-Id: 9436171 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 10B5760469 for ; Fri, 18 Nov 2016 10:25:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EDAFA286AB for ; Fri, 18 Nov 2016 10:25:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DECDA2987E; Fri, 18 Nov 2016 10:25:43 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 CD4DD286AB for ; Fri, 18 Nov 2016 10:25:42 +0000 (UTC) Received: from localhost ([::1]:35616 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7gMY-0007UK-08 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 18 Nov 2016 05:25:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43155) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7gMG-0007Sp-2L for qemu-devel@nongnu.org; Fri, 18 Nov 2016 05:25:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c7gMF-0007cW-4t for qemu-devel@nongnu.org; Fri, 18 Nov 2016 05:25:24 -0500 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::9]:14014) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c7gM8-0007XR-Lf; Fri, 18 Nov 2016 05:25:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1479464714; l=2596; s=domk; d=aepfle.de; h=Date:Subject:Cc:To:From; bh=8mj3OEseC6g6K0gt5jxYZZ5W8EJ6Y8MUB0msVKmLyQY=; b=u+byupDRUgTEIRdHlU2P+Bga1VxoNYlg79nptY7aJYd4prlJGT6p4jNEBYbOpWCkUO L6VhdQMQzhhtlL0qOaapcNOZUUYOc39HhIFtWtxRMpagO7UvlU7kDtF43aYQ6a0uNjfN hoQS0/3jqJRlhlEDSQRL+RnqsBnqKwhfPRiwQ= X-RZG-AUTH: :P2EQZWCpfu+qG7CngxMFH1J+zrwiavkK6tmQaLfvzsjDHvnJrF4x7tmeNvA= X-RZG-CLASS-ID: mo00 Received: from probook (aftr-185-17-205-239.dynamic.mnet-online.de [185.17.205.239]) by smtp.strato.de (RZmta 39.9 DYNA|AUTH) with ESMTPSA id 4043c8sAIAP7g91 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Fri, 18 Nov 2016 11:25:07 +0100 (CET) Received: by probook (Postfix, from userid 1000) id 426BA50B58; Fri, 18 Nov 2016 11:25:07 +0100 (CET) From: Olaf Hering To: qemu-block@nongnu.org Date: Fri, 18 Nov 2016 10:24:52 +0000 Message-Id: <20161118102452.5779-1-olaf@aepfle.de> X-Mailer: git-send-email 2.10.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 2a01:238:20a:202:5300::9 Subject: [Qemu-devel] [PATCH] xen_disk: convert discard input to byte ranges 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: Kevin Wolf , Olaf Hering , Stefano Stabellini , "open list:All patches CC here" , Max Reitz , "open list:X86" , Anthony Perard Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed. domU.cfg: 'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU: mkfs.ext4 -F /dev/xvda Discarding device blocks: failed - Input/output error Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow. Signed-off-by: Olaf Hering --- hw/block/xen_disk.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c index 3a7dc19..c3f572f 100644 --- a/hw/block/xen_disk.c +++ b/hw/block/xen_disk.c @@ -660,6 +660,41 @@ static void qemu_aio_complete(void *opaque, int ret) qemu_bh_schedule(ioreq->blkdev->bh); } +static bool blk_split_discard(struct ioreq *ioreq, blkif_sector_t sector_number, + uint64_t nr_sectors) +{ + struct XenBlkDev *blkdev = ioreq->blkdev; + int64_t byte_offset; + int byte_chunk; + uint64_t sec_start = sector_number; + uint64_t sec_count = nr_sectors; + uint64_t byte_remaining; + uint64_t limit = BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS; + + /* Wrap around? */ + if ((sec_start + sec_count) < sec_count) { + return false; + } + /* Overflowing byte limit? */ + if ((sec_start + sec_count) > ((INT64_MAX + INT_MAX) >> BDRV_SECTOR_BITS)) { + return false; + } + + byte_offset = sec_start << BDRV_SECTOR_BITS; + byte_remaining = sec_count << BDRV_SECTOR_BITS; + + do { + byte_chunk = byte_remaining > limit ? limit : byte_remaining; + ioreq->aio_inflight++; + blk_aio_pdiscard(blkdev->blk, byte_offset, byte_chunk, + qemu_aio_complete, ioreq); + byte_remaining -= byte_chunk; + byte_offset += byte_chunk; + } while (byte_remaining > 0); + + return true; +} + static int ioreq_runio_qemu_aio(struct ioreq *ioreq) { struct XenBlkDev *blkdev = ioreq->blkdev; @@ -708,12 +743,10 @@ static int ioreq_runio_qemu_aio(struct ioreq *ioreq) break; case BLKIF_OP_DISCARD: { - struct blkif_request_discard *discard_req = (void *)&ioreq->req; - ioreq->aio_inflight++; - blk_aio_pdiscard(blkdev->blk, - discard_req->sector_number << BDRV_SECTOR_BITS, - discard_req->nr_sectors << BDRV_SECTOR_BITS, - qemu_aio_complete, ioreq); + struct blkif_request_discard *req = (void *)&ioreq->req; + if (!blk_split_discard(ioreq, req->sector_number, req->nr_sectors)) { + goto err; + } break; } default: