From: Stefan Hajnoczi <stefanha@gmail.com>
To: Stefano Garzarella <sgarzare@redhat.com>
Cc: qemu-devel@nongnu.org, Laurent Vivier <lvivier@redhat.com>,
Kevin Wolf <kwolf@redhat.com>, Thomas Huth <thuth@redhat.com>,
qemu-block@nongnu.org, "Michael S. Tsirkin" <mst@redhat.com>,
Max Reitz <mreitz@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH RFC 1/2] virtio-blk: add DISCARD and WRITE ZEROES features
Date: Fri, 25 Jan 2019 14:58:56 +0000 [thread overview]
Message-ID: <20190125145856.GC28305@stefanha-x1.localdomain> (raw)
In-Reply-To: <20190124172323.230296-2-sgarzare@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 4049 bytes --]
On Thu, Jan 24, 2019 at 06:23:22PM +0100, Stefano Garzarella wrote:
> @@ -584,6 +603,56 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
> virtio_blk_free_request(req);
> break;
> }
> + /*
> + * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
> + * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
> + * so we must mask it for these requests, then we will check the type.
> + */
> + case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT:
> + case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT:
> + {
> + struct virtio_blk_discard_write_zeroes dwz_hdr;
> + uint64_t sector;
> + int bytes;
> +
> + if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr,
> + sizeof(dwz_hdr)) != sizeof(dwz_hdr))) {
"The data used for discard or write zeroes command is described by one
or more virtio_blk_discard_write_zeroes structs."
This needs to be a loop so that multiple dwz structs can be processed up
to the length of virtio_blk_req->data[].
> + virtio_error(vdev, "virtio-blk discard/wzeroes header too short");
> + return -1;
> + }
> +
> + sector = virtio_ldq_p(VIRTIO_DEVICE(req->dev), &dwz_hdr.sector);
> + bytes = virtio_ldl_p(VIRTIO_DEVICE(req->dev),
> + &dwz_hdr.num_sectors) << BDRV_SECTOR_BITS;
Please handle num_sectors << BDRV_SECTOR_BITS overflow so that we don't
need to worry about what happens with an overflowed value later on.
> +
> + if (!virtio_blk_sect_range_ok(req->dev, sector, bytes)) {
> + virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
> + virtio_blk_free_request(req);
> + return 0;
> + }
> +
> + if ((type & ~(VIRTIO_BLK_T_BARRIER)) == VIRTIO_BLK_T_DISCARD) {
Missing device requirement: "the device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard commands if the unmap flag is set."
> + blk_aio_pdiscard(req->dev->blk, sector << BDRV_SECTOR_BITS, bytes,
> + virtio_blk_discard_wzeroes_complete, req);
> + } else if ((type & ~(VIRTIO_BLK_T_BARRIER)) ==
> + VIRTIO_BLK_T_WRITE_ZEROES) {
> + int flags = 0;
> +
> + if (virtio_ldl_p(VIRTIO_DEVICE(req->dev), &dwz_hdr.flags) &
> + VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
> + flags |= BDRV_REQ_MAY_UNMAP;
> + }
> +
> + blk_aio_pwrite_zeroes(req->dev->blk, sector << BDRV_SECTOR_BITS,
> + bytes, flags,
> + virtio_blk_discard_wzeroes_complete, req);
Please add block_acct_start(), this is treated as a write.
> + } else { /* Unsupported if VIRTIO_BLK_T_OUT is not set */
> + virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
> + virtio_blk_free_request(req);
> + }
> +
> + break;
> + }
> default:
> virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
> virtio_blk_free_request(req);
> @@ -763,6 +832,14 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
> blkcfg.alignment_offset = 0;
> blkcfg.wce = blk_enable_write_cache(s->blk);
> virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
> + virtio_stl_p(vdev, &blkcfg.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS);
> + virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
> + virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
> + blk_size >> BDRV_SECTOR_BITS);
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
> + BDRV_REQUEST_MAX_SECTORS);
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
> + blkcfg.write_zeroes_may_unmap = 1;
Please check hw/scsi/scsi-disk.c for an example of how to initialize
these limits. It should be possible to set some of them via s->conf so
I'm surprised so many fields are hardcoded in this patch.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
next prev parent reply other threads:[~2019-01-25 15:10 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-24 17:23 [Qemu-devel] [PATCH RFC 0/2] virtio-blk: add DISCARD and WRITE ZEROES features Stefano Garzarella
2019-01-24 17:23 ` [Qemu-devel] [PATCH RFC 1/2] " Stefano Garzarella
2019-01-24 17:55 ` Dr. David Alan Gilbert
2019-01-24 18:31 ` Stefano Garzarella
2019-01-25 14:58 ` Stefan Hajnoczi [this message]
2019-01-25 16:18 ` Stefano Garzarella
2019-01-27 12:51 ` Stefan Hajnoczi
2019-01-28 8:28 ` Stefano Garzarella
2019-01-24 17:23 ` [Qemu-devel] [PATCH RFC 2/2] tests/virtio-blk: add test for WRITE_ZEROES command Stefano Garzarella
2019-01-25 6:01 ` Thomas Huth
2019-01-25 6:07 ` Thomas Huth
2019-01-25 8:16 ` Stefano Garzarella
2019-01-25 8:49 ` Thomas Huth
2019-01-25 11:58 ` Liu, Changpeng
2019-01-25 12:48 ` Thomas Huth
2019-01-25 19:18 ` Michael S. Tsirkin
2019-01-25 15:12 ` Stefan Hajnoczi
2019-01-25 19:17 ` Michael S. Tsirkin
2019-01-27 12:57 ` Stefan Hajnoczi
2019-01-27 18:42 ` Michael S. Tsirkin
2019-01-30 7:39 ` Stefan Hajnoczi
2019-01-30 7:59 ` Liu, Changpeng
2019-01-25 19:14 ` Michael S. Tsirkin
2019-01-25 6:07 ` Thomas Huth
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=20190125145856.GC28305@stefanha-x1.localdomain \
--to=stefanha@gmail.com \
--cc=kwolf@redhat.com \
--cc=lvivier@redhat.com \
--cc=mreitz@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--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).