From patchwork Wed Nov 23 10:39:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olaf Hering X-Patchwork-Id: 9442917 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 DCBE7600BA for ; Wed, 23 Nov 2016 10:43:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EC217208C2 for ; Wed, 23 Nov 2016 10:43:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0CF823E64; Wed, 23 Nov 2016 10:43:37 +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 639C7208C2 for ; Wed, 23 Nov 2016 10:43:37 +0000 (UTC) Received: from localhost ([::1]:60828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9V1b-000434-U5 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 23 Nov 2016 05:43:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53273) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9Uxk-0000ub-Uv for qemu-devel@nongnu.org; Wed, 23 Nov 2016 05:39:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c9Uxk-0001q0-1w for qemu-devel@nongnu.org; Wed, 23 Nov 2016 05:39:36 -0500 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::5]:16229) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c9Uxh-0001oN-Ha; Wed, 23 Nov 2016 05:39:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1479897570; l=2775; s=domk; d=aepfle.de; h=Date:Subject:Cc:To:From; bh=aa/w/R+OqzLS16TL71M/cmnDl5PlWmIPtfPWXT3al/o=; b=LEcYXlXdp4nQU3b8EhL9lhmW1ybipLnBekOT8ubZJJAUPCWwtegDpRJjTh8uWWU3Va 9gvZlmifbX4V2E8bUGVXe78lkh3CAfe9pmIYuPl11kkPy8EP9VOdULsgSjirMlnlfoYj V3EgrUyjlQvWNBRgjTh0kMEPPfMPQ/m1MSvxU= X-RZG-AUTH: :P2EQZWCpfu+qG7CngxMFH1J+zrwiavkK6tmQaLfsxs2aAREi5ot4MepIOPj9vPg8rSC0V5ii+kaZklATughyXBQ7Jw== X-RZG-CLASS-ID: mo00 Received: from probook ([2001:a61:10da:a6ff:c75:beb9:f3cc:c893]) by smtp.strato.de (RZmta 39.9 AUTH) with ESMTPSA id z058ccsANAdMIFD (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 11:39:22 +0100 (CET) Received: by probook (Postfix, from userid 1000) id 7791E50B59; Wed, 23 Nov 2016 11:39:21 +0100 (CET) From: Olaf Hering To: qemu-block@nongnu.org Date: Wed, 23 Nov 2016 10:39:12 +0000 Message-Id: <20161123103912.26699-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::5 Subject: [Qemu-devel] [PATCH v3] 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 Reviewed-by: Eric Blake Acked-by: Anthony PERARD Reviewed-by: Stefano Stabellini --- v3: turn tab into spaces to fix checkpatch warning 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..456a2d5 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: