Skip to contentSkip to conversation

virtio_blk: add DISCARD and WRIET ZEROES commands support

PATCH Superseded Changpeng Liu 3 participants 7 messages May 29, 2018 – Jun 5, 2018

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 virtio_blk: add DISCARD and WRIET ZEROES commands support

Existing virtio-blk protocol doesn't have DISCARD/WRITE ZEROES commands support, this will impact the performance when using SSD backend over file systems.

Commit 88c85538 "virtio-blk: add discard and write zeroes features to specification"(see https://github.com/oasis-tcs/virtio-spec) extended existing virtio-blk protocol, adding extra DISCARD and WRITE ZEROES commands support.

While here, using 16 bytes descriptor to describe one segment of DISCARD or WRITE ZEROES commands, each command may contain one or more decriptors.

The following data structure shows the definition of one descriptor:

struct virtio_blk_discard_write_zeroes {
        le64 sector;
        le32 num_sectors;
        le32 unmap;
};

Field 'sector' means the start sector for DISCARD and WRITE ZEROES, filed 'num_sectors' means the number of sectors for DISCARD and WRITE ZEROES, if both DISCARD and WRITE ZEROES are supported, field 'unmap' maybe used for WRITE ZEROES command with DISCARD enabled.

We also extended the virtio-blk configuration space to let backend device put DISCARD and WRITE ZEROES configuration parameters.

struct virtio_blk_config {
        [...]

        le32 max_discard_sectors;
        le32 max_discard_seg;
        le32 discard_sector_alignment;
        le32 max_write_zeroes_sectors;
        le32 max_write_zeroes_seg;
        u8 write_zeroes_may_unmap;
}

New feature bit [VIRTIO_BLK_F_DISCARD (13)]: Device can support discard command, maximum discard sectors size in field 'max_discard_sectors' and maximum discard segment number in field 'max_discard_seg'.

New feature [VIRTIO_BLK_F_WRITE_ZEROES (14)]: Device can support write zeroes command, maximum write zeroes sectors size in field 'max_write_zeroes_sectors' and maximum write zeroes segment number in field 'max_write_zeroes_seg'.

The parameters in the configuration space of the device field 'max_discard_sectors' and field 'discard_sector_alignment' are expressed in 512-byte units if the VIRTIO_BLK_F_DISCARD feature bit is negotiated. The field 'max_write_zeroes_sectors' is expressed in 512-byte units if the VIRTIO_BLK_F_WRITE_ZEROES feature bit is negotiated.

---
CHANGELOG:
v4: several optimizations based on MST's comments, remove bit field usage for command descriptor.
v3: define the virtio-blk protocol to add discard and write zeroes support, first version implementation based on proposed specification.
v2: add write zeroes command support.
v1: initial proposal implementation for discard command.

---
 drivers/block/virtio_blk.c      | 92 +++++++++++++++++++++++++++++++++++++++--
 include/uapi/linux/virtio_blk.h | 43 +++++++++++++++++++
 2 files changed, 132 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +89 -3
index 4a07593c..a250fcc 100644
@@ -172,10 +172,45 @@ static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
 	return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
 }
 
+static inline int virtblk_setup_discard_write_zeroes(struct request *req,
+						bool unmap)
+{
+	unsigned short segments = blk_rq_nr_discard_segments(req);
+	unsigned short n = 0;
+	struct virtio_blk_discard_write_zeroes *range;
+	struct bio *bio;
+
+	range = kmalloc_array(segments, sizeof(*range), GFP_KERNEL);
+	if (!range)
+		return -ENOMEM;
+
+	__rq_for_each_bio(bio, req) {
+		u64 sector = bio->bi_iter.bi_sector;
+		u32 num_sectors = bio->bi_iter.bi_size >> 9;
+
+		range[n].unmap = cpu_to_le32(unmap);
+		range[n].num_sectors = cpu_to_le32(num_sectors);
+		range[n].sector = cpu_to_le64(sector);
+		n++;
+	}
+
+	req->special_vec.bv_page = virt_to_page(range);
+	req->special_vec.bv_offset = offset_in_page(range);
+	req->special_vec.bv_len = sizeof(*range) * segments;
+	req->rq_flags |= RQF_SPECIAL_PAYLOAD;
+
+	return 0;
+}
+
 static inline void virtblk_request_done(struct request *req)
 {
 	struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
 
+	if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
+		kfree(page_address(req->special_vec.bv_page) +
+		      req->special_vec.bv_offset);
+	}
+
 	switch (req_op(req)) {
 	case REQ_OP_SCSI_IN:
 	case REQ_OP_SCSI_OUT:
@@ -225,6 +260,7 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 	int qid = hctx->queue_num;
 	int err;
 	bool notify = false;
+	bool unmap = false;
 	u32 type;
 
 	BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
@@ -237,6 +273,13 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 	case REQ_OP_FLUSH:
 		type = VIRTIO_BLK_T_FLUSH;
 		break;
+	case REQ_OP_DISCARD:
+		type = VIRTIO_BLK_T_DISCARD;
+		break;
+	case REQ_OP_WRITE_ZEROES:
+		type = VIRTIO_BLK_T_WRITE_ZEROES;
+		unmap = !(req->cmd_flags & REQ_NOUNMAP);
+		break;
 	case REQ_OP_SCSI_IN:
 	case REQ_OP_SCSI_OUT:
 		type = VIRTIO_BLK_T_SCSI_CMD;
@@ -256,9 +299,16 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 
 	blk_mq_start_request(req);
 
+	if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
+		err = virtblk_setup_discard_write_zeroes(req, unmap);
+		if (err)
+			return BLK_STS_RESOURCE;
+	}
+
 	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
 	if (num) {
-		if (rq_data_dir(req) == WRITE)
+		if (rq_data_dir(req) == WRITE || type == VIRTIO_BLK_T_DISCARD ||
+		    type == VIRTIO_BLK_T_WRITE_ZEROES)
 			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
 		else
 			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
@@ -777,6 +827,42 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (!err && opt_io_size)
 		blk_queue_io_opt(q, blk_size * opt_io_size);
 
+	if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
+		q->limits.discard_granularity = blk_size;
+
+		virtio_cread(vdev, struct virtio_blk_config,
+				discard_sector_alignment, &v);
+		if (v)
+			q->limits.discard_alignment = v << 9;
+		else
+			q->limits.discard_alignment = 0;
+
+		virtio_cread(vdev, struct virtio_blk_config,
+				max_discard_sectors, &v);
+		if (v)
+			blk_queue_max_discard_sectors(q, v);
+		else
+			blk_queue_max_discard_sectors(q, UINT_MAX);
+
+		virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
+				&v);
+		if (v)
+			blk_queue_max_discard_segments(q, v);
+		else
+			blk_queue_max_discard_segments(q, USHRT_MAX);
+
+		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
+	}
+
+	if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
+		virtio_cread(vdev, struct virtio_blk_config,
+				max_write_zeroes_sectors, &v);
+		if (v)
+			blk_queue_max_write_zeroes_sectors(q, v);
+		else
+			blk_queue_max_write_zeroes_sectors(q, UINT_MAX);
+	}
+
 	virtblk_update_capacity(vblk, false);
 	virtio_device_ready(vdev);
 
@@ -885,14 +971,14 @@ static int virtblk_restore(struct virtio_device *vdev)
 	VIRTIO_BLK_F_SCSI,
 #endif
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
-	VIRTIO_BLK_F_MQ,
+	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
 }
 ;
 static unsigned int features[] = {
 	VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
-	VIRTIO_BLK_F_MQ,
+	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
 };
 
 static struct virtio_driver virtio_blk = {
include/uapi/linux/virtio_blk.h +43 -0

67 lines not on this page · raw message

Signature · 1 line
1.9.3
#1 lore raw

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 312

Quoted 6 lines · Changpeng Liu
drivers/block/virtio_blk.c +2 -1
 	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
 	if (num) {
-		if (rq_data_dir(req) == WRITE)
+		if (rq_data_dir(req) == WRITE || type == VIRTIO_BLK_T_DISCARD ||
+		    type == VIRTIO_BLK_T_WRITE_ZEROES)
 			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);

The VIRTIO specification says:

The type of the request is either a read (VIRTIO_BLK_T_IN), a write (VIRTIO_BLK_T_OUT), a discard (VIRTIO_BLK_T_DISCARD), a write zeroes (VIRTIO_BLK_T_WRITE_ZEROES) or a flush (VIRTIO_BLK_T_FLUSH).

But this patch actually uses VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_OUT or VIRTIO_BLK_T_WRITE_ZEROES | VIRTIO_BLK_T_OUT. "either A or B" is exclusive, it does not mean "A and B".

Can you clarify whether the spec needs to be changed or what the purpose of ORing in the VIRTIO_BLK_T_OUT bit is?

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
Quoted 24 lines

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Thursday, May 31, 2018 6:31 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>
Subject: Re: [PATCH v4] virtio_blk: add DISCARD and WRIET ZEROES commands support

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 4 lines · Changpeng Liu
drivers/block/virtio_blk.c +1 -1
 	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
 	if (num) {
-		if (rq_data_dir(req) == WRITE)
+		if (rq_data_dir(req) == WRITE || type == VIRTIO_BLK_T_DISCARD
||
> +		    type == VIRTIO_BLK_T_WRITE_ZEROES)
>  			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev,

VIRTIO_BLK_T_OUT);

The VIRTIO specification says:

Quoted 7 lines

The type of the request is either a read (VIRTIO_BLK_T_IN), a write (VIRTIO_BLK_T_OUT), a discard (VIRTIO_BLK_T_DISCARD), a write zeroes (VIRTIO_BLK_T_WRITE_ZEROES) or a flush (VIRTIO_BLK_T_FLUSH).

But this patch actually uses VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_OUT or VIRTIO_BLK_T_WRITE_ZEROES | VIRTIO_BLK_T_OUT. "either A or B" is exclusive, it does not mean "A and B".

Hi Stefan,

For the new add DISCARD and WRITE ZEROES commands, I defined the type code to 11 and 13, so the last bit always is 1, there is no difference in practice.
But I believe the specification says VIRTIO_BLK_T_OUT means direction, so OR the two bits together should compliance with the specification.

Quoted 3 lines

Can you clarify whether the spec needs to be changed or what the purpose of ORing in the VIRTIO_BLK_T_OUT bit is?

#3 lore raw
Quoted 28 lines · Changpeng
Quoted 28 lines

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Thursday, May 31, 2018 6:31 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>
Subject: Re: [PATCH v4] virtio_blk: add DISCARD and WRIET ZEROES commands support

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 4 lines · Changpeng Liu
drivers/block/virtio_blk.c +1 -1
 	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
 	if (num) {
-		if (rq_data_dir(req) == WRITE)
+		if (rq_data_dir(req) == WRITE || type == VIRTIO_BLK_T_DISCARD
||
> +		    type == VIRTIO_BLK_T_WRITE_ZEROES)
>  			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev,

VIRTIO_BLK_T_OUT);

The VIRTIO specification says:

The type of the request is either a read (VIRTIO_BLK_T_IN), a write (VIRTIO_BLK_T_OUT), a discard (VIRTIO_BLK_T_DISCARD), a write zeroes (VIRTIO_BLK_T_WRITE_ZEROES) or a flush (VIRTIO_BLK_T_FLUSH).

Quoted 8 lines
Quoted 3 lines

But this patch actually uses VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_OUT or VIRTIO_BLK_T_WRITE_ZEROES | VIRTIO_BLK_T_OUT. "either A or B" is exclusive, it does not mean "A and B".

Hi Stefan,

For the new add DISCARD and WRITE ZEROES commands, I defined the type code to 11 and 13, so the last bit always is 1, there is no difference in practice.

Then the code is misleading. This is clearer:

  if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES)
      /* Do nothing, type already set */
  else if (rq_data_dir(req) == WRITE)
      vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
  ...
Quoted 2 lines

But I believe the specification says VIRTIO_BLK_T_OUT means direction, so OR the two bits together should compliance with the specification.

I cannot find that in the specification:

http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-2020002

and it would contradict the "The type of the request is either ... or ..." wording that I quoted from the spec above.

If you do find something in the spec, please let me know and we can figure out how to make the spec consistent.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#4 lore raw
Quoted 50 lines

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Friday, June 1, 2018 5:00 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>
Subject: Re: [PATCH v4] virtio_blk: add DISCARD and WRIET ZEROES commands support

Quoted 28 lines · Changpeng
Quoted 28 lines

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Thursday, May 31, 2018 6:31 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>
Subject: Re: [PATCH v4] virtio_blk: add DISCARD and WRIET ZEROES commands support

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 4 lines · Changpeng Liu
drivers/block/virtio_blk.c +1 -1
 	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
 	if (num) {
-		if (rq_data_dir(req) == WRITE)
+		if (rq_data_dir(req) == WRITE || type == VIRTIO_BLK_T_DISCARD
||
> +		    type == VIRTIO_BLK_T_WRITE_ZEROES)
>  			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev,

VIRTIO_BLK_T_OUT);

The VIRTIO specification says:

The type of the request is either a read (VIRTIO_BLK_T_IN), a write (VIRTIO_BLK_T_OUT), a discard (VIRTIO_BLK_T_DISCARD), a write zeroes (VIRTIO_BLK_T_WRITE_ZEROES) or a flush (VIRTIO_BLK_T_FLUSH).

Quoted 8 lines
Quoted 3 lines

But this patch actually uses VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_OUT or VIRTIO_BLK_T_WRITE_ZEROES | VIRTIO_BLK_T_OUT. "either A or B" is exclusive, it does not mean "A and B".

Hi Stefan,

For the new add DISCARD and WRITE ZEROES commands, I defined the type code to 11 and 13, so the last bit always is 1, there is no difference in practice.

Quoted 7 lines

Then the code is misleading. This is clearer:

  if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES)
      /* Do nothing, type already set */
  else if (rq_data_dir(req) == WRITE)
      vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
  ...

This change sounds good to me, will change the patch accordingly.

Quoted 6 lines
Quoted 3 lines

But I believe the specification says VIRTIO_BLK_T_OUT means direction, so OR the two bits together should compliance with the specification.

I cannot find that in the specification:

Quoted 8 lines

http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-
2020002

and it would contradict the "The type of the request is either ... or ..." wording that I quoted from the spec above.

If you do find something in the spec, please let me know and we can figure out how to make the spec consistent.

I saw comments from file linux/usr/include/uapi/linux/virtio_blk.h, which says VIRTIO_BLK_T_OUT may be combined with other commands and means direction, the specification does not have such description.

reviewer
#5 lore raw
Quoted 10 lines · Changpeng
Quoted 10 lines
Quoted 2 lines

But I believe the specification says VIRTIO_BLK_T_OUT means direction, so OR the two bits together should compliance with the specification.

I cannot find that in the specification:

http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-
2020002

and it would contradict the "The type of the request is either ... or ..." wording that I quoted from the spec above.

Quoted 6 lines
Quoted 2 lines

If you do find something in the spec, please let me know and we can figure out how to make the spec consistent.

I saw comments from file linux/usr/include/uapi/linux/virtio_blk.h, which says VIRTIO_BLK_T_OUT may be combined with other commands and means direction, the specification does not have such description.

I don't think it is in the specification indeed (however, 11 and 13 were chosen so that VIRTIO_BLK_T_OUT can still indicate direction).

Paolo

#6 lore raw
Quoted 26 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Monday, June 4, 2018 6:03 PM
To: Liu, Changpeng <[email protected]>; Stefan Hajnoczi <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>
Subject: Re: [PATCH v4] virtio_blk: add DISCARD and WRIET ZEROES commands support

Quoted 13 lines · Changpeng
Quoted 12 lines
Quoted 2 lines

But I believe the specification says VIRTIO_BLK_T_OUT means direction, so OR the two bits together should compliance with the specification.

I cannot find that in the specification:

http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-
2020002

and it would contradict the "The type of the request is either ... or ..." wording that I quoted from the spec above.

If you do find something in the spec, please let me know and we can figure out how to make the spec consistent.

Quoted 6 lines
Quoted 3 lines

I saw comments from file linux/usr/include/uapi/linux/virtio_blk.h, which says VIRTIO_BLK_T_OUT may be combined with other commands and means direction, the specification does not have such description.

I don't think it is in the specification indeed (however, 11 and 13 were chosen so that VIRTIO_BLK_T_OUT can still indicate direction).

Correct, we don't need to OR VIRTIO_BLK_T_OUT again for DISCARD and WRITE ZEROES commands. I'll remove it in next patch set.

Quoted 2 lines

Paolo