From patchwork Wed Nov 23 09:49:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olaf Hering X-Patchwork-Id: 9442861 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 590F46075F for ; Wed, 23 Nov 2016 10:13:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D36D208E3 for ; Wed, 23 Nov 2016 10:13:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21DD823F88; Wed, 23 Nov 2016 10:13:57 +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 5E49F208E3 for ; Wed, 23 Nov 2016 10:13:56 +0000 (UTC) Received: from localhost ([::1]:60700 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9UYt-0000fW-45 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 23 Nov 2016 05:13:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47849) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9UYc-0000e9-8P for qemu-devel@nongnu.org; Wed, 23 Nov 2016 05:13:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c9UYb-0006aC-6L for qemu-devel@nongnu.org; Wed, 23 Nov 2016 05:13:38 -0500 Received: from mo6-p04-ob.smtp.rzone.de ([2a01:238:20a:202:5304::8]:25998) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c9UYV-0006Ww-K8; Wed, 23 Nov 2016 05:13:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1479896009; l=2721; s=domk; d=aepfle.de; h=Date:Subject:Cc:To:From; bh=3Ceq86lPc4I6v58lZWILFtZW20QHAu9D5HPRJKV60qE=; b=W7gaaH5K8rF7LbY9elTljXh1YoxYabpI1qmq33M418xCUSRAA/UTGnzTtK082bsRha FksgiGWs3vM4a4dm/2XIspX/R3ULP3xpICbjNpKhpCt6PO6sNL3kgvGZea1XZYQ71uHu K3DXyxOeJ2TSHk3Pn3arzTdyRG2bkRmdlCiQA= X-RZG-CLASS-ID: mo04 X-RZG-AUTH: :P2EQZWCpfu+qG7CngxMFH1J+zrwiavkK6tmQaLfvzsjDHvnJrF4x7tmeNvA= Received: from probook (aftr-185-17-205-239.dynamic.mnet-online.de [185.17.205.239]) by smtp.strato.de (RZmta 39.9 SBL|AUTH) with ESMTPSA id Q0791fsAN9nRFi8 (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); Wed, 23 Nov 2016 10:49:27 +0100 (CET) Received: by probook (Postfix, from userid 1000) id 182DD50B59; Wed, 23 Nov 2016 10:49:25 +0100 (CET) From: Olaf Hering To: qemu-block@nongnu.org Date: Wed, 23 Nov 2016 09:49:13 +0000 Message-Id: <20161123094914.15675-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:5304::8 Subject: [Qemu-devel] [PATCH v2] xen_disk: split discard input to match internal representation 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. Fixes f313520 ("xen_disk: add discard support") Signed-off-by: Olaf Hering --- v2: adjust overflow check add Fixes revspec because the initial commit also failed to convert u64 to s32 adjust summary hw/block/xen_disk.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c index 3a7dc19..8d74b69 100644 --- a/hw/block/xen_disk.c +++ b/hw/block/xen_disk.c @@ -660,6 +660,38 @@ 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 byte_remaining, limit; + uint64_t sec_start = sector_number; + uint64_t sec_count = nr_sectors; + + /* Wrap around, or overflowing byte limit? */ + if (sec_start + sec_count < sec_count || + sec_start + sec_count > INT64_MAX >> BDRV_SECTOR_BITS) { + return false; + } + + limit = BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS; + 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 +740,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: