All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org, andrey.gruzdev@virtuozzo.com,
	berrange@redhat.com, gaojinhao@huawei.com, armbru@redhat.com,
	mst@redhat.com, philmd@redhat.com, wainersm@redhat.com
Subject: [PULL 02/27] savevm: Fix memory leak of vmstate_configuration
Date: Thu,  4 Feb 2021 16:39:34 +0000	[thread overview]
Message-ID: <20210204163959.377618-3-dgilbert@redhat.com> (raw)
In-Reply-To: <20210204163959.377618-1-dgilbert@redhat.com>

From: Jinhao Gao <gaojinhao@huawei.com>

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 post
load of configuration, it may result in memory leak of name and capabilities.
We free memory in configuration_post_save and configuration_post_load func,
which prevents memory leak.

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Jinhao Gao <gaojinhao@huawei.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Message-Id: <20201231061020.828-3-gaojinhao@huawei.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
---
 migration/savevm.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/migration/savevm.c b/migration/savevm.c
index 4f3b69ecfc..d1e6aaed60 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -315,6 +315,16 @@ static int configuration_pre_save(void *opaque)
     return 0;
 }
 
+static int configuration_post_save(void *opaque)
+{
+    SaveState *state = opaque;
+
+    g_free(state->capabilities);
+    state->capabilities = NULL;
+    state->caps_count = 0;
+    return 0;
+}
+
 static int configuration_pre_load(void *opaque)
 {
     SaveState *state = opaque;
@@ -365,24 +375,36 @@ static int configuration_post_load(void *opaque, int version_id)
 {
     SaveState *state = opaque;
     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
+    int ret = 0;
 
     if (strncmp(state->name, current_name, state->len) != 0) {
         error_report("Machine type received is '%.*s' and local is '%s'",
                      (int) state->len, state->name, current_name);
-        return -EINVAL;
+        ret = -EINVAL;
+        goto out;
     }
 
     if (state->target_page_bits != 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 = -EINVAL;
+        goto out;
     }
 
     if (!configuration_validate_capabilities(state)) {
-        return -EINVAL;
+        ret = -EINVAL;
+        goto out;
     }
 
-    return 0;
+out:
+    g_free((void *)state->name);
+    state->name = NULL;
+    state->len = 0;
+    g_free(state->capabilities);
+    state->capabilities = NULL;
+    state->caps_count = 0;
+
+    return ret;
 }
 
 static int get_capability(QEMUFile *f, void *pv, size_t size,
@@ -516,6 +538,7 @@ static const VMStateDescription vmstate_configuration = {
     .pre_load = configuration_pre_load,
     .post_load = configuration_post_load,
     .pre_save = configuration_pre_save,
+    .post_save = configuration_post_save,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(len, SaveState),
         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
-- 
2.29.2



  parent reply	other threads:[~2021-02-04 17:20 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-04 16:39 [PULL 00/27] migration queue Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 01/27] spapr_pci: Fix memory leak of vmstate_spapr_pci Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` Dr. David Alan Gilbert (git) [this message]
2021-02-04 16:39 ` [PULL 03/27] vmstate: Fix memory leak in vmstate_handle_alloc() Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 04/27] migration/qemu-file: Fix maybe uninitialized on qemu_get_buffer_in_place() Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 05/27] migration: introduce 'background-snapshot' migration capability Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 06/27] migration: introduce UFFD-WP low-level interface helpers Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 07/27] migration: support UFFD write fault processing in ram_save_iterate() Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 08/27] migration: implementation of background snapshot thread Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 09/27] migration: introduce 'userfaultfd-wrlat.py' script Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 10/27] migration: Fix migrate-set-parameters argument validation Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 11/27] migration: Clean up signed vs. unsigned XBZRLE cache-size Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 12/27] migration: Fix cache_init()'s "Failed to allocate" error messages Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 13/27] migration: Fix a few absurdly defective " Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 14/27] migration: Add blocker information Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 15/27] migration: Display the migration blockers Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 16/27] block: push error reporting into bdrv_all_*_snapshot functions Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 17/27] migration: Make save_snapshot() return bool, not 0/-1 Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 18/27] migration: stop returning errno from load_snapshot() Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 19/27] block: add ability to specify list of blockdevs during snapshot Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 20/27] block: allow specifying name of block device for vmstate storage Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 21/27] block: rename and alter bdrv_all_find_snapshot semantics Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 22/27] migration: control whether snapshots are ovewritten Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 23/27] migration: wire up support for snapshot device selection Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 24/27] migration: introduce a delete_snapshot wrapper Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 25/27] iotests: add support for capturing and matching QMP events Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 26/27] iotests: fix loading of common.config from tests/ subdir Dr. David Alan Gilbert (git)
2021-02-04 16:39 ` [PULL 27/27] migration: introduce snapshot-{save, load, delete} QMP commands Dr. David Alan Gilbert (git)
2021-02-04 19:48 ` [PULL 00/27] migration queue Peter Maydell
2021-02-04 19:51   ` Dr. David Alan Gilbert
2021-02-08 10:42   ` Dr. David Alan Gilbert
2021-02-08 11:28 Dr. David Alan Gilbert (git)
2021-02-08 11:28 ` [PULL 02/27] savevm: Fix memory leak of vmstate_configuration Dr. David Alan Gilbert (git)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210204163959.377618-3-dgilbert@redhat.com \
    --to=dgilbert@redhat.com \
    --cc=andrey.gruzdev@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=gaojinhao@huawei.com \
    --cc=mst@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wainersm@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.