* [PATCH] util/qemu-timer: restore expired timers into the list on snapshot load
@ 2026-12-02 13:20 Arkadiusz Grzelka via qemu development
2026-12-02 16:35 ` Alex Bennée
0 siblings, 1 reply; 2+ messages in thread
From: Arkadiusz Grzelka via qemu development @ 2026-12-02 13:20 UTC (permalink / raw)
To: qemu-devel; +Cc: pbonzini, peterx, farosas, iii, qemu-stable
Since commit f098c32db487, timer_mod_ns() turns a QEMU_CLOCK_VIRTUAL timer
armed already expired into CPU work when record/replay is active, so
that its callback is ordered against CPU work deterministically.
timer_get() restores timers from a snapshot through timer_mod_ns() as
well. A snapshot taken in replay at the exact instruction count where a
virtual timer is due holds that timer pending and expired, and the log
at that point expects the CHECKPOINT_CLOCK_VIRTUAL at which it fires.
On load the timer is converted to CPU work, its callback runs without
consuming the checkpoint, and nothing is left to consume it: the vCPU
gets no instruction budget and the replay hangs. Reverse debugging hits
this whenever reverse-step or reverse-continue loads such a snapshot.
Such a timer is not armed anew; it was pending when the state was saved
and fired from the timer list in the original run. Add
timer_restore_ns(), which puts the timer back in the list as it was,
and use it in timer_get(). Timers armed by device code, including from
post_load hooks as in the s390x case, still go through timer_mod_ns().
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/5068
Fixes: f098c32db487 ("target/s390x: Fix infinite loop during replay")
Signed-off-by: Arkadiusz Grzelka <arkadiusz.grzelka@arivemb.com>
---
Reproduced:
aarch64 -M virt -cpu cortex-a57, bare-metal guest with the CNTV timer
every 1 ms through the GICv2. Snapshot at a timer checkpoint count:
replay from it killed after 30 s. One instruction later: finishes in 32
ms.
Checked after the fix:
- Replay from the same snapshot: exit 0 in 31 ms.
- gdb: bs and bc across the snapshots work; register state at the
breakpoint identical in all 6 runs (before and after, snapshot offset
0, 1 and init only).
- Not tested: s390x (the case f098c32db487 fixed), upstream functional
replay tests.
include/qemu/timer.h | 15 +++++++++++++++
migration/savevm.c | 2 +-
util/qemu-timer.c | 24 ++++++++++++++++++++----
3 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 9a62ac8cde..2f16865ccc 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -652,6 +652,21 @@ static inline void timer_free(QEMUTimer *ts)
*/
void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
+/**
+ * timer_restore_ns:
+ * @ts: the timer
+ * @expire_time: the expiry time in nanoseconds
+ *
+ * Re-arm a timer from saved state to expire at @expire_time. Unlike
+ * timer_mod_ns(), a timer that has already expired stays in the timer
+ * list in record/replay mode, so that it fires at its recorded
+ * checkpoint.
+ *
+ * This function is thread-safe but the timer and its timer list must not be
+ * freed while this function is running.
+ */
+void timer_restore_ns(QEMUTimer *ts, int64_t expire_time);
+
/**
* timer_mod_anticipate_ns:
* @ts: the timer
diff --git a/migration/savevm.c b/migration/savevm.c
index fe94ba96e4..13a4898b28 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -199,7 +199,7 @@ void timer_get(QEMUFile *f, QEMUTimer *ts)
expire_time = qemu_get_be64(f);
if (expire_time != -1) {
- timer_mod_ns(ts, expire_time);
+ timer_restore_ns(ts, expire_time);
} else {
timer_del(ts);
}
diff --git a/util/qemu-timer.c b/util/qemu-timer.c
index d93a020064..e4e32caff9 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -396,7 +396,8 @@ static void timer_fire(CPUState *cpu, run_on_cpu_data data)
}
static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
- QEMUTimer *ts, int64_t expire_time)
+ QEMUTimer *ts, int64_t expire_time,
+ bool restore)
{
QEMUTimer **pt, *t;
@@ -413,7 +414,7 @@ static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
*
* Convert such timers to CPU work in order to avoid it.
*/
- if (replay_mode != REPLAY_MODE_NONE &&
+ if (!restore && replay_mode != REPLAY_MODE_NONE &&
timer_list->clock->type == QEMU_CLOCK_VIRTUAL &&
!(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL) &&
expire_time <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) {
@@ -464,7 +465,22 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
qemu_mutex_lock(&timer_list->active_timers_lock);
timer_del_locked(timer_list, ts);
- rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
+ rearm = timer_mod_ns_locked(timer_list, ts, expire_time, false);
+ qemu_mutex_unlock(&timer_list->active_timers_lock);
+
+ if (rearm) {
+ timerlist_rearm(timer_list);
+ }
+}
+
+void timer_restore_ns(QEMUTimer *ts, int64_t expire_time)
+{
+ QEMUTimerList *timer_list = ts->timer_list;
+ bool rearm;
+
+ qemu_mutex_lock(&timer_list->active_timers_lock);
+ timer_del_locked(timer_list, ts);
+ rearm = timer_mod_ns_locked(timer_list, ts, expire_time, true);
qemu_mutex_unlock(&timer_list->active_timers_lock);
if (rearm) {
@@ -485,7 +501,7 @@ void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
if (ts->expire_time != -1) {
timer_del_locked(timer_list, ts);
}
- rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
+ rearm = timer_mod_ns_locked(timer_list, ts, expire_time, false);
} else {
rearm = false;
}
base-commit: 3c061218fcf37a98c944ecb88cff56f94f5c0a43
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] util/qemu-timer: restore expired timers into the list on snapshot load
2026-12-02 13:20 [PATCH] util/qemu-timer: restore expired timers into the list on snapshot load Arkadiusz Grzelka via qemu development
@ 2026-12-02 16:35 ` Alex Bennée
0 siblings, 0 replies; 2+ messages in thread
From: Alex Bennée @ 2026-12-02 16:35 UTC (permalink / raw)
To: Arkadiusz Grzelka via qemu development
Cc: Arkadiusz Grzelka, pbonzini, peterx, farosas, iii, qemu-stable
Arkadiusz Grzelka via qemu development <qemu-devel@nongnu.org> writes:
> Since commit f098c32db487, timer_mod_ns() turns a QEMU_CLOCK_VIRTUAL timer
> armed already expired into CPU work when record/replay is active, so
> that its callback is ordered against CPU work deterministically.
>
> timer_get() restores timers from a snapshot through timer_mod_ns() as
> well. A snapshot taken in replay at the exact instruction count where a
> virtual timer is due holds that timer pending and expired, and the log
> at that point expects the CHECKPOINT_CLOCK_VIRTUAL at which it fires.
> On load the timer is converted to CPU work, its callback runs without
> consuming the checkpoint, and nothing is left to consume it: the vCPU
> gets no instruction budget and the replay hangs. Reverse debugging hits
> this whenever reverse-step or reverse-continue loads such a snapshot.
>
> Such a timer is not armed anew; it was pending when the state was saved
> and fired from the timer list in the original run. Add
> timer_restore_ns(), which puts the timer back in the list as it was,
> and use it in timer_get(). Timers armed by device code, including from
> post_load hooks as in the s390x case, still go through timer_mod_ns().
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/5068
> Fixes: f098c32db487 ("target/s390x: Fix infinite loop during replay")
> Signed-off-by: Arkadiusz Grzelka <arkadiusz.grzelka@arivemb.com>
> ---
> Reproduced:
>
> aarch64 -M virt -cpu cortex-a57, bare-metal guest with the CNTV timer
> every 1 ms through the GICv2. Snapshot at a timer checkpoint count:
> replay from it killed after 30 s. One instruction later: finishes in 32
> ms.
I think this would benefit with a test case that replicates the failure
and could be kept as a regression test. It would probably be a
functional test although you could probably create a softmmu tcg test
instead if the code for the guest isn't overly complex.
>
> Checked after the fix:
>
> - Replay from the same snapshot: exit 0 in 31 ms.
> - gdb: bs and bc across the snapshots work; register state at the
> breakpoint identical in all 6 runs (before and after, snapshot offset
> 0, 1 and init only).
> - Not tested: s390x (the case f098c32db487 fixed), upstream functional
> replay tests.
>
> include/qemu/timer.h | 15 +++++++++++++++
> migration/savevm.c | 2 +-
> util/qemu-timer.c | 24 ++++++++++++++++++++----
> 3 files changed, 36 insertions(+), 5 deletions(-)
>
> diff --git a/include/qemu/timer.h b/include/qemu/timer.h
> index 9a62ac8cde..2f16865ccc 100644
> --- a/include/qemu/timer.h
> +++ b/include/qemu/timer.h
> @@ -652,6 +652,21 @@ static inline void timer_free(QEMUTimer *ts)
> */
> void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
>
> +/**
> + * timer_restore_ns:
> + * @ts: the timer
> + * @expire_time: the expiry time in nanoseconds
> + *
> + * Re-arm a timer from saved state to expire at @expire_time. Unlike
> + * timer_mod_ns(), a timer that has already expired stays in the timer
> + * list in record/replay mode, so that it fires at its recorded
> + * checkpoint.
> + *
> + * This function is thread-safe but the timer and its timer list must not be
> + * freed while this function is running.
> + */
> +void timer_restore_ns(QEMUTimer *ts, int64_t expire_time);
> +
> /**
> * timer_mod_anticipate_ns:
> * @ts: the timer
> diff --git a/migration/savevm.c b/migration/savevm.c
> index fe94ba96e4..13a4898b28 100644
> --- a/migration/savevm.c
> +++ b/migration/savevm.c
> @@ -199,7 +199,7 @@ void timer_get(QEMUFile *f, QEMUTimer *ts)
>
> expire_time = qemu_get_be64(f);
> if (expire_time != -1) {
> - timer_mod_ns(ts, expire_time);
> + timer_restore_ns(ts, expire_time);
> } else {
> timer_del(ts);
> }
> diff --git a/util/qemu-timer.c b/util/qemu-timer.c
> index d93a020064..e4e32caff9 100644
> --- a/util/qemu-timer.c
> +++ b/util/qemu-timer.c
> @@ -396,7 +396,8 @@ static void timer_fire(CPUState *cpu, run_on_cpu_data data)
> }
>
> static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
> - QEMUTimer *ts, int64_t expire_time)
> + QEMUTimer *ts, int64_t expire_time,
> + bool restore)
> {
> QEMUTimer **pt, *t;
>
> @@ -413,7 +414,7 @@ static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
> *
> * Convert such timers to CPU work in order to avoid it.
> */
> - if (replay_mode != REPLAY_MODE_NONE &&
> + if (!restore && replay_mode != REPLAY_MODE_NONE &&
> timer_list->clock->type == QEMU_CLOCK_VIRTUAL &&
> !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL) &&
> expire_time <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) {
> @@ -464,7 +465,22 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
>
> qemu_mutex_lock(&timer_list->active_timers_lock);
> timer_del_locked(timer_list, ts);
> - rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
> + rearm = timer_mod_ns_locked(timer_list, ts, expire_time, false);
> + qemu_mutex_unlock(&timer_list->active_timers_lock);
> +
> + if (rearm) {
> + timerlist_rearm(timer_list);
> + }
> +}
> +
> +void timer_restore_ns(QEMUTimer *ts, int64_t expire_time)
> +{
> + QEMUTimerList *timer_list = ts->timer_list;
> + bool rearm;
> +
> + qemu_mutex_lock(&timer_list->active_timers_lock);
> + timer_del_locked(timer_list, ts);
> + rearm = timer_mod_ns_locked(timer_list, ts, expire_time, true);
> qemu_mutex_unlock(&timer_list->active_timers_lock);
>
> if (rearm) {
> @@ -485,7 +501,7 @@ void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
> if (ts->expire_time != -1) {
> timer_del_locked(timer_list, ts);
> }
> - rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
> + rearm = timer_mod_ns_locked(timer_list, ts, expire_time, false);
> } else {
> rearm = false;
> }
>
> base-commit: 3c061218fcf37a98c944ecb88cff56f94f5c0a43
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 16:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 13:20 [PATCH] util/qemu-timer: restore expired timers into the list on snapshot load Arkadiusz Grzelka via qemu development
2026-12-02 16:35 ` Alex Bennée
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.