[PATCH] icount: don't adjust virtual time backwards after warp

Nicholas Piggin posted 1 patch 10 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20230627061406.241847-1-npiggin@gmail.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>
softmmu/icount.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] icount: don't adjust virtual time backwards after warp
Posted by Nicholas Piggin 10 months, 3 weeks ago
The icount-based QEMU_CLOCK_VIRTUAL runs ahead of the RT clock at times.
When warping, it is possible it is still ahead at the end of the warp,
which causes icount adaptive mode to adjust it backward. This can result
in the machine observing time going backwards.

Prevent this by clamping adaptive adjustment to 0 at minimum.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 softmmu/icount.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/softmmu/icount.c b/softmmu/icount.c
index 4504433e16..486ea7ef41 100644
--- a/softmmu/icount.c
+++ b/softmmu/icount.c
@@ -259,11 +259,16 @@ static void icount_warp_rt(void)
         warp_delta = clock - timers_state.vm_clock_warp_start;
         if (icount_enabled() == 2) {
             /*
-             * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
-             * far ahead of real time.
+             * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far
+             * ahead of real time (it might already be ahead so careful not
+             * to go backwards).
              */
             int64_t cur_icount = icount_get_locked();
             int64_t delta = clock - cur_icount;
+
+            if (delta < 0) {
+                delta = 0;
+            }
             warp_delta = MIN(warp_delta, delta);
         }
         qatomic_set_i64(&timers_state.qemu_icount_bias,
-- 
2.40.1
Re: [PATCH] icount: don't adjust virtual time backwards after warp
Posted by Paolo Bonzini 10 months, 3 weeks ago
On 6/27/23 08:14, Nicholas Piggin wrote:
> The icount-based QEMU_CLOCK_VIRTUAL runs ahead of the RT clock at times.
> When warping, it is possible it is still ahead at the end of the warp,
> which causes icount adaptive mode to adjust it backward. This can result
> in the machine observing time going backwards.
> 
> Prevent this by clamping adaptive adjustment to 0 at minimum.

Queued, thanks.

Paolo

> Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
> ---
>   softmmu/icount.c | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/softmmu/icount.c b/softmmu/icount.c
> index 4504433e16..486ea7ef41 100644
> --- a/softmmu/icount.c
> +++ b/softmmu/icount.c
> @@ -259,11 +259,16 @@ static void icount_warp_rt(void)
>           warp_delta = clock - timers_state.vm_clock_warp_start;
>           if (icount_enabled() == 2) {
>               /*
> -             * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
> -             * far ahead of real time.
> +             * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far
> +             * ahead of real time (it might already be ahead so careful not
> +             * to go backwards).
>                */
>               int64_t cur_icount = icount_get_locked();
>               int64_t delta = clock - cur_icount;
> +
> +            if (delta < 0) {
> +                delta = 0;
> +            }
>               warp_delta = MIN(warp_delta, delta);
>           }
>           qatomic_set_i64(&timers_state.qemu_icount_bias,