Commit d0ec72885a for qemu.org

commit d0ec72885ac5f115dc9d0fa97f6d7ae4cf4712bd
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Mon Nov 16 11:46:41 2026 +0000

    hw/watchdog/wdt_imx2: Fix migration when pretimeout-support not enabled

    The wdt_imx2 device has optional support for pretimeouts, which are
    present on most i.MX SoCs but not on i.MX31.  If the QOM property
    'pretimeout-support' is not set to true then we don't create the
    ptimer associated with that feature.  However, the migration vmstate
    unconditionally lists that ptimer, with the result that trying to
    save the vmstate of an i.MX31-based machine like "kzm" will hit an
    assertion.

    Move the migration of 'itimer' into its own subsection which we only
    send when pretimeout support is enabled.

    This bug has existed in the code for a long time, but it was hidden
    until commit d9abd0253be ("hw/watchdog/wdt_imx2: Remove redundant
    attributes)" because a bug in the VMSTATE_PTIMER() macro together
    with the fact that the vmstate_imx2_wdt version_id was 0 meant that
    we weren't trying to migrate the device's ptimers at all, and so
    we avoided the assertion (but didn't migrate correctly).

    This is a migration compatibility break for this device, but it isn't
    used in any versioned machine types, and we've already decided to
    break compat on it this release cycle.

    Fixes: daca13d495a11 ("hw/watchdog: Implement full i.MX watchdog support")
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4876
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Tested-by: Karl Mehltretter <kmehltretter@gmail.com>
    Message-id: 20261116114641.1799526-1-peter.maydell@linaro.org
    Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

diff --git a/hw/watchdog/wdt_imx2.c b/hw/watchdog/wdt_imx2.c
index abb8edfae2..b4196de702 100644
--- a/hw/watchdog/wdt_imx2.c
+++ b/hw/watchdog/wdt_imx2.c
@@ -221,13 +221,31 @@ static const MemoryRegionOps imx2_wdt_ops = {
     },
 };

-static const VMStateDescription vmstate_imx2_wdt = {
-    .name = "imx2.wdt",
+static bool pretimeout_state_needed(void *opaque)
+{
+    /* The itimer is only present if pretimeout-support is enabled */
+    IMX2WdtState *s = opaque;
+
+    return s->pretimeout_support;
+}
+
+static const VMStateDescription vmstate_imx2_wdt_pretimeout = {
+    .name = "imx2.wdt/pretimeout",
     .version_id = 1,
     .minimum_version_id = 1,
+    .needed = pretimeout_state_needed,
     .fields = (const VMStateField[]) {
-        VMSTATE_PTIMER(timer, IMX2WdtState),
         VMSTATE_PTIMER(itimer, IMX2WdtState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_imx2_wdt = {
+    .name = "imx2.wdt",
+    .version_id = 2,
+    .minimum_version_id = 2,
+    .fields = (const VMStateField[]) {
+        VMSTATE_PTIMER(timer, IMX2WdtState),
         VMSTATE_BOOL(wicr_locked, IMX2WdtState),
         VMSTATE_BOOL(wcr_locked, IMX2WdtState),
         VMSTATE_UINT16(wcr, IMX2WdtState),
@@ -236,6 +254,10 @@ static const VMStateDescription vmstate_imx2_wdt = {
         VMSTATE_UINT16(wmcr, IMX2WdtState),
         VMSTATE_UINT16(wicr, IMX2WdtState),
         VMSTATE_END_OF_LIST()
+    },
+    .subsections = (const VMStateDescription * const []) {
+        &vmstate_imx2_wdt_pretimeout,
+        NULL
     }
 };