file-posix: Fix crash on discard_granularity == 0
PATCH 1/1 file-posix: Fix crash on discard_granularity == 0
Block devices that don't support discard have a discard_granularity of
0. Currently, this results in a division by zero when we try to make sure that it's a multiple of request_alignment. Only try to update bs->bl.pdiscard_alignment when we got a non-zero discard_granularity from sysfs.
--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/file-posix.c +1 -1
@@ -1573,7 +1573,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)int ret; ret =hdev_get_pdiscard_alignment (&st, &dalign); -if (ret ==0 ) { +if (ret ==0 && dalign !=0 ) {uint32_t ralign = bs->bl.request_alignment;/* Probably never happens, but handle it just in case */
Signature · 1 line
2.49.0
Quoted 11 lines · Kevin Wolf
Block devices that don't support discard have a
discard_granularityof
0. Currently, this results in a division by zero when we try to make sure that it's a multiple ofrequest_alignment. Only try to updatebs->bl.pdiscard_alignmentwhen we got a non-zerodiscard_granularityfrom sysfs.Fixes f605796aae4 ('file-posix: probe discard alignment on Linux block devices')Signed-off-by Kevin Wolf <[email protected]>--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
(Coverity has proven its worth, yet again)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Am 29.04.2025 um 23:09 hat Eric Blake geschrieben:
Quoted 13 lines · Kevin Wolf
Quoted 11 lines
Block devices that don't support discard have a
discard_granularityof
0. Currently, this results in a division by zero when we try to make sure that it's a multiple ofrequest_alignment. Only try to updatebs->bl.pdiscard_alignmentwhen we got a non-zerodiscard_granularityfrom sysfs.Fixes f605796aae4 ('file-posix: probe discard alignment on Linux block devices')Signed-off-by Kevin Wolf <[email protected]>--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines
Reviewed-by Eric Blake <[email protected]>(Coverity has proven its worth, yet again)
Oh, interesting, you're right, Coverity flagged this, too. I got its notification a few hours after sending the patch. :-)
I actually ran into this myself with an iscsi disk I used for testing the multipath failover stuff.
Kevin
Quoted 11 lines · Kevin Wolf
Block devices that don't support discard have a
discard_granularityof
0. Currently, this results in a division by zero when we try to make sure that it's a multiple ofrequest_alignment. Only try to updatebs->bl.pdiscard_alignmentwhen we got a non-zerodiscard_granularityfrom sysfs.Fixes f605796aae4 ('file-posix: probe discard alignment on Linux block devices')Signed-off-by Kevin Wolf <[email protected]>--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Stefan Hajnoczi
AppliedQuoted 11 lines · Kevin Wolf
Block devices that don't support discard have a
discard_granularityof
0. Currently, this results in a division by zero when we try to make sure that it's a multiple ofrequest_alignment. Only try to updatebs->bl.pdiscard_alignmentwhen we got a non-zerodiscard_granularityfrom sysfs.Fixes f605796aae4 ('file-posix: probe discard alignment on Linux block devices')Signed-off-by Kevin Wolf <[email protected]>--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block
Stefan