From nobody Sun Apr 6 03:30:39 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=drigo.nl ARC-Seal: i=1; a=rsa-sha256; t=1736889784; cv=none; d=zohomail.com; s=zohoarc; b=VtDkXVdO/vPqYR97GCqpu0VqrILNoaNFIDiHWSdEo3eUrggZkGwAYuKpxFYp+WfLlvfSFXfdJB8/ZFz9YaFU54aQRptY11RuYNVw5CHApLPAK/GT5WRAa9XdE95+YELjKOXimqc6lU/LVFJtB/49KI44un8LXihR9i4eIEHuw2I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1736889784; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=G2rbHS6u3Za8/AC5FJ/CdxlwUMqNT7soTE46QA9lHHw=; b=UFvz7zEDeUFCoICZyZt1zfb3pFdxkcYdrp7pYqsGZDrMYgyJ7kVep+DDQ3k4PWG+kmspcOxDJcRmhVkK2UaT9iZH5CqLfDmdQIHCQw46J+pn4S4xFXrrXL2NDrJc4K2eidF582dQK6fOowX99ddAMiS1gYCn8LWMgt3+dScsysE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1736889784201743.2639224663549; Tue, 14 Jan 2025 13:23:04 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tXoMd-00059d-Fn; Tue, 14 Jan 2025 16:22:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tXoMb-00059M-Qx for qemu-devel@nongnu.org; Tue, 14 Jan 2025 16:22:17 -0500 Received: from smtp-190b.mail.infomaniak.ch ([185.125.25.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tXoMY-0008Ca-Te for qemu-devel@nongnu.org; Tue, 14 Jan 2025 16:22:17 -0500 Received: from smtp-4-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10:40ca:feff:fe05:0]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4YXhsX06Klz6v0; Tue, 14 Jan 2025 22:22:08 +0100 (CET) Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4YXhsW2Z14zgJR; Tue, 14 Jan 2025 22:22:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=drigo.nl; s=20241108; t=1736889727; bh=G2rbHS6u3Za8/AC5FJ/CdxlwUMqNT7soTE46QA9lHHw=; h=From:To:Cc:Subject:Date:From; b=VPsLgVDJmfmY3YHy94ysAD9X2EEpAZc/484AI69r95KdzGIDDRlr+qvjZS1s6KudA b/ilGuzxv8KlC+rESDZad/ok4qiKmGFUUd2K0Om+HELueJNfWhDPgWIitl0vmfbNpr 1bCkqm88KAlgCuq4mApiJBtbobzQsIXtNChACk89fwSPq11YxjRSSiWs3kt7cX8PWI 7Bo7q9KGh78yugy1K+CgmQNaNjbAup7eaAbiZLZDdZWvOsw1NZlRXM2gIdxJ4al+Jy 1yxUxQLfy4jdHMfwZWNM6cjPiY+PoAYgsnFwyDRtdyhlvOuyAPXdU9MTate6rJNkZg vUvPGjGXNo30A== From: Rodrigo Dias Correa To: anup.patel@wdc.com, Alistair.Francis@wdc.com Cc: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH] goldfish_rtc: Fix tick_offset migration Date: Tue, 14 Jan 2025 22:21:50 +0100 Message-Id: <20250114212150.228241-1-r@drigo.nl> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Infomaniak-Routing: alpha Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.125.25.11; envelope-from=r@drigo.nl; helo=smtp-190b.mail.infomaniak.ch X-Spam_score_int: -45 X-Spam_score: -4.6 X-Spam_bar: ---- X-Spam_report: (-4.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-1.794, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @drigo.nl) X-ZM-MESSAGEID: 1736889784931019000 Content-Type: text/plain; charset="utf-8" Instead of migrating the raw tick_offset, goldfish_rtc migrates a recalculated value based on QEMU_CLOCK_VIRTUAL. As QEMU_CLOCK_VIRTUAL stands still across a save-and-restore cycle, the guest RTC becomes out of sync with the host RTC when the VM is restored. As described in the bug description, it looks like this calculation was copied from pl031 RTC, which had its tick_offset migration fixed by Commit 032cfe6a79c8 ("pl031: Correctly migrate state when using -rtc clock=3Dhost"). Migrate the tick_offset directly, adding it as a version-dependent field to VMState. Keep the old behavior when migrating from previous versions. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2033 Signed-off-by: Rodrigo Dias Correa Reviewed-by: Alistair Francis --- As a new field was added to VMState, after this patch, it won't be possibl= e to migrate to old versions. I'm not sure if this is needed. Please, let me kn= ow. --- hw/rtc/goldfish_rtc.c | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/hw/rtc/goldfish_rtc.c b/hw/rtc/goldfish_rtc.c index fa1d9051f4..0f1b53e0e4 100644 --- a/hw/rtc/goldfish_rtc.c +++ b/hw/rtc/goldfish_rtc.c @@ -178,38 +178,21 @@ static void goldfish_rtc_write(void *opaque, hwaddr o= ffset, trace_goldfish_rtc_write(offset, value); } =20 -static int goldfish_rtc_pre_save(void *opaque) -{ - uint64_t delta; - GoldfishRTCState *s =3D opaque; - - /* - * We want to migrate this offset, which sounds straightforward. - * Unfortunately, we cannot directly pass tick_offset because - * rtc_clock on destination Host might not be same source Host. - * - * To tackle, this we pass tick_offset relative to vm_clock from - * source Host and make it relative to rtc_clock at destination Host. - */ - delta =3D qemu_clock_get_ns(rtc_clock) - - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - s->tick_offset_vmstate =3D s->tick_offset + delta; - - return 0; -} - static int goldfish_rtc_post_load(void *opaque, int version_id) { - uint64_t delta; GoldfishRTCState *s =3D opaque; =20 - /* - * We extract tick_offset from tick_offset_vmstate by doing - * reverse math compared to pre_save() function. - */ - delta =3D qemu_clock_get_ns(rtc_clock) - - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - s->tick_offset =3D s->tick_offset_vmstate - delta; + if (version_id < 3) { + /* + * Previous versions didn't migrate tick_offset directly. Instead,= they + * migrated tick_offset_vmstate, which is a recalculation based on + * QEMU_CLOCK_VIRTUAL. We use tick_offset_vmstate when migrating f= rom + * older versions. + */ + uint64_t delta =3D qemu_clock_get_ns(rtc_clock) - + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + s->tick_offset =3D s->tick_offset_vmstate - delta; + } =20 goldfish_rtc_set_alarm(s); =20 @@ -239,8 +222,7 @@ static const MemoryRegionOps goldfish_rtc_ops[2] =3D { =20 static const VMStateDescription goldfish_rtc_vmstate =3D { .name =3D TYPE_GOLDFISH_RTC, - .version_id =3D 2, - .pre_save =3D goldfish_rtc_pre_save, + .version_id =3D 3, .post_load =3D goldfish_rtc_post_load, .fields =3D (const VMStateField[]) { VMSTATE_UINT64(tick_offset_vmstate, GoldfishRTCState), @@ -249,6 +231,7 @@ static const VMStateDescription goldfish_rtc_vmstate = =3D { VMSTATE_UINT32(irq_pending, GoldfishRTCState), VMSTATE_UINT32(irq_enabled, GoldfishRTCState), VMSTATE_UINT32(time_high, GoldfishRTCState), + VMSTATE_UINT64_V(tick_offset, GoldfishRTCState, 3), VMSTATE_END_OF_LIST() } }; --=20 2.34.1