vfio/migration: Fix page size calculation
PATCH 1/1 vfio/migration: Fix page size calculation
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice bcontainer->dirty_pgsizes always have some common page sizes when dirty tracking is supported.
Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188
--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/migration.c +1 -1
@@ -1173,7 +1173,7 @@ static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)* can also switch to use IOMMUFD backend if there is a need to migrate * large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size;return current_machine->ram_size > max_size;
Signature · 1 line
2.47.1
Quoted 21 lines · Zhenzhong Duan
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice
bcontainer->dirty_pgsizesalways have some common page sizes when dirty tracking is supported.Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188Fixes 46c763311419 ("vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure").Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/migration.c +0
-0
hw/vfio/migration.c static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev) · line 1179
Quoted 8 lines
hw/vfio/migration.c +1
-1@@ -1173,7 +1173,7 @@ static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)* can also switch to use IOMMUFD backend if there is a need to migrate * large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size;return current_machine->ram_size > max_size;
Thanks,
C.
Cédric Le Goater
AppliedQuoted 21 lines · Zhenzhong Duan
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice
bcontainer->dirty_pgsizesalways have some common page sizes when dirty tracking is supported.Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188Fixes 46c763311419 ("vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure").Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/migration.c +0
-0
hw/vfio/migration.c static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev) · line 1179
Quoted 8 lines
hw/vfio/migration.c +1
-1@@ -1173,7 +1173,7 @@ static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)* can also switch to use IOMMUFD backend if there is a need to migrate * large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size;return current_machine->ram_size > max_size;
Applied to vfio-next.
Thanks,
C.
Quoted 22 lines · Zhenzhong Duan
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice
bcontainer->dirty_pgsizesalways have some common page sizes when dirty tracking is supported.Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188Fixes 46c763311419 ("vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure").Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/migration.c +0
-0
hw/vfio/migration.c static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev) · line 1177
Quoted 6 lines
hw/vfio/migration.c +1
-1@@ -1173,7 +1173,7 @@ static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)* can also switch to use IOMMUFD backend if there is a need to migrate * large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size;
This doesn't strictly speaking resolve CID 1644186, because what Coverity sees is that ctz64() returns 64 when dirty_pgsizes is zero. That is still UB even with the ULL suffix.
But if we are enforcing somewhere that dirty_pgsizes is never zero, then this is fine and we can mark that Coverity issue as a false-positive.
thanks
-- PMM
Quoted 39 lines
-----Original Message-----
From: Peter Maydell <[email protected]>
Subject: Re: [PATCH] vfio/migration: Fix page size calculationQuoted 11 lines · Zhenzhong Duan
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice
bcontainer->dirty_pgsizesalways have some common page sizes when dirty tracking is supported.Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188Fixes 46c763311419 ("vfio/migration: Add migration blocker if VM memoryis too large to cause
unmap_bitmapfailure").hw/vfio/migration.c static bool
Quoted 11 lines
Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/migration.c +0
-0@@ -1173,7 +1173,7 @@ static bool
vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)hw/vfio/migration.c static bool · line 1173
Quoted 1 line
hw/vfio/migration.c +0
-0* can also switch to use IOMMUFD backend if there is a need to migrate
hw/vfio/migration.c static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)
Quoted 5 lines
hw/vfio/migration.c +1
-1* large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size * BITS_PER_BYTE *
Quoted 5 lines
page_size;This doesn't strictly speaking resolve CID 1644186, because what Coverity sees is that
ctz64()returns 64 whendirty_pgsizesis zero. That is still UB even with the ULL suffix.
Hmm, maybe, I didn't find easy way to verify it with Coverity test. In fact, this code is guarded by below check:
if (!bcontainer->dirty_pages_supported) {
return false;
}If the check pass, we are sure dirty_pgsizes has host page size bit set at least:
if (cap_mig->pgsize_bitmap & qemu_real_host_page_size()) {
bcontainer->dirty_pages_supported = true;
bcontainer->max_dirty_bitmap_size = cap_mig->max_dirty_bitmap_size;
bcontainer->dirty_pgsizes = cap_mig->pgsize_bitmap;
}Quoted 4 lines
But if we are enforcing somewhere that
dirty_pgsizesis never zero, then this is fine and we can mark that Coverity issue as a false-positive.
May I ask how to mark it?
Maybe Coverity test is smart enough to not report in this case😊
Thanks
Zhenzhong
Quoted 62 lines · Zhenzhong
Quoted 40 lines
-----Original Message-----
From: Peter Maydell <[email protected]>
Subject: Re: [PATCH] vfio/migration: Fix page size calculationQuoted 11 lines · Zhenzhong Duan
Coverity detected an issue of left shifting int by more than 31 bits leading to undefined behavior.
In practice
bcontainer->dirty_pgsizesalways have some common page sizes when dirty tracking is supported.Resolves: Coverity CID 1644186
Resolves: Coverity CID 1644187
Resolves: Coverity CID 1644188Fixes 46c763311419 ("vfio/migration: Add migration blocker if VM memoryis too large to cause
unmap_bitmapfailure").hw/vfio/migration.c static bool
Quoted 11 lines
Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/migration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/migration.c +0
-0@@ -1173,7 +1173,7 @@ static bool
vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)hw/vfio/migration.c static bool · line 1173
Quoted 1 line
hw/vfio/migration.c +0
-0* can also switch to use IOMMUFD backend if there is a need to migrate
hw/vfio/migration.c static bool vfio_dirty_tracking_exceed_limit(VFIODevice *vbasedev)
Quoted 5 lines
hw/vfio/migration.c +1
-1* large VM. */ - page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + page_size =1ULL <<ctz64 (bcontainer->dirty_pgsizes); max_size = bcontainer->max_dirty_bitmap_size * BITS_PER_BYTE *Quoted 5 lines
page_size;This doesn't strictly speaking resolve CID 1644186, because what Coverity sees is that
ctz64()returns 64 whendirty_pgsizesis zero. That is still UB even with the ULL suffix.Hmm, maybe, I didn't find easy way to verify it with Coverity test. In fact, this code is guarded by below check:
if (!bcontainer->dirty_pages_supported) { return false; }If the check pass, we are sure
dirty_pgsizeshas host page size bit set at least:if (cap_mig->pgsize_bitmap & qemu_real_host_page_size()) { bcontainer->dirty_pages_supported = true; bcontainer->max_dirty_bitmap_size = cap_mig->max_dirty_bitmap_size; bcontainer->dirty_pgsizes = cap_mig->pgsize_bitmap; }Quoted 1 line
Quoted 6 lines
Quoted 3 lines
But if we are enforcing somewhere that
dirty_pgsizesis never zero, then this is fine and we can mark that Coverity issue as a false-positive.May I ask how to mark it?
Maybe Coverity test is smart enough to not report in this case😊
It's usually not that clever. I'll mark the relevant issues as false-positive in the web UI.
Thanks for confirming that there's a "definitely at least one bit set" check already.
-- PMM