diff mbox series

[5/8] migration/block: replace uses of blk_nb_sectors that do not check result

Message ID 20230407153303.391121-6-pbonzini@redhat.com
State New
Headers show
Series block: remove bdrv_co_get_geometry coroutines from I/O hot path | expand

Commit Message

Paolo Bonzini April 7, 2023, 3:33 p.m. UTC
Uses of blk_nb_sectors must check whether the result is negative.
Otherwise, underflow can happen.  Fortunately, alloc_aio_bitmap()
and bmds_aio_inflight() both have an alternative way to retrieve the
number of sectors in the file.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 migration/block.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Eric Blake April 7, 2023, 7:49 p.m. UTC | #1
On Fri, Apr 07, 2023 at 05:33:00PM +0200, Paolo Bonzini wrote:
> Uses of blk_nb_sectors must check whether the result is negative.
> Otherwise, underflow can happen.  Fortunately, alloc_aio_bitmap()
> and bmds_aio_inflight() both have an alternative way to retrieve the
> number of sectors in the file.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  migration/block.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

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

Patch

diff --git a/migration/block.c b/migration/block.c
index 426a25bb192e..b2497bbd329c 100644
--- a/migration/block.c
+++ b/migration/block.c
@@ -195,7 +195,7 @@  static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
 {
     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
 
-    if (sector < blk_nb_sectors(bmds->blk)) {
+    if (sector < bmds->total_sectors) {
         return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
             (1UL << (chunk % (sizeof(unsigned long) * 8))));
     } else {
@@ -229,10 +229,9 @@  static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
 
 static void alloc_aio_bitmap(BlkMigDevState *bmds)
 {
-    BlockBackend *bb = bmds->blk;
     int64_t bitmap_size;
 
-    bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
+    bitmap_size = bmds->total_sectors + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
     bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
 
     bmds->aio_bitmap = g_malloc0(bitmap_size);