From nobody Mon Apr 13 13:32:49 2026 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=quarantine dis=none) header.from=mihalicyn.com ARC-Seal: i=1; a=rsa-sha256; t=1772809499; cv=none; d=zohomail.com; s=zohoarc; b=D2OliY/ozhLSyYwHaeHVApbzN2uPpZN/B+obZffY/lM+Gvl7AshaXEmYTltuSYukagwj7xz+YO3AEPSYZllMBKpIUmCi776AMa4gknafYwwECnSizFM2OrSiBLwB188Av/Y4P85rJ1juoF3y8ryboFCPZwi5OL0mU4Wy3jLt/2s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1772809499; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Aa7YS8YRAQ8yRhQF9Whg1Qcoxr3p3yRf12L+u0hXtWc=; b=A/fGGwdpurwbVK8MgJwprkeNHxQgo5UwEw4lP/wMHUXlxw0Kq4/zCGe7Uwli7vkYX/vvj1eDZo2UiPsaUIY8sU3eiPDkBEWVsH6A62M1n6JMKfRjWf4rD4+Gt1K0SKWcCjzPSQOTtNGivsplIb48Cxc89PV4tAvuuCz7G21v+zI= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1772809499713201.09542526760072; Fri, 6 Mar 2026 07:04:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vyWiz-0008ER-H5; Fri, 06 Mar 2026 10:04:22 -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 1vyWif-0007v5-AC for qemu-devel@nongnu.org; Fri, 06 Mar 2026 10:04:03 -0500 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vyWid-0003Le-Hd for qemu-devel@nongnu.org; Fri, 06 Mar 2026 10:04:01 -0500 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-439b8a3f2bcso4479694f8f.3 for ; Fri, 06 Mar 2026 07:03:58 -0800 (PST) Received: from alex-laptop.lan (p200300cf57228c00583f1937c9b9cecc.dip0.t-ipconnect.de. [2003:cf:5722:8c00:583f:1937:c9b9:cecc]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439dae36785sm4703623f8f.27.2026.03.06.07.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2026 07:03:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mihalicyn.com; s=mihalicyn; t=1772809437; x=1773414237; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Aa7YS8YRAQ8yRhQF9Whg1Qcoxr3p3yRf12L+u0hXtWc=; b=MU9H0/3cesldAQoj2jQodTuSmj7vCQcTq4wDe+RPIHvJ/R2nw0V1HHR3cL4WLq4nYy mxK9uIxwNlqXM+Flxx06hciNoLuX32LccBh1nNKFfmWEySG6M9ZNeLhsqztzMGSY2NFp MfBtm4nJOSygOTkg64gSj2GjM9uWLJg1vfmDQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772809437; x=1773414237; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Aa7YS8YRAQ8yRhQF9Whg1Qcoxr3p3yRf12L+u0hXtWc=; b=Ai7lsfd8f+NZUJbsJDY8eyw086npbhZhyCY8lxTszA08uwqDYAEdF9tG0AWFCXkxkx Aggt+ATFrNWPp6DIZtPxJOLBMG5AYbJGTNQOpvvTbKQCST+TCiv9sr2A2QS3CmwlFUDH DfUSOxu5Clzh5RC4kPN4Nai5a0GrXeoGMMMyv+b4e3lTK1ceYRKTcoIb0LY8IhK1R2+j Sm+lPnaGIiqeCEx9VkUH4uk7uYjkIwB8bRuR9wtHq9Cq7YC8+P5aMKj8kEXhQ+ix9Wrp zs/C2yNyiNWMjucuYe4YtW5Z7fYMsuuF/0HJNHHW2ud06M95V97rtot5YDjbIc/Tx/M1 eFng== X-Gm-Message-State: AOJu0YyRIu1c63u8LykGrrTArHPFMA98hidZawAJqBZv8cELzFbNH1e4 5o35FNhXbdR7mWEnDQen3NezZ78437FsKNUSYgfkB3FvPy4c+o1bAotNxyJh59Pu4MGUzG0Do3K vLzYXgS0= X-Gm-Gg: ATEYQzwjAPnyRnpOI8K6WBFtpZolSYxNvV5qlWM+retlD9uK9Ven8/iVRn+VmnrJWd0 s90g7UHMI4gSW0j6xactjtw4A4IlF3jEI6UAZGngq/geXfOBERJs5xRDIxfkZJj0G1toQteve14 dOMlO0ZTCCNj2GmmJ0kRizgs5Bhk9GatgDnCNjauwTwqWHP12y8K7Q0SgfzMOLUmJKIfLTqA3p+ nuhzbBLfmTANTRCx6Xs/5++oXma9dhBJEe9Z/Tjda80gJj4w874czPdZx+qg7ZdHfVeCW5p0W/U nw5t8O5sogOlYCI8yy8gLdg2YAyaqXcfrAWZ502mu6w9k/i+vFHqO1B1/HmBgjgZS6h7cJ/y9nM AaTv7oKHkfX3xjs5w7HS9XY/dgXZ8cpBbbpgpXILvhp1Z9/1zNrUSMRPzjxBxh9FmXNZBDkotFM RkxI6YGf0JVQ1zp7toP+eo/rkbx4JW13aauWRm7Vpd3D0ae1rcDaPVeV7Tcc5uhC44wS7yrTWxN JSS0ziNVymTMTePo8MFU4E= X-Received: by 2002:a05:6000:1acd:b0:439:c1ca:82be with SMTP id ffacd0b85a97d-439da885f92mr3986014f8f.28.1772809425164; Fri, 06 Mar 2026 07:03:45 -0800 (PST) From: Alexander Mikhalitsyn To: qemu-devel@nongnu.org Cc: Zhao Liu , Jesper Devantier , Alexander Mikhalitsyn , Klaus Jensen , =?UTF-8?q?St=C3=A9phane=20Graber?= , Paolo Bonzini , qemu-block@nongnu.org, Keith Busch , Peter Xu , Fabiano Rosas , Alexander Mikhalitsyn Subject: [PATCH v3 1/6] migration: add VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT{8, 32}_ALLOC Date: Fri, 6 Mar 2026 16:03:37 +0100 Message-ID: <20260306150342.395923-2-alexander@mihalicyn.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260306150342.395923-1-alexander@mihalicyn.com> References: <20260306150342.395923-1-alexander@mihalicyn.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::436; envelope-from=alexander@mihalicyn.com; helo=mail-wr1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, SPF_HELO_NONE=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: qemu development 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 @mihalicyn.com) X-ZM-MESSAGEID: 1772809505487154100 Content-Type: text/plain; charset="utf-8" From: Alexander Mikhalitsyn Add VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT{8, 32}_ALLOC, which helps to save/restore a dynamic array of pointers to structures. Signed-off-by: Alexander Mikhalitsyn v2: - added VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT8_ALLOC --- include/migration/vmstate.h | 33 ++++++++++++++ migration/vmstate-types.c | 88 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 89f9f49d20a..0bbe3317740 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -265,6 +265,7 @@ extern const VMStateInfo vmstate_info_bitmap; extern const VMStateInfo vmstate_info_qtailq; extern const VMStateInfo vmstate_info_gtree; extern const VMStateInfo vmstate_info_qlist; +extern const VMStateInfo vmstate_info_ptrs_array_entry; =20 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0) /* @@ -537,6 +538,38 @@ extern const VMStateInfo vmstate_info_qlist; .offset =3D vmstate_offset_array(_s, _f, _type*, _n), \ } =20 +/* + * For migrating a dynamically allocated uint32-indexed array + * of pointers to structures (with NULL entries and with auto memory alloc= ation). + * + * _type: type of structure pointed to + * _vmsd: VMSD for structure + * start: size of structure pointed to (for auto memory allocation) + */ +#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT8_ALLOC(_field, _state, _f= ield_num, _version, _vmsd, _type) { \ + .name =3D (stringify(_field)), \ + .version_id =3D (_version), \ + .num_offset =3D vmstate_offset_value(_state, _field_num, uint8_t), \ + .info =3D &vmstate_info_ptrs_array_entry, \ + .vmsd =3D &(_vmsd), \ + .start =3D sizeof(_type), \ + .size =3D sizeof(_type *), \ + .flags =3D VMS_VARRAY_UINT8|VMS_POINTER, \ + .offset =3D vmstate_offset_pointer(_state, _field, _type *), \ +} + +#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT32_ALLOC(_field, _state, _= field_num, _version, _vmsd, _type) { \ + .name =3D (stringify(_field)), \ + .version_id =3D (_version), \ + .num_offset =3D vmstate_offset_value(_state, _field_num, uint32_t), \ + .info =3D &vmstate_info_ptrs_array_entry, \ + .vmsd =3D &(_vmsd), \ + .start =3D sizeof(_type), \ + .size =3D sizeof(_type *), \ + .flags =3D VMS_VARRAY_UINT32|VMS_POINTER, \ + .offset =3D vmstate_offset_pointer(_state, _field, _type *), \ +} + #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _vers= ion, _info, _type) { \ .name =3D (stringify(_field)), = \ .version_id =3D (_version), = \ diff --git a/migration/vmstate-types.c b/migration/vmstate-types.c index 89cb2114721..3335377cd07 100644 --- a/migration/vmstate-types.c +++ b/migration/vmstate-types.c @@ -942,3 +942,91 @@ const VMStateInfo vmstate_info_qlist =3D { .get =3D get_qlist, .put =3D put_qlist, }; + +static int put_ptrs_array_entry(QEMUFile *f, void *ppv, size_t unused_size, + const VMStateField *field, JSONWriter *vmd= esc) +{ + const VMStateDescription *vmsd =3D field->vmsd; + int ret; + Error *local_err =3D NULL; + void *pv; + + /* + * (ppv) is an address of an i-th element of a dynamic array. + * + * (ppv) can not be NULL unless we have some regression/bug in + * vmstate_save_state_v(), because it is result of pointer arithemic l= ike: + * first_elem + size * i. + */ + if (ppv =3D=3D NULL) { + error_report("vmstate: put_ptrs_array_entry must be called with pp= v !=3D NULL"); + return -EINVAL; + } + + /* get a pointer to a structure */ + pv =3D *(void **)ppv; + + if (pv =3D=3D NULL) { + /* write a mark telling that there was a NULL pointer */ + qemu_put_byte(f, false); + return 0; + } + + /* if pointer is not NULL, dump the structure contents with help of vm= sd */ + qemu_put_byte(f, true); + ret =3D vmstate_save_state(f, vmsd, pv, vmdesc, &local_err); + if (ret) { + error_report_err(local_err); + return ret; + } + + return 0; +} + +static int get_ptrs_array_entry(QEMUFile *f, void *ppv, size_t unused_size, + const VMStateField *field) +{ + int ret =3D 0; + Error *local_err =3D NULL; + const VMStateDescription *vmsd =3D field->vmsd; + /* size of structure pointed to by elements of array */ + size_t size =3D field->start; + + if (ppv =3D=3D NULL) { + error_report("vmstate: get_ptrs_array_entry must be called with pp= v !=3D NULL"); + return -EINVAL; + } + + /* + * We start from a clean array, all elements must be NULL, unless + * something we haven't prepared for has changed in vmstate_save_state= _v(). + * Let's check for this just in case. + */ + if (*(void **)ppv !=3D NULL) { + error_report("vmstate: get_ptrs_array_entry must be called with *p= pv =3D=3D NULL"); + return -EINVAL; + } + + if (qemu_get_byte(f)) { + void *pv; + + /* allocate memory for structure */ + pv =3D g_malloc0(size); + ret =3D vmstate_load_state(f, vmsd, pv, vmsd->version_id, &local_e= rr); + if (ret) { + error_report_err(local_err); + g_free(pv); + return ret; + } + + *(void **)ppv =3D pv; + } + + return ret; +} + +const VMStateInfo vmstate_info_ptrs_array_entry =3D { + .name =3D "ptrs_array_entry", + .get =3D get_ptrs_array_entry, + .put =3D put_ptrs_array_entry, +}; --=20 2.47.3