Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio-blk: Don't use MAX_DISCARD_SEGMENTS if max_discard_seg is zero

Currently the value of max_discard_segment will be set to
MAX_DISCARD_SEGMENTS (256) with no basis in hardware if device
set 0 to max_discard_seg in configuration space. It's incorrect
since the device might not be able to handle such large descriptors.
To fix it, let's follow max_segments restrictions in this case.

Fixes: 1f23816b8eb8 ("virtio_blk: add discard and write zeroes support")
Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Link: https://lore.kernel.org/r/20220304100058.116-1-xieyongji@bytedance.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Xie Yongji and committed by
Michael S. Tsirkin
dacc73ed e2ae38cf

+8 -2
+8 -2
drivers/block/virtio_blk.c
··· 925 925 926 926 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg, 927 927 &v); 928 + 929 + /* 930 + * max_discard_seg == 0 is out of spec but we always 931 + * handled it. 932 + */ 933 + if (!v) 934 + v = sg_elems - 2; 928 935 blk_queue_max_discard_segments(q, 929 - min_not_zero(v, 930 - MAX_DISCARD_SEGMENTS)); 936 + min(v, MAX_DISCARD_SEGMENTS)); 931 937 932 938 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 933 939 }