Skip to contentSkip to conversation

virtio-blk: Assign discard_granularity

PATCH In review Akihiko Odaki 3 participants 5 messages Feb 24, 2022 – Mar 1, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-blk: Assign discard_granularity

Virtual I/O Device (VIRTIO) Version 1.1
https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html

Quoted 2 lines

discard_sector_alignment can be used by OS when splitting a request based on alignment.

According to Documentation/ABI/stable/sysfs-block, the corresponding field in the kernel is, confusingly, discard_granularity, not discard_alignment.

---
 drivers/block/virtio_blk.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/block/virtio_blk.c +1 -3
index c443cd64fc9b..1fb3c89900e3 100644
@@ -913,11 +913,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 		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);
-		q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;
+		q->limits.discard_granularity = v ? v << SECTOR_SHIFT : 0;
 
 		virtio_cread(vdev, struct virtio_blk_config,
 			     max_discard_sectors, &v);
Signature · 1 line
2.35.1
#1 lore raw
Quoted 8 lines · Akihiko Odaki

Virtual I/O Device (VIRTIO) Version 1.1
https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html

Quoted 2 lines

discard_sector_alignment can be used by OS when splitting a request based on alignment.

According to Documentation/ABI/stable/sysfs-block, the corresponding field in the kernel is, confusingly, discard_granularity, not discard_alignment.

Good catch, struct virtio_blk_config->discard_sector_alignment is Linux q->limits.discard_granularity.

Quoted 11 lines
---
 drivers/block/virtio_blk.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/block/virtio_blk.c +0 -0
index c443cd64fc9b..1fb3c89900e3 100644

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 920

Quoted 8 lines
drivers/block/virtio_blk.c +0 -3
@@ -913,11 +913,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 		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);
-		q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;

Should we use struct virtio_blk_config->topology.alignment_offset ("offset of first aligned logical block" and used for Linux blk_queue_alignment_offset()) for q->limits.discard_alignment?

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

#2 lore raw
Quoted 26 lines · Stefan Hajnoczi
Quoted 8 lines · Akihiko Odaki

Virtual I/O Device (VIRTIO) Version 1.1
https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html

Quoted 2 lines

discard_sector_alignment can be used by OS when splitting a request based on alignment.

According to Documentation/ABI/stable/sysfs-block, the corresponding field in the kernel is, confusingly, discard_granularity, not discard_alignment.

Good catch, struct virtio_blk_config->discard_sector_alignment is Linux q->limits.discard_granularity.

Quoted 13 lines
---
  drivers/block/virtio_blk.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/block/virtio_blk.c +0 -0
index c443cd64fc9b..1fb3c89900e3 100644
@@ -913,11 +913,9 @@ static int virtblk_probe(struct virtio_device *vdev)
  		blk_queue_io_opt(q, blk_size * opt_io_size);
 
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 920

Quoted 6 lines
drivers/block/virtio_blk.c +0 -3
  	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);
-		q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;

Should we use struct virtio_blk_config->topology.alignment_offset ("offset of first aligned logical block" and used for Linux blk_queue_alignment_offset()) for q->limits.discard_alignment?

Maybe but I'm not sure. I had looked at the code of QEMU
(commit 5c1ee569660d4a205dced9cb4d0306b907fb7599) but it apparently always sets 0 for virtio_blk_config->topology.alignment_offset. I don't have a hardware which requires discard_alignment either so I cannot test it.

I'd like to leave this patch as is since I cannot deny the possibility that the host has a different alignment offset for discarding and other operations.

reviewer Reviewed-by
#3 lore raw
Quoted 43 lines · Akihiko Odaki
Quoted 19 lines · Stefan Hajnoczi
Quoted 8 lines · Akihiko Odaki

Virtual I/O Device (VIRTIO) Version 1.1
https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html

Quoted 2 lines

discard_sector_alignment can be used by OS when splitting a request based on alignment.

According to Documentation/ABI/stable/sysfs-block, the corresponding field in the kernel is, confusingly, discard_granularity, not discard_alignment.

Good catch, struct virtio_blk_config->discard_sector_alignment is Linux q->limits.discard_granularity.

Quoted 6 lines
---
  drivers/block/virtio_blk.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 919

Quoted 12 lines
drivers/block/virtio_blk.c +0 -3
index c443cd64fc9b..1fb3c89900e3 100644
@@ -913,11 +913,9 @@ static int virtblk_probe(struct virtio_device *vdev)
  		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);
-		q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;

Should we use struct virtio_blk_config->topology.alignment_offset ("offset of first aligned logical block" and used for Linux blk_queue_alignment_offset()) for q->limits.discard_alignment?

Maybe but I'm not sure. I had looked at the code of QEMU (commit 5c1ee569660d4a205dced9cb4d0306b907fb7599) but it apparently always sets 0 for virtio_blk_config->topology.alignment_offset.
I don't have a hardware which requires discard_alignment either so I cannot test it.

Quoted 3 lines

I'd like to leave this patch as is since I cannot deny the possibility that the host has a different alignment offset for discarding and other operations.

Fair enough. To do it properly we'd need to add a new configuration space field to virtio-blk.

#4 lore raw
In reply to Akihiko Odaki

Akihiko,

Quoted 3 lines

I'd like to leave this patch as is since I cannot deny the possibility that the host has a different alignment offset for discarding and other operations.

That's correct.

SCSI explicitly reports separate values for physical block alignment and "discard" alignment. The queue limits reflect this distinction.

While it is not super common for these two to be different, it does happen. There are several disk arrays that do not have an internal allocation unit (discard granularity) which is a power of two, for instance.

I am not particularly happy that we have to deal with two distinct types of alignment in the stack but that is the reality of the hardware we have to support.

Signature · 5 lines
Martin K. Petersen	Oracle Linux Engineering
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization