From nobody Tue Nov 18 07:42:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1609395180; cv=none; d=zohomail.com; s=zohoarc; b=XjFOJtF7r01/yehu8ROjvbtF0RbMc1tinvamCdbhHb22CJy+P8tndNb279E29IE5DwPMgiKnCk1u+U15Dt1qwEb2TIMnu7p/m+MtFtRpWsnwYKiDT5EDaXhmBlAE8S/c32KdwugPp0wDW4kRzOl5pJZP07qJhGnx1NCyJt4HPgQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609395180; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dR7vno4cs3okJeTlKu89jE9DSf8giCcY+XInpIXXUec=; b=iwHRwhnyM/c6KrEiv5OVVXB0HsH3eBfP0hPTLulz4EzXr1kJ4J04uOR7vPHPhracrRtm31I7nurqDM4KiZD08+5DX3/3gdOAeOI/Z44DZlEREJw9J+CPzPi42mY/KOTnZ2Ll/G/M7gsD3QH1kMe/2k5kFz/ENM7N/hnZdeorOlw= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1609395180587902.950798041613; Wed, 30 Dec 2020 22:13:00 -0800 (PST) Received: from localhost ([::1]:54088 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kurCk-0004lV-Ox for importer@patchew.org; Thu, 31 Dec 2020 01:12:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33954) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurAx-0003Vr-3j; Thu, 31 Dec 2020 01:11:07 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2838) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurAr-0005cV-J8; Thu, 31 Dec 2020 01:11:06 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4D5yRX5wL0zkycB; Thu, 31 Dec 2020 14:09:44 +0800 (CST) Received: from DESKTOP-EDHIELA.china.huawei.com (10.174.187.50) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.498.0; Thu, 31 Dec 2020 14:10:40 +0800 From: Jinhao Gao To: , Subject: [PATCH v3 1/3] spapr_pci: Fix memory leak of vmstate_spapr_pci Date: Thu, 31 Dec 2020 14:10:18 +0800 Message-ID: <20201231061020.828-2-gaojinhao@huawei.com> X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: <20201231061020.828-1-gaojinhao@huawei.com> References: <20201231061020.828-1-gaojinhao@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.187.50] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=gaojinhao@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Berger , Jason Wang , "Michael S . Tsirkin" , Greg Kurz , "Dr . David Alan Gilbert" , Juan Quintela , wanghaibin.wang@huawei.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , zhukeqian1@huawei.com, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When VM migrate VMState of spapr_pci, the field(msi_devs) of spapr_pci having a flag of VMS_ALLOC need to allocate memory. If the src doesn't free memory of msi_devs in SaveStateEntry of spapr_pci after QEMUFile save VMState of spapr_pci, it may result in memory leak of msi_devs. We add the post_save func to free memory, which prevents memory leak. Reported-by: Euler Robot Signed-off-by: Jinhao Gao Acked-by: David Gibson Reviewed-by: Michael S. Tsirkin --- hw/ppc/spapr_pci.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 76d7c91e9c..1b2b940606 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -2173,6 +2173,16 @@ static int spapr_pci_pre_save(void *opaque) return 0; } =20 +static int spapr_pci_post_save(void *opaque) +{ + SpaprPhbState *sphb =3D opaque; + + g_free(sphb->msi_devs); + sphb->msi_devs =3D NULL; + sphb->msi_devs_num =3D 0; + return 0; +} + static int spapr_pci_post_load(void *opaque, int version_id) { SpaprPhbState *sphb =3D opaque; @@ -2205,6 +2215,7 @@ static const VMStateDescription vmstate_spapr_pci =3D= { .version_id =3D 2, .minimum_version_id =3D 2, .pre_save =3D spapr_pci_pre_save, + .post_save =3D spapr_pci_post_save, .post_load =3D spapr_pci_post_load, .fields =3D (VMStateField[]) { VMSTATE_UINT64_EQUAL(buid, SpaprPhbState, NULL), --=20 2.23.0 From nobody Tue Nov 18 07:42:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1609395197; cv=none; d=zohomail.com; s=zohoarc; b=cVd615NZ2M4EfwtvvSP8Xt+WESLoUqA6qOUsf8QsQiYbMikfsHLfqV2XpvlNOEZIYxYJkHSy3aOtaKP1x12pjgo1yfu7nKT6eJ+0lDhZHqkG/AbJIwraSblVE4fUBL+cd221OInVgglHLZQv5onfeSs4r2nYfwVbpgxkWBgUwqI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609395197; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=qEIwm3MxGClCIvCitCUwbpCuMgPIPomcsxkikIXsiMg=; b=GSXDAmBBJtNbK8bb9TAPgbWD2V+65YCrqj7lVmEgIpfVMiARTaJGS9KbqCK3whcyaW9ALBJrS4ceJTiKnSoq0s0KuwCz2ZlfKL2UYGyrCH1B01iVNJ5W89+kccorXrUEJZ1bCNHqVYYn1vAoxgx1PmxYWvREb5CPhxpYkQk5y3g= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1609395197879893.8665419443284; Wed, 30 Dec 2020 22:13:17 -0800 (PST) Received: from localhost ([::1]:54730 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kurD2-00051I-R9 for importer@patchew.org; Thu, 31 Dec 2020 01:13:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33988) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurB4-0003ZJ-37; Thu, 31 Dec 2020 01:11:14 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2839) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurAs-0005cW-Gn; Thu, 31 Dec 2020 01:11:13 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4D5yRX6Brfzkybk; Thu, 31 Dec 2020 14:09:44 +0800 (CST) Received: from DESKTOP-EDHIELA.china.huawei.com (10.174.187.50) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.498.0; Thu, 31 Dec 2020 14:10:41 +0800 From: Jinhao Gao To: , Subject: [PATCH v3 2/3] savevm: Fix memory leak of vmstate_configuration Date: Thu, 31 Dec 2020 14:10:19 +0800 Message-ID: <20201231061020.828-3-gaojinhao@huawei.com> X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: <20201231061020.828-1-gaojinhao@huawei.com> References: <20201231061020.828-1-gaojinhao@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.187.50] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=gaojinhao@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Berger , Jason Wang , "Michael S . Tsirkin" , Greg Kurz , "Dr . David Alan Gilbert" , Juan Quintela , wanghaibin.wang@huawei.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , zhukeqian1@huawei.com, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When VM migrate VMState of configuration, the fields(name and capabilities) of configuration having a flag of VMS_ALLOC need to allocate memory. If the src doesn't free memory of capabilities in SaveState after save VMState of configuration, or the dst doesn't free memory of name and capabilities in p= ost load of configuration, it may result in memory leak of name and capabilitie= s. We free memory in configuration_post_save and configuration_post_load func, which prevents memory leak. Reported-by: Euler Robot Signed-off-by: Jinhao Gao Reviewed-by: Michael S. Tsirkin --- migration/savevm.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/migration/savevm.c b/migration/savevm.c index 5f937a2762..13f1a5dab7 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -314,6 +314,16 @@ static int configuration_pre_save(void *opaque) return 0; } =20 +static int configuration_post_save(void *opaque) +{ + SaveState *state =3D opaque; + + g_free(state->capabilities); + state->capabilities =3D NULL; + state->caps_count =3D 0; + return 0; +} + static int configuration_pre_load(void *opaque) { SaveState *state =3D opaque; @@ -364,24 +374,36 @@ static int configuration_post_load(void *opaque, int = version_id) { SaveState *state =3D opaque; const char *current_name =3D MACHINE_GET_CLASS(current_machine)->name; + int ret =3D 0; =20 if (strncmp(state->name, current_name, state->len) !=3D 0) { error_report("Machine type received is '%.*s' and local is '%s'", (int) state->len, state->name, current_name); - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (state->target_page_bits !=3D qemu_target_page_bits()) { error_report("Received TARGET_PAGE_BITS is %d but local is %d", state->target_page_bits, qemu_target_page_bits()); - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!configuration_validate_capabilities(state)) { - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 - return 0; +out: + g_free((void *)state->name); + state->name =3D NULL; + state->len =3D 0; + g_free(state->capabilities); + state->capabilities =3D NULL; + state->caps_count =3D 0; + + return ret; } =20 static int get_capability(QEMUFile *f, void *pv, size_t size, @@ -515,6 +537,7 @@ static const VMStateDescription vmstate_configuration = =3D { .pre_load =3D configuration_pre_load, .post_load =3D configuration_post_load, .pre_save =3D configuration_pre_save, + .post_save =3D configuration_post_save, .fields =3D (VMStateField[]) { VMSTATE_UINT32(len, SaveState), VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len), --=20 2.23.0 From nobody Tue Nov 18 07:42:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1609395180; cv=none; d=zohomail.com; s=zohoarc; b=HK45hn9UVI+0aKAV/4b0VgGOdvW+dTh1g3s0PalSCAkgQ3jWooXAS9T2ybgHd7IagKeYwPifmOLzX3fwphFWYZKAsRQunyFyKKoVvGcpurIFp1DvbUH2LsO4UH4RxlXL3tTijZeeR3EvePq+b6FxluuSft1gnIEFBdvUsmhVCgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609395180; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0nXpwFTr4qgFJMqV0T5oDyjzLZ8e+WY6ZbnAtZbEcuc=; b=DnUKDNzN8dQBzCxhj9ychZfipx3vOA0BEllVJzg/ncpe/T0Nzh1eyTNvJ/MvAr97uNuHlNklvpuJ0qC0jL6aUrdypsn7UvZmTIMp/DF1a1C322olvoAGftSkHie8lAta8/KzRJ3v1MxqEavBmTnQVHz8oyehwuMk17pVaDq9Neg= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1609395180650618.5194492719648; Wed, 30 Dec 2020 22:13:00 -0800 (PST) Received: from localhost ([::1]:54072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kurCk-0004lC-QZ for importer@patchew.org; Thu, 31 Dec 2020 01:12:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33912) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurAv-0003Vi-EE; Thu, 31 Dec 2020 01:11:05 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2840) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kurAr-0005fc-Ck; Thu, 31 Dec 2020 01:11:04 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4D5yRd6QgNzkycJ; Thu, 31 Dec 2020 14:09:49 +0800 (CST) Received: from DESKTOP-EDHIELA.china.huawei.com (10.174.187.50) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.498.0; Thu, 31 Dec 2020 14:10:42 +0800 From: Jinhao Gao To: , Subject: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc() Date: Thu, 31 Dec 2020 14:10:20 +0800 Message-ID: <20201231061020.828-4-gaojinhao@huawei.com> X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: <20201231061020.828-1-gaojinhao@huawei.com> References: <20201231061020.828-1-gaojinhao@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.187.50] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=gaojinhao@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Berger , Jason Wang , "Michael S . Tsirkin" , Greg Kurz , "Dr . David Alan Gilbert" , Juan Quintela , wanghaibin.wang@huawei.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , zhukeqian1@huawei.com, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Some memory allocated for fields having a flag of VMS_ALLOC in SaveState may not free before VM load vmsd in migration. So we pre-free memory before allocation in vmstate_handle_alloc() to avoid memleaks. Reported-by: Euler Robot Signed-off-by: Jinhao Gao Signed-off-by: Michael S. Tsirkin Reviewed-by: Dr. David Alan Gilbert --- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/migration/vmstate.c b/migration/vmstate.c index e9d2aef66b..873f76739f 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMState= Field *field, gsize size =3D vmstate_size(opaque, field); size *=3D vmstate_n_elems(opaque, field); if (size) { + g_free(*(void **)ptr); *(void **)ptr =3D g_malloc(size); } } --=20 2.23.0