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

virtio-blk: Remove BUG_ON() in virtio_queue_rq()

Currently we have a BUG_ON() to make sure the number of sg
list does not exceed queue_max_segments() in virtio_queue_rq().
However, the block layer uses queue_max_discard_segments()
instead of queue_max_segments() to limit the sg list for
discard requests. So the BUG_ON() might be triggered if
virtio-blk device reports a larger value for max discard
segment than queue_max_segments(). To fix it, let's simply
remove the BUG_ON() which has become unnecessary after commit
02746e26c39e("virtio-blk: avoid preallocating big SGL for data").
And the unused vblk->sg_elems can also be removed together.

Fixes: 1f23816b8eb8 ("virtio_blk: add discard and write zeroes support")
Suggested-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Link: https://lore.kernel.org/r/20220304100058.116-2-xieyongji@bytedance.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Xie Yongji and committed by
Michael S. Tsirkin
e030759a dacc73ed

+2 -10
+2 -10
drivers/block/virtio_blk.c
··· 76 76 */ 77 77 refcount_t refs; 78 78 79 - /* What host tells us, plus 2 for header & tailer. */ 80 - unsigned int sg_elems; 81 - 82 79 /* Ida index - used to track minor number allocations. */ 83 80 int index; 84 81 ··· 318 321 bool notify = false; 319 322 blk_status_t status; 320 323 int err; 321 - 322 - BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems); 323 324 324 325 status = virtblk_setup_cmd(vblk->vdev, req, vbr); 325 326 if (unlikely(status)) ··· 778 783 /* Prevent integer overflows and honor max vq size */ 779 784 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2); 780 785 781 - /* We need extra sg elements at head and tail. */ 782 - sg_elems += 2; 783 786 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL); 784 787 if (!vblk) { 785 788 err = -ENOMEM; ··· 789 796 mutex_init(&vblk->vdev_mutex); 790 797 791 798 vblk->vdev = vdev; 792 - vblk->sg_elems = sg_elems; 793 799 794 800 INIT_WORK(&vblk->config_work, virtblk_config_changed_work); 795 801 ··· 845 853 set_disk_ro(vblk->disk, 1); 846 854 847 855 /* We can handle whatever the host told us to handle. */ 848 - blk_queue_max_segments(q, vblk->sg_elems-2); 856 + blk_queue_max_segments(q, sg_elems); 849 857 850 858 /* No real sector limit. */ 851 859 blk_queue_max_hw_sectors(q, -1U); ··· 923 931 * handled it. 924 932 */ 925 933 if (!v) 926 - v = sg_elems - 2; 934 + v = sg_elems; 927 935 blk_queue_max_discard_segments(q, 928 936 min(v, MAX_DISCARD_SEGMENTS)); 929 937