From: Eric Blake Subject: Re: [Qemu-devel] [PATCH] xen_disk: convert discard input to byte ranges Date: Fri, 18 Nov 2016 10:39:28 -0600 Message-ID: <42ca6186-ca47-3c63-d2e0-54f2ed9f4be7@redhat.com> References: <20161118102452.5779-1-olaf@aepfle.de> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Itx70RPeevGi9ksJl7NgH92C4vA9SRpPf" Return-path: In-Reply-To: <20161118102452.5779-1-olaf@aepfle.de> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-block-bounces+gceqb-qemu-block=m.gmane.org@nongnu.org Sender: "Qemu-block" To: Olaf Hering , qemu-block@nongnu.org Cc: Kevin Wolf , "open list:X86" , "open list:All patches CC here" , Max Reitz , Stefano Stabellini , Anthony Perard List-Id: xen-devel@lists.xenproject.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --Itx70RPeevGi9ksJl7NgH92C4vA9SRpPf Content-Type: multipart/mixed; boundary="eewXubQuBSkTJ6OVOWltdT7DjHASH17s2"; protected-headers="v1" From: Eric Blake To: Olaf Hering , qemu-block@nongnu.org Cc: Kevin Wolf , Stefano Stabellini , "open list:All patches CC here" , Max Reitz , "open list:X86" , Anthony Perard Message-ID: <42ca6186-ca47-3c63-d2e0-54f2ed9f4be7@redhat.com> Subject: Re: [Qemu-devel] [PATCH] xen_disk: convert discard input to byte ranges References: <20161118102452.5779-1-olaf@aepfle.de> In-Reply-To: <20161118102452.5779-1-olaf@aepfle.de> --eewXubQuBSkTJ6OVOWltdT7DjHASH17s2 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 11/18/2016 04:24 AM, Olaf Hering wrote: > 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. >=20 > domU.cfg: > 'vdev=3Dxvda, format=3Dqcow2, backendtype=3Dqdisk, target=3D/x.qcow2'= > domU: > mkfs.ext4 -F /dev/xvda > Discarding device blocks: failed - Input/output error >=20 > Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTO= RS. > Add input range checking to avoid overflow. >=20 > Signed-off-by: Olaf Hering > --- > hw/block/xen_disk.c | 45 +++++++++++++++++++++++++++++++++++++++------= > 1 file changed, 39 insertions(+), 6 deletions(-) >=20 > 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 re= t) > qemu_bh_schedule(ioreq->blkdev->bh); > } > =20 > +static bool blk_split_discard(struct ioreq *ioreq, blkif_sector_t sect= or_number, > + uint64_t nr_sectors) > +{ > + struct XenBlkDev *blkdev =3D ioreq->blkdev; > + int64_t byte_offset; > + int byte_chunk; > + uint64_t sec_start =3D sector_number; > + uint64_t sec_count =3D nr_sectors; > + uint64_t byte_remaining; > + uint64_t limit =3D BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS; [For reference, this limit is the same as rounding INT32_MAX down to the nearest 512-byte limit, or 0x7ffffe00] > + > + /* Wrap around? */ > + if ((sec_start + sec_count) < sec_count) { > + return false; > + } > + /* Overflowing byte limit? */ > + if ((sec_start + sec_count) > ((INT64_MAX + INT_MAX) >> BDRV_SECTO= R_BITS)) { This is undefined. INT64_MAX + anything non-negative overflows int64, and even if you treat overflow as defined by twos-complement representation (which creates a negative number), shifting a negative number is also undefined. If you are trying to detect guests that make a request that would cover more than INT64_MAX bytes, you can simplify. Besides, for as much storage as there is out there, I seriously doubt ANYONE will ever have 2^63 bytes addressable through a single device. Why not just write it as= : if ((INT64_MAX >> BDRV_SECTOR_BITS) - sec_count < sec_start) { > + return false; > + } > + > + byte_offset =3D sec_start << BDRV_SECTOR_BITS; > + byte_remaining =3D sec_count << BDRV_SECTOR_BITS; > + > + do { > + byte_chunk =3D byte_remaining > limit ? limit : byte_remaining= ; > + ioreq->aio_inflight++; > + blk_aio_pdiscard(blkdev->blk, byte_offset, byte_chunk, > + qemu_aio_complete, ioreq); > + byte_remaining -=3D byte_chunk; > + byte_offset +=3D byte_chunk; > + } while (byte_remaining > 0); This part looks reasonable. > + > + return true; > +} > + > static int ioreq_runio_qemu_aio(struct ioreq *ioreq) > { > struct XenBlkDev *blkdev =3D ioreq->blkdev; > @@ -708,12 +743,10 @@ static int ioreq_runio_qemu_aio(struct ioreq *ior= eq) > break; > case BLKIF_OP_DISCARD: > { > - struct blkif_request_discard *discard_req =3D (void *)&ioreq->= req; The old code had it... > - ioreq->aio_inflight++; > - blk_aio_pdiscard(blkdev->blk, > - discard_req->sector_number << BDRV_SECTOR_BIT= S, > - discard_req->nr_sectors << BDRV_SECTOR_BITS, > - qemu_aio_complete, ioreq); > + struct blkif_request_discard *req =3D (void *)&ioreq->req; =2E..but C doesn't require a cast to void*. As long as you are touching this, you could remove the cast (unless I'm missing something, and the cast is also there to cast away const). > + if (!blk_split_discard(ioreq, req->sector_number, req->nr_sect= ors)) { > + goto err; > + } > break; > } > default: >=20 >=20 --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --eewXubQuBSkTJ6OVOWltdT7DjHASH17s2-- --Itx70RPeevGi9ksJl7NgH92C4vA9SRpPf Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJYLy7AAAoJEKeha0olJ0Nqxw0IAImtAby86HiSVY4vZxGvo8cG 7K0dcjltvWkBIc9cuR4me3knFSJ7bOgtW0UmdJAws5JKjJwiHejiVAYk5Y9+dL8u zWOoWFSaH+klTeNLbRCfQUZtSIawVStMSZc9A8psOQgGJlt/QWxSgDAL38hCmnoO fRGgDVh2N9rIGaN1POP7VHn9RXuYlHGvPmrcqzWAVP2BP/Ek6K6NzV4nk8T1Qap5 Iwlpm9rxS96yj4mhetQT1Fn5e101+1iaiaqCTrPvnv/vimF0JKuaDF4wY8EEu1lh wWxDPReiAGzA1R6+KQK2EoIYSKvaGpmVinmJMYoGuBIPH2nXYbQVBt1zpBx+0Bs= =e4f1 -----END PGP SIGNATURE----- --Itx70RPeevGi9ksJl7NgH92C4vA9SRpPf--