savevm: Fix memory leak of vmstate_configuration

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>
This commit is contained in:
Jinhao Gao 2020-12-31 14:10:19 +08:00 committed by Dr. David Alan Gilbert
parent e6ddad1fd5
commit 39f633d429
1 changed files with 27 additions and 4 deletions

View File

@ -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),