[PATCH] file-posix: Fix crash on discard_granularity == 0

Kevin Wolf posted 1 patch 6 months, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250429155654.102735-1-kwolf@redhat.com
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
block/file-posix.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] file-posix: Fix crash on discard_granularity == 0
Posted by Kevin Wolf 6 months, 2 weeks ago
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.

Fixes: f605796aae4 ('file-posix: probe discard alignment on Linux block devices')
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/file-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/file-posix.c b/block/file-posix.c
index 0d6e12f880..0d85123d0f 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -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 */
-- 
2.49.0
Re: [PATCH] file-posix: Fix crash on discard_granularity == 0
Posted by Stefan Hajnoczi 6 months, 2 weeks ago
On Tue, Apr 29, 2025 at 05:56:54PM +0200, Kevin Wolf wrote:
> 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.
> 
> Fixes: f605796aae4 ('file-posix: probe discard alignment on Linux block devices')
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  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
Re: [PATCH] file-posix: Fix crash on discard_granularity == 0
Posted by Stefan Hajnoczi 6 months, 2 weeks ago
On Tue, Apr 29, 2025 at 05:56:54PM +0200, Kevin Wolf wrote:
> 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.
> 
> Fixes: f605796aae4 ('file-posix: probe discard alignment on Linux block devices')
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/file-posix.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Re: [PATCH] file-posix: Fix crash on discard_granularity == 0
Posted by Eric Blake 6 months, 2 weeks ago
On Tue, Apr 29, 2025 at 05:56:54PM +0200, Kevin Wolf wrote:
> 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.
> 
> Fixes: f605796aae4 ('file-posix: probe discard alignment on Linux block devices')
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/file-posix.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Eric Blake <eblake@redhat.com>

(Coverity has proven its worth, yet again)

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
Re: [PATCH] file-posix: Fix crash on discard_granularity == 0
Posted by Kevin Wolf 6 months, 2 weeks ago
Am 29.04.2025 um 23:09 hat Eric Blake geschrieben:
> On Tue, Apr 29, 2025 at 05:56:54PM +0200, Kevin Wolf wrote:
> > 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.
> > 
> > Fixes: f605796aae4 ('file-posix: probe discard alignment on Linux block devices')
> > Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> > ---
> >  block/file-posix.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> Reviewed-by: Eric Blake <eblake@redhat.com>
> 
> (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