qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Max Reitz <mreitz@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	Thomas Huth <thuth@redhat.com>,
	qemu-block@nongnu.org, "Michael S. Tsirkin" <mst@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	qemu-devel@nongnu.org, Coiby Xu <Coiby.Xu@gmail.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH for-5.2 08/10] block/export: port virtio-blk discard/write zeroes input validation
Date: Mon, 7 Dec 2020 11:38:40 +0000	[thread overview]
Message-ID: <20201207113840.GF203660@stefanha-x1.localdomain> (raw)
In-Reply-To: <0446562a-c3bf-cff4-82e2-71b9ae2cf3bf@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2586 bytes --]

On Thu, Nov 12, 2020 at 04:25:39PM +0100, Max Reitz wrote:
> On 11.11.20 13:43, Stefan Hajnoczi wrote:
> > @@ -58,30 +60,101 @@ static void vu_blk_req_complete(VuBlkReq *req)
> >       free(req);
> >   }
> > +static bool vu_blk_sect_range_ok(VuBlkExport *vexp, uint64_t sector,
> > +                                 size_t size)
> > +{
> > +    uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
> > +    uint64_t total_sectors;
> > +
> > +    if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
> 
> I wonder why you pass a byte length, then shift it down to sectors, when
> it’s kind of unsafe on the caller’s side to even calculate that byte length
> (because the caller has to verify that shifting the sector count up to be a
> byte length is safe).
> 
> Wouldn’t it be simpler to pass nb_sectors (perhaps even as uint64_t, because
> why not) and then compare that against BDRV_REQUEST_MAX_BYTES here?
> 
> (With how the code is written, it also has the problem of rounding down
> @size.  Which isn’t a problem in practice because the caller effectively
> guarantees that @size is aligned to sectors, but it still means that the
> code looks a bit strange.  As in, “Why is it safe to round down?  Ah,
> because the caller always produces an aligned value.  But why does the
> caller even pass a byte count, then?  Especially given that the offset is
> passed as a sector index, too.”)

Thanks for the suggestions, I'll take a look for the next revision.

> > +        return false;
> > +    }
> > +    if ((sector << BDRV_SECTOR_BITS) % vexp->blk_size) {
> 
> This made me wonder why the discard/write-zeroes sector granularity would be
> BDRV_SECTOR_BITS and not blk_size, which is used for IN/OUT (read/write)
> requests.
> 
> I still don’t know, but judging from the only reference I could find quickly
> (contrib/vhost-user-blk/vhost-user-blk.c), it seems correct.
> 
> OTOH, I wonder whether BDRV_SECTOR_BITS/BDRV_SECTOR_SIZE are the correct
> constants.  Isn’t it just 9/512 as per some specification, i.e., shouldn’t
> it be independent of qemu’s block layer’s sector size?

Yes, a new VIRTIO_BLK_SECTOR_SIZE constant would be clearer. According
to the VIRTIO 1.1 specification:

  blk_size can be read to determine the optimal sector size for the
  driver to use. This does not affect the units used in the protocol
  (always 512 bytes), but awareness of the correct value can affect
  performance.

So all virtio-blk sector fields are in 512-byte units, regardless of the
blk_size config field.

Stefan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2020-12-07 12:48 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 12:43 [PATCH for-5.2 00/10] block/export: vhost-user-blk server tests and input validation Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 01/10] test: new qTest case to test the vhost-user-blk-server Stefan Hajnoczi
2020-11-25  8:20   ` Coiby Xu
2020-11-25  8:28     ` Coiby Xu
2020-12-07 11:28       ` Stefan Hajnoczi
2020-12-18 14:49         ` Coiby Xu
2020-11-11 12:43 ` [PATCH for-5.2 02/10] tests/qtest: add multi-queue test case to vhost-user-blk-test Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 03/10] libqtest: add qtest_socket_server() Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 04/10] vhost-user-blk-test: rename destroy_drive() to destroy_file() Stefan Hajnoczi
2020-11-12 14:32   ` Max Reitz
2020-11-12 17:04     ` Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 05/10] vhost-user-blk-test: close fork child file descriptors Stefan Hajnoczi
2020-11-24 12:08   ` Coiby Xu
2020-12-03 13:57     ` Stefan Hajnoczi
2020-12-18 13:44       ` Coiby Xu
2020-11-11 12:43 ` [PATCH for-5.2 06/10] vhost-user-blk-test: drop unused return value Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 07/10] vhost-user-blk-test: fix races by using fd passing Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 08/10] block/export: port virtio-blk discard/write zeroes input validation Stefan Hajnoczi
2020-11-12 15:25   ` Max Reitz
2020-12-07 11:38     ` Stefan Hajnoczi [this message]
2020-11-11 12:43 ` [PATCH for-5.2 09/10] vhost-user-blk-test: test discard/write zeroes invalid inputs Stefan Hajnoczi
2020-11-12 15:40   ` Max Reitz
2020-12-07 11:31     ` Stefan Hajnoczi
2020-11-11 12:43 ` [PATCH for-5.2 10/10] block/export: port virtio-blk read/write range check Stefan Hajnoczi
2020-11-12 15:51   ` Max Reitz
2020-11-17  9:18 ` [PATCH for-5.2 00/10] block/export: vhost-user-blk server tests and input validation Michael S. Tsirkin
2021-01-15 11:48   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201207113840.GF203660@stefanha-x1.localdomain \
    --to=stefanha@redhat.com \
    --cc=Coiby.Xu@gmail.com \
    --cc=kwolf@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).