[Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD

Alexander Ivanov alexander.ivanov at virtuozzo.com
Mon Nov 23 11:57:28 MSK 2026


>> @@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
>> -	if (vhost_blk_backend_ro(f) || !max_sects ||
>> -	    nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects ||
>> +	if (vhost_blk_backend_ro(f) ||
>> +	    (!discard && (!max_sects || nr_sects > max_sects)) ||
>> +	    nr_sects > UINT_MAX >> SECTOR_SHIFT ||
>>   	    (sector | nr_sects) & bs_mask ||
>>   	    sector > bdev_nr_sectors(bdev) ||
>>   	    nr_sects > bdev_nr_sectors(bdev) - sector) {
>>   		*status = VIRTIO_BLK_S_IOERR;
> [Severity: Low]
> Can a valid discard of 4 GiB or more fail with VIRTIO_BLK_S_IOERR here? The commit message treats discard as a hint that is not checked against backend limits. For discard, though, the nr_sects > UINT_MAX >> SECTOR_SHIFT check still returns IOERR.
>
> If the VMM leaves max_discard_sectors at 0, drivers/block/virtio_blk.c sets max_hw_discard_sectors to UINT_MAX. The guest can then merge discards well past this cap. With max_discard_seg = 1, the merged range goes out as a single segment. fstrim or blkdiscard on a large range would then report errors. QEMU does not fail these requests.
>
> Should vhost_blk_bio_make() split a large discard across several bios? Alternatively, the commit message could tell the VMM to cap max_discard_sectors, as the write-zeroes commit did for its limit.
>
> Carry the trailer below into the next revision and Sashiko will not review this patch again.
>
> Reviewed-by: Sashiko <no-reply at sashiko.virtuozzo.com>
A Linux guest cannot send a discard of 4 GiB or more: 
blk_queue_get_max_sectors()
caps discard requests at UINT_MAX >> SECTOR_SHIFT, whatever 
max_discard_sectors says,
so fstrim/blkdiscard will not hit IOERR. QEMU never advertises 0 there 
either, and it fails a
discard above its advertised limit with VIRTIO_BLK_S_IOERR too. 
Splitting is not worth the
complexity.


More information about the Devel mailing list