Skip to content

Commit 4ff3d01

Browse files
johnpgarryaxboe
authored andcommitted
virtio_blk: Fix default logical block size fallback
If we fail to read a logical block size in virtblk_read_limits() -> virtio_cread_feature(), then we default to what is in lim->logical_block_size, but that would be 0. We can deal with lim->logical_block_size = 0 later in the blk_mq_alloc_disk(), but the code in virtblk_read_limits() needs a proper default, so give a default of SECTOR_SIZE. Fixes: 27e32cd ("block: pass a queue_limits argument to blk_mq_alloc_disk") Reviewed-by: Christoph Hellwig <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Signed-off-by: John Garry <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 6b43537 commit 4ff3d01

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

drivers/block/virtio_blk.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ static int virtblk_read_limits(struct virtio_blk *vblk,
12501250
struct queue_limits *lim)
12511251
{
12521252
struct virtio_device *vdev = vblk->vdev;
1253-
u32 v, blk_size, max_size, sg_elems, opt_io_size;
1253+
u32 v, max_size, sg_elems, opt_io_size;
12541254
u32 max_discard_segs = 0;
12551255
u32 discard_granularity = 0;
12561256
u16 min_io_size;
@@ -1291,44 +1291,43 @@ static int virtblk_read_limits(struct virtio_blk *vblk,
12911291
/* Host can optionally specify the block size of the device */
12921292
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
12931293
struct virtio_blk_config, blk_size,
1294-
&blk_size);
1294+
&lim->logical_block_size);
12951295
if (!err) {
1296-
err = blk_validate_block_size(blk_size);
1296+
err = blk_validate_block_size(lim->logical_block_size);
12971297
if (err) {
12981298
dev_err(&vdev->dev,
12991299
"virtio_blk: invalid block size: 0x%x\n",
1300-
blk_size);
1300+
lim->logical_block_size);
13011301
return err;
13021302
}
1303-
1304-
lim->logical_block_size = blk_size;
1305-
} else
1306-
blk_size = lim->logical_block_size;
1303+
}
13071304

13081305
/* Use topology information if available */
13091306
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
13101307
struct virtio_blk_config, physical_block_exp,
13111308
&physical_block_exp);
13121309
if (!err && physical_block_exp)
1313-
lim->physical_block_size = blk_size * (1 << physical_block_exp);
1310+
lim->physical_block_size =
1311+
lim->logical_block_size * (1 << physical_block_exp);
13141312

13151313
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
13161314
struct virtio_blk_config, alignment_offset,
13171315
&alignment_offset);
13181316
if (!err && alignment_offset)
1319-
lim->alignment_offset = blk_size * alignment_offset;
1317+
lim->alignment_offset =
1318+
lim->logical_block_size * alignment_offset;
13201319

13211320
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
13221321
struct virtio_blk_config, min_io_size,
13231322
&min_io_size);
13241323
if (!err && min_io_size)
1325-
lim->io_min = blk_size * min_io_size;
1324+
lim->io_min = lim->logical_block_size * min_io_size;
13261325

13271326
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
13281327
struct virtio_blk_config, opt_io_size,
13291328
&opt_io_size);
13301329
if (!err && opt_io_size)
1331-
lim->io_opt = blk_size * opt_io_size;
1330+
lim->io_opt = lim->logical_block_size * opt_io_size;
13321331

13331332
if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
13341333
virtio_cread(vdev, struct virtio_blk_config,
@@ -1422,7 +1421,7 @@ static int virtblk_read_limits(struct virtio_blk *vblk,
14221421
lim->discard_granularity =
14231422
discard_granularity << SECTOR_SHIFT;
14241423
else
1425-
lim->discard_granularity = blk_size;
1424+
lim->discard_granularity = lim->logical_block_size;
14261425
}
14271426

14281427
if (virtio_has_feature(vdev, VIRTIO_BLK_F_ZONED)) {
@@ -1453,6 +1452,7 @@ static int virtblk_probe(struct virtio_device *vdev)
14531452
struct virtio_blk *vblk;
14541453
struct queue_limits lim = {
14551454
.features = BLK_FEAT_ROTATIONAL,
1455+
.logical_block_size = SECTOR_SIZE,
14561456
};
14571457
int err, index;
14581458
unsigned int queue_depth;

0 commit comments

Comments
 (0)