Fix memory leak of some device state in migration
v3 cover Fix memory leak of some device state in migration
For some device state having some fields of VMS_ALLOC flag, they don't free memory allocated for the fields in vmstate_save_state and vmstate_load_state. We add funcs or sentences of free memory before and after VM saves or loads device state to avoid memory leak.
v2
- Drop patch1-3,6-8 of v1
- Address Michael's comment (free memory before load vmsd centrally)
- Add David's Acked-by and Michael's Signed-off-by
v3
- Add Euler's Reported-by and Michael's Reviewed-by
Jinhao Gao (3):
spapr_pci: Fix memory leak of vmstate_spapr_pci
savevm: Fix memory leak of vmstate_configuration
vmstate: Fix memory leak in vmstate_handle_alloc()
hw/ppc/spapr_pci.c | 11 +++++++++++ migration/savevm.c | 31 +++++++++++++++++++++++++++---- migration/vmstate.c | 1 + 3 files changed, 39 insertions(+), 4 deletions(-)
Signature · 1 line
2.23.0
PATCH v3 1/3 spapr_pci: Fix memory leak of vmstate_spapr_pci
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.
--- hw/ppc/spapr_pci.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
hw/ppc/spapr_pci.c +11 -0
@@ -2173,6 +2173,16 @@ static int spapr_pci_pre_save(void *opaque)return 0 ; } +static int spapr_pci_post_save (void *opaque) +{ +SpaprPhbState *sphb = opaque; + +g_free (sphb->msi_devs); + sphb->msi_devs =NULL ; + sphb->msi_devs_num =0 ; +return 0 ; +} +static int spapr_pci_post_load (void *opaque,int version_id) {SpaprPhbState *sphb = opaque;
@@ -2205,6 +2215,7 @@ static const VMStateDescription vmstate_spapr_pci = { .version_id = 2, .minimum_version_id = 2, .pre_save = spapr_pci_pre_save, + .post_save = spapr_pci_post_save, .post_load = spapr_pci_post_load, .fields = (VMStateField []) {VMSTATE_UINT64_EQUAL (buid, SpaprPhbState,NULL ),
Signature · 1 line
2.23.0
PATCH v3 2/3 savevm: Fix memory leak of vmstate_configuration
In reply to Jinhao GaoWhen 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.
--- migration/savevm.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-)
migration/savevm.c +27 -4
@@ -314,6 +314,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;
@@ -364,24 +374,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,
@@ -515,6 +537,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),
Signature · 1 line
2.23.0
PATCH v3 3/3 vmstate: Fix memory leak in vmstate_handle_alloc()
In reply to Jinhao GaoSome 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.
--- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+)
migration/vmstate.c +1 -0
@@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,gsize size =vmstate_size (opaque, field); size *=vmstate_n_elems (opaque, field);if (size) { +g_free (*(void **)ptr); *(void **)ptr =g_malloc (size); } }
Signature · 1 line
2.23.0
Re: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
Quoted 7 lines · * Jinhao Gao ([email protected]) wrote:
Some memory allocated for fields having a flag of
VMS_ALLOCin SaveState may not free before VM load vmsd in migration. So we pre-free memory before allocation invmstate_handle_alloc()to avoid memleaks.Reported-by Euler Robot <[email protected]>Signed-off-by Jinhao Gao <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
Yes, I think that's OK; it's actually pretty rare for this to happen; normally inwards migrations either succeed or fail and exit; doing multiple loads from snapshots is valid and I guess COLO hits this as well.
Quoted 19 lines
--- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+)migration/vmstate.c +1
-0@@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,gsize size =vmstate_size (opaque, field); size *=vmstate_n_elems (opaque, field);if (size) { +g_free (*(void **)ptr); *(void **)ptr =g_malloc (size); } }Signature · 1 line
2.23.0
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
gaojinhao
Reviewed-by Dr. David Alan GilbertRE: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
Thank you for review!
Jinhao Gao
-----Original Message-----
From: Dr. David Alan Gilbert [mailto:[email protected]]
Sent: 2021年1月5日 19:18
To: gaojinhao <[email protected]>
Cc: [email protected]; [email protected]; Michael S . Tsirkin <[email protected]>; David Gibson <[email protected]>; Greg Kurz <[email protected]>; Marc-André Lureau <[email protected]>; Stefan Berger <[email protected]>; Jason Wang <[email protected]>; Juan Quintela <[email protected]>; Wanghaibin (D) <[email protected]>; zhukeqian <[email protected]>
Subject: Re: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
Quoted 7 lines · * Jinhao Gao ([email protected]) wrote:
Some memory allocated for fields having a flag of
VMS_ALLOCin SaveState may not free before VM load vmsd in migration. So we pre-free memory before allocation invmstate_handle_alloc()to avoid memleaks.Reported-by Euler Robot <[email protected]>Signed-off-by Jinhao Gao <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
Yes, I think that's OK; it's actually pretty rare for this to happen; normally inwards migrations either succeed or fail and exit; doing multiple loads from snapshots is valid and I guess COLO hits this as well.
Quoted 17 lines
--- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+)migration/vmstate.c index renamed from migration/vmstate.c +0
-0
e9d2aef66b..873f76739f100644migration/vmstate.c +1
-1@@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,gsize size =vmstate_size (opaque, field); size *=vmstate_n_elems (opaque, field);if (size) { +g_free (*(void **)ptr); *(void **)ptr =g_malloc (size); } } --
Quoted 2 lines
2.23.0
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
In reply to Dr. David Alan GilbertQuoted 9 lines · * Dr. David Alan Gilbert ([email protected]) wrote:
Quoted 7 lines · * Jinhao Gao ([email protected]) wrote:
Some memory allocated for fields having a flag of
VMS_ALLOCin SaveState may not free before VM load vmsd in migration. So we pre-free memory before allocation invmstate_handle_alloc()to avoid memleaks.Reported-by Euler Robot <[email protected]>Signed-off-by Jinhao Gao <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
Quoted 5 lines
Yes, I think that's OK; it's actually pretty rare for this to happen; normally inwards migrations either succeed or fail and exit; doing multiple loads from snapshots is valid and I guess COLO hits this as well.
Reviewed-by Dr. David Alan Gilbert <[email protected]>
I'm having to unqueue this because it's triggering a seg fault on Power in iotest 267 (just run make check).
#2 0x0000000116d0d4c8 in vmstate_handle_alloc (opaque=<optimized out>, field=0x11799e0c8 <__compound_literal.1+312>, ptr=0x1001f8f14b0) at ../qemu/migration/vmstate.c:73 #3 0x0000000116d0d4c8 in vmstate_load_state (f=0x1001f6d0000, vmsd=0x117928730 <vmstate_spapr_tce_table>, opaque=0x1001f8f1400, version_id=<optimized out>) at ../qemu/migration/vmstate.c:122 #4 0x0000000116fb4a4c in vmstate_load (f=0x1001f6d0000, se=0x1001fc7bc40) at ../qemu/migration/savevm.c:910 #5 0x0000000116fb5010 in qemu_loadvm_section_start_full (f=f@entry=0x1001f6d0000, mis=<optimized out>) at ../qemu/migration/savevm.c:2433
It's the mig_nb_table that Power is doing some special handling with; so it needs some more checking before we can fix this.
Dave
Quoted 21 lines
Quoted 19 lines
--- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+)migration/vmstate.c +1
-0@@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,gsize size =vmstate_size (opaque, field); size *=vmstate_n_elems (opaque, field);if (size) { +g_free (*(void **)ptr); *(void **)ptr =g_malloc (size); } }Signature · 1 line
2.23.0Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
RE: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
Hi, Dave,
I will check the code about vmstate_spapr_tce_table to figure out the problem of seg fault. Thank you for your check.
Jinhao Gao
-----Original Message-----
From: Dr. David Alan Gilbert [mailto:[email protected]]
Sent: 2021年2月8日 18:53
To: gaojinhao <[email protected]>
Cc: [email protected]; [email protected]; Michael S . Tsirkin <[email protected]>; David Gibson <[email protected]>; Greg Kurz <[email protected]>; Marc-André Lureau <[email protected]>; Stefan Berger <[email protected]>; Jason Wang <[email protected]>; Juan Quintela <[email protected]>; Wanghaibin (D) <[email protected]>; zhukeqian <[email protected]>
Subject: Re: [PATCH v3 3/3] vmstate: Fix memory leak in vmstate_handle_alloc()
Quoted 9 lines · * Dr. David Alan Gilbert ([email protected]) wrote:
Quoted 7 lines · * Jinhao Gao ([email protected]) wrote:
Some memory allocated for fields having a flag of
VMS_ALLOCin SaveState may not free before VM load vmsd in migration. So we pre-free memory before allocation invmstate_handle_alloc()to avoid memleaks.Reported-by Euler Robot <[email protected]>Signed-off-by Jinhao Gao <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
Quoted 5 lines
Yes, I think that's OK; it's actually pretty rare for this to happen; normally inwards migrations either succeed or fail and exit; doing multiple loads from snapshots is valid and I guess COLO hits this as well.
Reviewed-by Dr. David Alan Gilbert <[email protected]>
I'm having to unqueue this because it's triggering a seg fault on Power in iotest 267 (just run make check).
#2 0x0000000116d0d4c8 in vmstate_handle_alloc (opaque=<optimized out>, field=0x11799e0c8 <__compound_literal.1+312>, ptr=0x1001f8f14b0) at ../qemu/migration/vmstate.c:73 #3 0x0000000116d0d4c8 in vmstate_load_state (f=0x1001f6d0000, vmsd=0x117928730 <vmstate_spapr_tce_table>, opaque=0x1001f8f1400, version_id=<optimized out>) at ../qemu/migration/vmstate.c:122 #4 0x0000000116fb4a4c in vmstate_load (f=0x1001f6d0000, se=0x1001fc7bc40) at ../qemu/migration/savevm.c:910 #5 0x0000000116fb5010 in qemu_loadvm_section_start_full (f=f@entry=0x1001f6d0000, mis=<optimized out>) at ../qemu/migration/savevm.c:2433
It's the mig_nb_table that Power is doing some special handling with; so it needs some more checking before we can fix this.
Dave
Quoted 13 lines
Quoted 13 lines
--- migration/vmstate.c | 1 + 1 file changed, 1 insertion(+)migration/vmstate.c index renamed from migration/vmstate.c +0
-0
e9d2aef66b..873f76739f100644migration/vmstate.c +1
-0@@ -70,6 +70,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,gsize size =vmstate_size (opaque, field); size *=vmstate_n_elems (opaque, field);if (size) { +g_free (*(void **)ptr);
Quoted 8 lines
Quoted 6 lines
migration/vmstate.c +0
-1*(void **)ptr =g_malloc (size); } } --2.23.0
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 4 lines · * Jinhao Gao ([email protected]) wrote:
For some device state having some fields of
VMS_ALLOCflag, they don't free memory allocated for the fields invmstate_save_stateandvmstate_load_state. We add funcs or sentences of free memory before and after VM saves or loads device state to avoid memory leak.
Queued
Quoted 23 lines
v2
- Drop patch1-3,6-8 of v1
- Address Michael's comment (free memory before load vmsd centrally)
- Add David's Acked-by and Michael's Signed-off-byv3
- Add Euler's Reported-by and Michael's Reviewed-byJinhao Gao (3):
spapr_pci: Fix memory leak ofvmstate_spapr_pci
savevm: Fix memory leak ofvmstate_configuration
vmstate: Fix memory leak invmstate_handle_alloc()hw/ppc/spapr_pci.c | 11 +++++++++++ migration/savevm.c | 31 +++++++++++++++++++++++++++---- migration/vmstate.c | 1 + 3 files changed, 39 insertions(+), 4 deletions(-)Signature · 1 line
2.23.0
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK