From: "Alex Bennée" <alex.bennee@linaro.org>
To: Arkadiusz Grzelka via qemu development <qemu-devel@nongnu.org>
Cc: Arkadiusz Grzelka <arkadiusz.grzelka@arivemb.com>,
pbonzini@redhat.com, peterx@redhat.com, farosas@suse.de,
iii@linux.ibm.com, qemu-stable@nongnu.org
Subject: Re: [PATCH] util/qemu-timer: restore expired timers into the list on snapshot load
Date: Wed, 02 Dec 2026 16:35:18 +0000 [thread overview]
Message-ID: <87o6b31vzt.fsf@draig.linaro.org> (raw)
In-Reply-To: <20261202132036.7-1-arkadiusz.grzelka@arivemb.com> (Arkadiusz Grzelka via qemu development's message of "Wed, 2 Dec 2026 13:20:36 +0000")
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
prev parent reply other threads:[~2026-12-02 16:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87o6b31vzt.fsf@draig.linaro.org \
--to=alex.bennee@linaro.org \
--cc=arkadiusz.grzelka@arivemb.com \
--cc=farosas@suse.de \
--cc=iii@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.