Received: from eggs.gnu.org ([2001:4830:134:3::10]:42766) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7V8o-0005nv-4P for qemu-devel@nongnu.org; Thu, 17 Nov 2016 17:26:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c7V8m-00021F-RB for qemu-devel@nongnu.org; Thu, 17 Nov 2016 17:26:46 -0500 References: <1479413642-22463-1-git-send-email-eblake@redhat.com> <1479413642-22463-6-git-send-email-eblake@redhat.com> From: Max Reitz Message-ID: <0c583cb9-0dbf-a43b-ba47-25f51a1b3232@redhat.com> Date: Thu, 17 Nov 2016 23:26:34 +0100 MIME-Version: 1.0 In-Reply-To: <1479413642-22463-6-git-send-email-eblake@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="r7nUBkEKH2Ne77fmTIuUSn7eIf7rboJ9Q" Subject: Re: [Qemu-devel] [PATCH v2 5/9] block: Pass unaligned discard requests to drivers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, kwolf@redhat.com, pbonzini@redhat.com, qemu-stable@nongnu.org, Stefan Hajnoczi , Fam Zheng This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --r7nUBkEKH2Ne77fmTIuUSn7eIf7rboJ9Q From: Max Reitz To: Eric Blake , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, kwolf@redhat.com, pbonzini@redhat.com, qemu-stable@nongnu.org, Stefan Hajnoczi , Fam Zheng Message-ID: <0c583cb9-0dbf-a43b-ba47-25f51a1b3232@redhat.com> Subject: Re: [PATCH v2 5/9] block: Pass unaligned discard requests to drivers References: <1479413642-22463-1-git-send-email-eblake@redhat.com> <1479413642-22463-6-git-send-email-eblake@redhat.com> In-Reply-To: <1479413642-22463-6-git-send-email-eblake@redhat.com> Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable On 17.11.2016 21:13, Eric Blake wrote: > Discard is advisory, so rounding the requests to alignment > boundaries is never semantically wrong from the data that > the guest sees. But at least the Dell Equallogic iSCSI SANs > has an interesting property that its advertised discard > alignment is 15M, yet documents that discarding a sequence > of 1M slices will eventually result in the 15M page being > marked as discarded, and it is possible to observe which > pages have been discarded. >=20 > Between commits 9f1963b and b8d0a980, we converted the block > layer to a byte-based interface that ultimately ignores any > unaligned head or tail based on the driver's advertised > discard granularity, which means that qemu 2.7 refuses to > pass any discard request smaller than 15M down to the Dell > Equallogic hardware. This is a slight regression in behavior > compared to earlier qemu, where a guest executing discards > in power-of-2 chunks used to be able to get every page > discarded, but is now left with various pages still allocated > because the guest requests did not align with the hardware's > 15M pages. >=20 > Since the SCSI specification says nothing about a minimum > discard granularity, and only documents the preferred > alignment, it is best if the block layer gives the driver > every bit of information about discard requests, rather than > rounding it to alignment boundaries early. >=20 > Rework the block layer discard algorithm to mirror the write > zero algorithm: always peel off any unaligned head or tail > and manage that in isolation, then do the bulk of the request > on an aligned boundary. The fallback when the driver returns > -ENOTSUP for an unaligned request is to silently ignore that > portion of the discard request; but for devices that can pass > the partial request all the way down to hardware, this can > result in the hardware coalescing requests and discarding > aligned pages after all. >=20 > Reported by: Peter Lieven > CC: qemu-stable@nongnu.org > Signed-off-by: Eric Blake >=20 > --- > v2: Split at more boundaries. > --- > block/io.c | 45 ++++++++++++++++++++++++++++++++------------- > 1 file changed, 32 insertions(+), 13 deletions(-) >=20 > diff --git a/block/io.c b/block/io.c > index 085ac34..4f00562 100644 > --- a/block/io.c > +++ b/block/io.c > @@ -2424,7 +2424,7 @@ int coroutine_fn bdrv_co_pdiscard(BlockDriverStat= e *bs, int64_t offset, > { > BdrvTrackedRequest req; > int max_pdiscard, ret; > - int head, align; > + int head, tail, align; >=20 > if (!bs->drv) { > return -ENOMEDIUM; > @@ -2447,19 +2447,15 @@ int coroutine_fn bdrv_co_pdiscard(BlockDriverSt= ate *bs, int64_t offset, > return 0; > } >=20 > - /* Discard is advisory, so ignore any unaligned head or tail */ > + /* Discard is advisory, but some devices track and coalesce > + * unaligned requests, so we must pass everything down rather than= > + * round here. Still, most devices will just silently ignore > + * unaligned requests (by returning -ENOTSUP), so we must fragment= Returning -ENOTSUP is not quite "silently" in my book. > + * the request accordingly. */ > align =3D MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment)= ; > assert(align % bs->bl.request_alignment =3D=3D 0); > head =3D offset % align; > - if (head) { > - head =3D MIN(count, align - head); > - count -=3D head; > - offset +=3D head; > - } > - count =3D QEMU_ALIGN_DOWN(count, align); > - if (!count) { > - return 0; > - } > + tail =3D (offset + count) % align; >=20 > bdrv_inc_in_flight(bs); > tracked_request_begin(&req, bs, offset, count, BDRV_TRACKED_DISCAR= D); > @@ -2471,11 +2467,34 @@ int coroutine_fn bdrv_co_pdiscard(BlockDriverSt= ate *bs, int64_t offset, >=20 > max_pdiscard =3D QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard,= INT_MAX), > align); > - assert(max_pdiscard); > + assert(max_pdiscard >=3D bs->bl.request_alignment); >=20 > while (count > 0) { > int ret; > - int num =3D MIN(count, max_pdiscard); > + int num =3D count; > + > + if (head) { > + /* Make small requests to get to alignment boundaries. */ > + num =3D MIN(count, align - head); > + if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) { > + num %=3D bs->bl.request_alignment; > + } Could be written as num =3D num % bs->bl.request_alignment ?: num; But that's up to you. More importantly, is it possible that request_alignment > pdiscard_alignment? In that case, align would be request_alignment, head would be less than request_alignment but could be more than pdiscard_alignment. Thus, if that is possible, this might create a request longer than pdiscard_alignment. > + head =3D (head + num) % align; > + assert(num < max_pdiscard); > + } else if (tail) { > + if (num > align) { > + /* Shorten the request to the last aligned cluster. *= / > + num -=3D tail; > + } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment= ) && > + tail > bs->bl.request_alignment) { > + tail %=3D bs->bl.request_alignment; > + num -=3D tail; Same here. Max > + } > + } > + /* limit request size */ > + if (num > max_pdiscard) { > + num =3D max_pdiscard; > + } >=20 > if (bs->drv->bdrv_co_pdiscard) { > ret =3D bs->drv->bdrv_co_pdiscard(bs, offset, num); >=20 --r7nUBkEKH2Ne77fmTIuUSn7eIf7rboJ9Q Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEvBAEBCAAZBQJYLi6aEhxtcmVpdHpAcmVkaGF0LmNvbQAKCRD0B9sAYdXPQG+j B/9JrC62Laa/BDMCz9n6lgTa2KlGgT6423Q9xao1nDi3xkDCEI4ANGZhHxvIP3tv mwLaX0Fy1FDnF4Z3HuQZKVDv6KSUnT4IdeEHP9viZ7Q6hO9EjRbYoA4bL9HgR4t9 J1kxqooKaNtRxXEF7NAjqPOIFrqrHY3Cd0qTr5POqtLu/1o1o2DUDoUYY7LilRQ3 OcRFLu/ms3NERpIiRfYtlGZ+alXp8EGEr9BCDEEnxTjsn47f86NswXX8LNg9kvtC ION0RIfr1/uDvw2KSi8PRxZKv+kofC6OfkK3LLSlb8PmNJm1iZG3Y0soOlVphSeu wOMjSUkQqrlQ+lI7bOo4ICiR =+M8Z -----END PGP SIGNATURE----- --r7nUBkEKH2Ne77fmTIuUSn7eIf7rboJ9Q--