vdpa_sim_blk: add support for flush, discard, and write-zeroes
v3 cover vdpa_sim_blk: add support for flush, discard, and write-zeroes
The first 2 patches are in preparation and generalize parts later used by the following patches that add handling of flush, discard, and write-zeroes requests.
v3:
- fixed clang issue reported by kernel test robot <[email protected]>
- used le*_to_cpu() to access virtio_blk_discard_write_zeroes fields
v2: https://lore.kernel.org/virtualization/[email protected]/
v1: https://lore.kernel.org/virtualization/[email protected]/
Tree available here:
https://gitlab.com/sgarzarella/linux.git vdpa-sim-blk-flush-discard-wz
Thanks,
Stefano
Stefano Garzarella (4):
vdpa_sim_blk: check if sector is 0 for commands other than read or
write
vdpa_sim_blk: make vdpasim_blk_check_range usable by other requests
vdpa_sim_blk: add support for VIRTIO_BLK_T_FLUSH
vdpa_sim_blk: add support for discard and write-zeroes
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 127 +++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 16 deletions(-)
Signature · 6 lines
2.37.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH v3 1/4 vdpa_sim_blk: check if sector is 0 for commands other than read or write
VIRTIO spec states: "The sector number indicates the offset (multiplied by 512) where the read or write is to occur. This field is unused and set to 0 for commands other than read or write."
--- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +10 -0
@@ -112,6 +112,15 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim, offset = sector <<SECTOR_SHIFT ; status =VIRTIO_BLK_S_OK ; +if (type !=VIRTIO_BLK_T_IN && type !=VIRTIO_BLK_T_OUT && + sector !=0 ) { +dev_dbg (&vdpasim->vdpa.dev, +"sector must be 0 for %u request - sector: 0x%llx\n" , + type, sector); + status =VIRTIO_BLK_S_IOERR ; + goto err_status; + } +switch (type) {case VIRTIO_BLK_T_IN :if (!vdpasim_blk_check_range (sector, to_push)) {
@@ -178,6 +187,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,break ; } +err_status:/* If some operations fail, we need to skip the remaining bytes * to put the status in the last byte */
Signature · 1 line
2.37.1
PATCH v3 2/4 vdpa_sim_blk: make vdpasim_blk_check_range usable by other requests
In reply to Stefano GarzarellaNext patches will add handling of other requests, where will be useful to reuse vdpasim_blk_check_range().
So let's make it more generic by adding the max_sectors parameter, since different requests allow different numbers of maximum sectors.
Let's also print the messages directly in vdpasim_blk_check_range() to avoid duplicate prints.
--- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 38 +++++++++++++++++----------- 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +23 -15
@@ -42,18 +42,28 @@static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES ] ="vdpa_blk_sim" ; -static bool vdpasim_blk_check_range (u64 start_sector,size_t range_size) +static bool vdpasim_blk_check_range (struct vdpasim *vdpasim,u64 start_sector, +u64 num_sectors,u64 max_sectors) { -u64 range_sectors = range_size >>SECTOR_SHIFT ; - -if (range_size >VDPASIM_BLK_SIZE_MAX *VDPASIM_BLK_SEG_MAX ) -return false; +if (start_sector >VDPASIM_BLK_CAPACITY ) { +dev_dbg (&vdpasim->vdpa.dev, +"starting sector exceeds the capacity - start: 0x%llx capacity: 0x%x\n" , + start_sector,VDPASIM_BLK_CAPACITY ); + } -if (start_sector >VDPASIM_BLK_CAPACITY ) +if (num_sectors > max_sectors) { +dev_dbg (&vdpasim->vdpa.dev, +"number of sectors exceeds the max allowed in a request - num: 0x%llx max: 0x%llx\n" , + num_sectors, max_sectors);return false; + } -if (range_sectors >VDPASIM_BLK_CAPACITY - start_sector) +if (num_sectors >VDPASIM_BLK_CAPACITY - start_sector) { +dev_dbg (&vdpasim->vdpa.dev, +"request exceeds the capacity - start: 0x%llx num: 0x%llx capacity: 0x%x\n" , + start_sector, num_sectors,VDPASIM_BLK_CAPACITY );return false; + }return true; }
@@ -123,10 +133,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,switch (type) {case VIRTIO_BLK_T_IN : -if (!vdpasim_blk_check_range (sector, to_push)) { -dev_dbg (&vdpasim->vdpa.dev, -"reading over the capacity - offset: 0x%llx len: 0x%zx\n" , - offset, to_push); +if (!vdpasim_blk_check_range (vdpasim, sector, + to_push >>SECTOR_SHIFT , +VDPASIM_BLK_SIZE_MAX *VDPASIM_BLK_SEG_MAX )) { status =VIRTIO_BLK_S_IOERR ;break ; }
@@ -146,10 +155,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,break ;case VIRTIO_BLK_T_OUT : -if (!vdpasim_blk_check_range (sector, to_pull)) { -dev_dbg (&vdpasim->vdpa.dev, -"writing over the capacity - offset: 0x%llx len: 0x%zx\n" , - offset, to_pull); +if (!vdpasim_blk_check_range (vdpasim, sector, + to_pull >>SECTOR_SHIFT , +VDPASIM_BLK_SIZE_MAX *VDPASIM_BLK_SEG_MAX )) { status =VIRTIO_BLK_S_IOERR ;break ; }
Signature · 6 lines
2.37.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH v3 3/4 vdpa_sim_blk: add support for VIRTIO_BLK_T_FLUSH
In reply to Stefano GarzarellaThe simulator behaves like a ramdisk, so we don't have to do anything when a VIRTIO_BLK_T_FLUSH request is received, but it could be useful to test driver behavior.
Let's expose the VIRTIO_BLK_F_FLUSH feature to inform the driver that we support the flush command.
--- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +5 -0
@@ -25,6 +25,7 @@#define DRV_LICENSE "GPL v2"#define VDPASIM_BLK_FEATURES (VDPASIM_FEATURES | \ + (1ULL << VIRTIO_BLK_F_FLUSH) | \ (1ULL << VIRTIO_BLK_F_SIZE_MAX) | \ (1ULL << VIRTIO_BLK_F_SEG_MAX) | \ (1ULL << VIRTIO_BLK_F_BLK_SIZE) | \
@@ -188,6 +189,10 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim, pushed += bytes;break ; +case VIRTIO_BLK_T_FLUSH : +/* nothing to do */ +break ; +default :dev_dbg (&vdpasim->vdpa.dev,"Unsupported request type %d\n" , type);
Signature · 6 lines
2.37.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
PATCH v3 4/4 vdpa_sim_blk: add support for discard and write-zeroes
In reply to Stefano GarzarellaExpose VIRTIO_BLK_F_DISCARD and VIRTIO_BLK_F_WRITE_ZEROES features to the drivers and handle VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES requests checking ranges and flags.
The simulator behaves like a ramdisk, so for VIRTIO_BLK_F_DISCARD does nothing, while for VIRTIO_BLK_T_WRITE_ZEROES sets to 0 the specified region.
--- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 74 +++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +73 -1
@@ -30,11 +30,14 @@ (1ULL << VIRTIO_BLK_F_SEG_MAX) | \ (1ULL << VIRTIO_BLK_F_BLK_SIZE) | \ (1ULL << VIRTIO_BLK_F_TOPOLOGY) | \ - (1ULL << VIRTIO_BLK_F_MQ)) + (1ULL << VIRTIO_BLK_F_MQ) | \ + (1ULL << VIRTIO_BLK_F_DISCARD) | \ + (1ULL << VIRTIO_BLK_F_WRITE_ZEROES))#define VDPASIM_BLK_CAPACITY 0x40000#define VDPASIM_BLK_SIZE_MAX 0x1000#define VDPASIM_BLK_SEG_MAX 32 +#define VDPASIM_BLK_DWZ_MAX_SECTORS UINT_MAX/* 1 virtqueue, 1 address space, 1 virtqueue group */ #define VDPASIM_BLK_VQ_NUM 1
@@ -193,6 +196,64 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,/* nothing to do */ break ; +case VIRTIO_BLK_T_DISCARD : +case VIRTIO_BLK_T_WRITE_ZEROES : { +struct virtio_blk_discard_write_zeroes range; +u32 num_sectors, flags; + +if (to_pull !=sizeof (range)) { +dev_dbg (&vdpasim->vdpa.dev, +"discard/write_zeroes header len: 0x%zx [expected: 0x%zx]\n" , + to_pull,sizeof (range)); + status =VIRTIO_BLK_S_IOERR ; +break ; + } + + bytes =vringh_iov_pull_iotlb (&vq->vring, &vq->out_iov, &range, + to_pull); +if (bytes <0 ) { +dev_dbg (&vdpasim->vdpa.dev, +"vringh_iov_pull_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n" , + bytes, offset, to_pull); + status =VIRTIO_BLK_S_IOERR ; +break ; + } + + sector =le64_to_cpu (range.sector); + offset = sector <<SECTOR_SHIFT ; + num_sectors =le32_to_cpu (range.num_sectors); + flags =le32_to_cpu (range.flags); + +if (type ==VIRTIO_BLK_T_DISCARD && flags !=0 ) { +dev_dbg (&vdpasim->vdpa.dev, +"discard unexpected flags set - flags: 0x%x\n" , + flags); + status =VIRTIO_BLK_S_UNSUPP ; +break ; + } + +if (type ==VIRTIO_BLK_T_WRITE_ZEROES && + flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP ) { +dev_dbg (&vdpasim->vdpa.dev, +"write_zeroes unexpected flags set - flags: 0x%x\n" , + flags); + status =VIRTIO_BLK_S_UNSUPP ; +break ; + } + +if (!vdpasim_blk_check_range (vdpasim, sector, num_sectors, +VDPASIM_BLK_DWZ_MAX_SECTORS )) { + status =VIRTIO_BLK_S_IOERR ; +break ; + } + +if (type ==VIRTIO_BLK_T_WRITE_ZEROES ) { +memset (vdpasim->buffer + offset,0 , + num_sectors <<SECTOR_SHIFT ); + } + +break ; + }default :dev_dbg (&vdpasim->vdpa.dev,"Unsupported request type %d\n" , type);
@@ -281,6 +342,17 @@ static void vdpasim_blk_get_config(struct vdpasim *vdpasim, void *config) blk_config->min_io_size =cpu_to_vdpasim16 (vdpasim,1 ); blk_config->opt_io_size =cpu_to_vdpasim32 (vdpasim,1 ); blk_config->blk_size =cpu_to_vdpasim32 (vdpasim,SECTOR_SIZE ); +/* VIRTIO_BLK_F_DISCARD */ + blk_config->discard_sector_alignment = +cpu_to_vdpasim32 (vdpasim,SECTOR_SIZE ); + blk_config->max_discard_sectors = +cpu_to_vdpasim32 (vdpasim,VDPASIM_BLK_DWZ_MAX_SECTORS ); + blk_config->max_discard_seg =cpu_to_vdpasim32 (vdpasim,1 ); +/* VIRTIO_BLK_F_WRITE_ZEROES */ + blk_config->max_write_zeroes_sectors = +cpu_to_vdpasim32 (vdpasim,VDPASIM_BLK_DWZ_MAX_SECTORS ); + blk_config->max_write_zeroes_seg =cpu_to_vdpasim32 (vdpasim,1 ); + }static void vdpasim_blk_mgmtdev_release (struct device *dev)
Signature · 6 lines
2.37.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization