migration: Fix possible division by zero on calc expected downtime
PATCH 1/1 migration: Fix possible division by zero on calc expected downtime
Commit dd4fe8844b changed the reporting of expected downtime behavior, so that the value will be calculated on-demand. One side effect on the change is QEMU will allow the calculation to happen anytime even if there's no transfer happening for a short while.
PeterM reported an ubsan report from clang when running migration-test with aarch64 binary on x86_64 hosts. I can also reproduce if I run the test concurrently so some of the src QEMU may not get chance to push any data, causing mbps to be 0:
../migration/migration.c:1051:12: runtime error: -nan is outside the range of representable values of type 'long'
Fix it by checking mbps non-zero before the devision.
--- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
migration/migration.c +8 -2
@@ -1044,12 +1044,18 @@ static bool migrate_show_downtime(MigrationState *s)/* Return expected downtime (unit: milliseconds) */ int64_t migration_downtime_calc_expected (MigrationState *s) { +double bw; +if (mig_stats.dirty_sync_count <=1 ) {return migrate_downtime_limit (); } -return mig_stats.dirty_bytes_last_sync / -migration_get_switchover_bw (s) *1000 ; + bw =migration_get_switchover_bw (s) *1000 ; +if (!bw) { +return INT64_MAX ; + } + +return mig_stats.dirty_bytes_last_sync / bw; }static void populate_time_info (MigrationInfo *info,MigrationState *s)
Signature · 1 line
2.53.0
Quoted 35 lines · Peter Xu
Commit
dd4fe8844bchanged the reporting of expected downtime behavior, so that the value will be calculated on-demand. One side effect on the change is QEMU will allow the calculation to happen anytime even if there's no transfer happening for a short while.PeterM reported an ubsan report from clang when running migration-test with aarch64 binary on
x86_64hosts. I can also reproduce if I run the test concurrently so some of the src QEMU may not get chance to push any data, causing mbps to be 0:
../migration/migration.c:1051:12: runtime error: -nan is outside the range of representable values of type 'long'Fix it by checking mbps non-zero before the devision.
Link https://lore.kernel.org/r/CAFEAcA-MYH6C39xO0OLx4-M5pKurJpurwRsMqZe9q=W-NShAbw@mail.gmail.com open hereReported-by Peter Maydell <[email protected]>Fixes dd4fe8844b ("migration: Calculate expected downtime on demand")Signed-off-by Peter Xu <[email protected]>--- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)migration/migration.c +2
-0@@ -1044,12 +1044,18 @@ static bool migrate_show_downtime(MigrationState *s)/* Return expected downtime (unit: milliseconds) */ int64_t migration_downtime_calc_expected (MigrationState *s) { +double bw; +if (mig_stats.dirty_sync_count <=1 ) {return migrate_downtime_limit ();
migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1058
Quoted 10 lines
migration/migration.c +6
-2} -return mig_stats.dirty_bytes_last_sync / -migration_get_switchover_bw (s) *1000 ; + bw =migration_get_switchover_bw (s) *1000 ; +if (!bw) { +return INT64_MAX ; + } + +return mig_stats.dirty_bytes_last_sync / bw;
I think this is still UB if bw is a value that's nonzero but still small enough that mig_stats.dirty_bytes_last_sync / bw is greater than INT64_MAX. It might be more robust to do something like:
double expected = mig_stats.dirty_bytes_last_sync /
migration_get_switchover_bw(s) * 1000;
if (expected >= (double)INT64_MAX) {
return INT64_MAX;
}
return (int64_t)expected;(which will also handle the infinity case, because in IEEE fp infinity is larger than any finite value).
-- PMM
Quoted 60 lines · Peter Maydell
Quoted 35 lines · Peter Xu
Commit
dd4fe8844bchanged the reporting of expected downtime behavior, so that the value will be calculated on-demand. One side effect on the change is QEMU will allow the calculation to happen anytime even if there's no transfer happening for a short while.PeterM reported an ubsan report from clang when running migration-test with aarch64 binary on
x86_64hosts. I can also reproduce if I run the test concurrently so some of the src QEMU may not get chance to push any data, causing mbps to be 0:
../migration/migration.c:1051:12: runtime error: -nan is outside the range of representable values of type 'long'Fix it by checking mbps non-zero before the devision.
Link https://lore.kernel.org/r/CAFEAcA-MYH6C39xO0OLx4-M5pKurJpurwRsMqZe9q=W-NShAbw@mail.gmail.com open hereReported-by Peter Maydell <[email protected]>Fixes dd4fe8844b ("migration: Calculate expected downtime on demand")Signed-off-by Peter Xu <[email protected]>--- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)migration/migration.c +2
-0@@ -1044,12 +1044,18 @@ static bool migrate_show_downtime(MigrationState *s)/* Return expected downtime (unit: milliseconds) */ int64_t migration_downtime_calc_expected (MigrationState *s) { +double bw; +if (mig_stats.dirty_sync_count <=1 ) {return migrate_downtime_limit ();migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1058
Quoted 10 lines
migration/migration.c +6
-2} -return mig_stats.dirty_bytes_last_sync / -migration_get_switchover_bw (s) *1000 ; + bw =migration_get_switchover_bw (s) *1000 ; +if (!bw) { +return INT64_MAX ; + } + +return mig_stats.dirty_bytes_last_sync / bw;I think this is still UB if bw is a value that's nonzero but still small enough that mig_stats.dirty_bytes_last_sync / bw is greater than
INT64_MAX. It might be more robust to do something like:double expected = mig_stats.dirty_bytes_last_sync / migration_get_switchover_bw(s) * 1000; if (expected >= (double)INT64_MAX) { return INT64_MAX; } return (int64_t)expected;
Quoted 2 lines
(which will also handle the infinity case, because in IEEE fp infinity is larger than any finite value).
I almost treated mbps as integer, because when it's non-zero it almost should be at least ~10byte/s (our unit is (uint)transferred / 100ms).. so I doubt if the issue can happen. But indeed it's theoretically flawed.
I'll repost, thanks for the careful review, Peter.
Signature · 1 line
Peter Xu
Quoted 39 lines · Peter Xu
Quoted 38 lines · Peter Maydell
Quoted 37 lines · Peter Xu
Commit
dd4fe8844bchanged the reporting of expected downtime behavior, so that the value will be calculated on-demand. One side effect on the change is QEMU will allow the calculation to happen anytime even if there's no transfer happening for a short while.PeterM reported an ubsan report from clang when running migration-test with aarch64 binary on
x86_64hosts. I can also reproduce if I run the test concurrently so some of the src QEMU may not get chance to push any data, causing mbps to be 0:
../migration/migration.c:1051:12: runtime error: -nan is outside the range of representable values of type 'long'Fix it by checking mbps non-zero before the devision.
Link https://lore.kernel.org/r/CAFEAcA-MYH6C39xO0OLx4-M5pKurJpurwRsMqZe9q=W-NShAbw@mail.gmail.com open hereReported-by Peter Maydell <[email protected]>Fixes dd4fe8844b ("migration: Calculate expected downtime on demand")Signed-off-by Peter Xu <[email protected]>--- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)migration/migration.c +2
-0@@ -1044,12 +1044,18 @@ static bool migrate_show_downtime(MigrationState *s)/* Return expected downtime (unit: milliseconds) */ int64_t migration_downtime_calc_expected (MigrationState *s) { +double bw; +if (mig_stats.dirty_sync_count <=1 ) {return migrate_downtime_limit (); }
Quoted 3 lines
Quoted 3 lines
migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1053
Quoted 3 lines
migration/migration.c +1
-2-return mig_stats.dirty_bytes_last_sync / -migration_get_switchover_bw (s) *1000 ; + bw =migration_get_switchover_bw (s) *1000 ;
PS: I think I got the math wrong, not that it matters anymore.. :(
Quoted 11 lines
Quoted 11 lines
migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1058
Quoted 5 lines
migration/migration.c +5
-0+if (!bw) { +return INT64_MAX ; + } + +return mig_stats.dirty_bytes_last_sync / bw;I think this is still UB if bw is a value that's nonzero but still small enough that mig_stats.dirty_bytes_last_sync / bw is greater than
INT64_MAX. It might be more robust to do something like:
Quoted 6 lines
Quoted 6 lines
double expected = mig_stats.dirty_bytes_last_sync / migration_get_switchover_bw(s) * 1000; if (expected >= (double)INT64_MAX) { return INT64_MAX; }
This actually fails to fix the ubsan issue and I can still reproduce, because the ubsan issue was a Nan reported, where it seems when Nan found in normal such evaluations it'll return FALSE. I'll need to use:
if (expected < (double)INT64_MAX) {
return (int64_t)expected;
}I'll add a rich comment when repost.
Quoted 13 lines
Quoted 4 lines
return (int64_t)expected;(which will also handle the infinity case, because in IEEE fp infinity is larger than any finite value).
I almost treated mbps as integer, because when it's non-zero it almost should be at least ~10byte/s (our unit is (uint)transferred / 100ms).. so I doubt if the issue can happen. But indeed it's theoretically flawed.
I'll repost, thanks for the careful review, Peter.
Signature · 1 line
Peter Xu
Signature · 1 line
Peter Xu
Quoted 65 lines · Peter Xu
Quoted 39 lines · Peter Xu
Quoted 38 lines · Peter Maydell
Quoted 37 lines · Peter Xu
Commit
dd4fe8844bchanged the reporting of expected downtime behavior, so that the value will be calculated on-demand. One side effect on the change is QEMU will allow the calculation to happen anytime even if there's no transfer happening for a short while.PeterM reported an ubsan report from clang when running migration-test with aarch64 binary on
x86_64hosts. I can also reproduce if I run the test concurrently so some of the src QEMU may not get chance to push any data, causing mbps to be 0:
../migration/migration.c:1051:12: runtime error: -nan is outside the range of representable values of type 'long'Fix it by checking mbps non-zero before the devision.
Link https://lore.kernel.org/r/CAFEAcA-MYH6C39xO0OLx4-M5pKurJpurwRsMqZe9q=W-NShAbw@mail.gmail.com open hereReported-by Peter Maydell <[email protected]>Fixes dd4fe8844b ("migration: Calculate expected downtime on demand")Signed-off-by Peter Xu <[email protected]>--- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)migration/migration.c +2
-0@@ -1044,12 +1044,18 @@ static bool migrate_show_downtime(MigrationState *s)/* Return expected downtime (unit: milliseconds) */ int64_t migration_downtime_calc_expected (MigrationState *s) { +double bw; +if (mig_stats.dirty_sync_count <=1 ) {return migrate_downtime_limit (); }Quoted 3 lines
Quoted 3 lines
migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1053
Quoted 3 lines
migration/migration.c +1
-2-return mig_stats.dirty_bytes_last_sync / -migration_get_switchover_bw (s) *1000 ; + bw =migration_get_switchover_bw (s) *1000 ;PS: I think I got the math wrong, not that it matters anymore.. :(
Quoted 11 lines
Quoted 11 lines
migration/migration.c static bool migrate_show_downtime(MigrationState *s) · line 1058
Quoted 5 lines
migration/migration.c +5
-0+if (!bw) { +return INT64_MAX ; + } + +return mig_stats.dirty_bytes_last_sync / bw;I think this is still UB if bw is a value that's nonzero but still small enough that mig_stats.dirty_bytes_last_sync / bw is greater than
INT64_MAX. It might be more robust to do something like:Quoted 6 lines
Quoted 6 lines
double expected = mig_stats.dirty_bytes_last_sync / migration_get_switchover_bw(s) * 1000; if (expected >= (double)INT64_MAX) { return INT64_MAX; }
Quoted 8 lines
This actually fails to fix the ubsan issue and I can still reproduce, because the ubsan issue was a Nan reported, where it seems when Nan found in normal such evaluations it'll return FALSE. I'll need to use:
if (expected < (double)INT64_MAX) { return (int64_t)expected; }
Hmm, how do we get a NaN ?
-- PMM
Quoted 10 lines · Peter Maydell
Quoted 8 lines
This actually fails to fix the ubsan issue and I can still reproduce, because the ubsan issue was a Nan reported, where it seems when Nan found in normal such evaluations it'll return FALSE. I'll need to use:
if (expected < (double)INT64_MAX) { return (int64_t)expected; }Hmm, how do we get a NaN ?
When mbps is zero (when it reproduces here). I have the comment in v2 explaining it.
Thanks,
Signature · 1 line
Peter Xu
Quoted 11 lines · Peter Xu
Quoted 9 lines · Peter Maydell
Quoted 8 lines
This actually fails to fix the ubsan issue and I can still reproduce, because the ubsan issue was a Nan reported, where it seems when Nan found in normal such evaluations it'll return FALSE. I'll need to use:
if (expected < (double)INT64_MAX) { return (int64_t)expected; }
Quoted 4 lines
Quoted 1 line
Hmm, how do we get a NaN ?
When mbps is zero (when it reproduces here). I have the comment in v2 explaining it.
Oh, I think I see -- we get the NaN if both s->mbps == 0 and also mig_stats.dirty_bytes_last_sync == 0, because that's 0 / 0, which is NaN. If only s->mbps == 0 but dirty_bytes_last_sync is not zero, then we get +Inf.
thanks
-- PMM