From nobody Sun Feb 8 22:07:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 148613978292120.322444038937988; Fri, 3 Feb 2017 08:36:22 -0800 (PST) Received: from localhost ([::1]:35688 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgqS-0006BN-Rc for importer@patchew.org; Fri, 03 Feb 2017 11:36:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgO2-0007Qg-Ja for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZgO0-0007S8-QW for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49118) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZgO0-0007Ru-Ii for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:56 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C50CE369C3 for ; Fri, 3 Feb 2017 16:06:56 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-144.ams2.redhat.com [10.36.117.144]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13G6r0n014461; Fri, 3 Feb 2017 11:06:55 -0500 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, mst@redhat.com, quintela@redhat.com Date: Fri, 3 Feb 2017 16:06:49 +0000 Message-Id: <20170203160651.19917-3-dgilbert@redhat.com> In-Reply-To: <20170203160651.19917-1-dgilbert@redhat.com> References: <20170203160651.19917-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 03 Feb 2017 16:06:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/4] migration: Add VMSTATE_WITH_TMP X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" VMSTATE_WITH_TMP is for handling structures where some calculation or rearrangement of the data needs to be performed before the data hits the wire. For example, where the value on the wire is an offset from a non-migrated base, but the data in the structure is the actual pointer. To use it, a temporary type is created and a vmsd used on that type. The first element of the type must be 'parent' a pointer back to the type of the main structure. VMSTATE_WITH_TMP takes care of allocating and freeing the temporary before running the child vmsd. The post_load/pre_save on the child vmsd can copy things from the parent to the temporary using the parent pointer and do any other calculations needed; it can then use normal VMSD entries to do the actual data storage without having to fiddle around with qemu_get_*/qemu_put_* Signed-off-by: Dr. David Alan Gilbert Reviewed-by: David Gibson Reviewed-by: Alex Benn=C3=A9e --- include/migration/vmstate.h | 19 +++++++++++++++++++ migration/vmstate.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index cc66910..f2dfb85 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -259,6 +259,7 @@ extern const VMStateInfo vmstate_info_cpudouble; extern const VMStateInfo vmstate_info_timer; extern const VMStateInfo vmstate_info_buffer; extern const VMStateInfo vmstate_info_unused_buffer; +extern const VMStateInfo vmstate_info_tmp; extern const VMStateInfo vmstate_info_bitmap; extern const VMStateInfo vmstate_info_qtailq; =20 @@ -651,6 +652,24 @@ extern const VMStateInfo vmstate_info_qtailq; .offset =3D offsetof(_state, _field), \ } =20 +/* Allocate a temporary of type 'tmp_type', set tmp->parent to _state + * and execute the vmsd on the temporary. Note that we're working with + * the whole of _state here, not a field within it. + * We compile time check that: + * That _tmp_type contains a 'parent' member that's a pointer to the + * '_state' type + * That the pointer is right at the start of _tmp_type. + */ +#define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) { \ + .name =3D "tmp", \ + .size =3D sizeof(_tmp_type) + \ + QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != =3D 0) + \ + type_check_pointer(_state, \ + typeof_field(_tmp_type, parent)), \ + .vmsd =3D &(_vmsd), \ + .info =3D &vmstate_info_tmp, \ +} + #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) { \ .name =3D "unused", \ .field_exists =3D (_test), \ diff --git a/migration/vmstate.c b/migration/vmstate.c index 2b2b3a5..a3de20f 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -935,6 +935,46 @@ const VMStateInfo vmstate_info_unused_buffer =3D { .put =3D put_unused_buffer, }; =20 +/* vmstate_info_tmp, see VMSTATE_WITH_TMP, the idea is that we allocate + * a temporary buffer and the pre_load/pre_save methods in the child vmsd + * copy stuff from the parent into the child and do calculations to fill + * in fields that don't really exist in the parent but need to be in the + * stream. + */ +static int get_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field) +{ + int ret; + const VMStateDescription *vmsd =3D field->vmsd; + int version_id =3D field->version_id; + void *tmp =3D g_malloc(size); + + /* Writes the parent field which is at the start of the tmp */ + *(void **)tmp =3D pv; + ret =3D vmstate_load_state(f, vmsd, tmp, version_id); + g_free(tmp); + return ret; +} + +static int put_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field, + QJSON *vmdesc) +{ + const VMStateDescription *vmsd =3D field->vmsd; + void *tmp =3D g_malloc(size); + + /* Writes the parent field which is at the start of the tmp */ + *(void **)tmp =3D pv; + vmstate_save_state(f, vmsd, tmp, vmdesc); + g_free(tmp); + + return 0; +} + +const VMStateInfo vmstate_info_tmp =3D { + .name =3D "tmp", + .get =3D get_tmp, + .put =3D put_tmp, +}; + /* bitmaps (as defined by bitmap.h). Note that size here is the size * of the bitmap in bits. The on-the-wire format of a bitmap is 64 * bit words with the bits in big endian order. The in-memory format --=20 2.9.3