diff mbox series

[6/8] vmbus: Fix memory leak of vmstate_vmbus_chan_req

Message ID 20201226103347.868-7-gaojinhao@huawei.com (mailing list archive)
State New, archived
Headers show
Series Fix memory leak of some device state in migration | expand

Commit Message

gaojinhao Dec. 26, 2020, 10:33 a.m. UTC
From: Jinhao Gao <gaojinhao@huawei.com>

When VM migrate VMState of vmbus/vmbus_chan_req, the field(msg) of
vmbus/vmbus_chan_req having a flag of VMS_ALLOC needs to allocate
memory. If the dst doesn't free memory which has been allocated for
SaveStateEntry of vmbus/vmbus_chan_req before dst loads device state,
it may result that the pointer of msg is overlaid when vm loads.
We add the pre_load func to free memory, which prevents memory leak.

Signed-off-by: Jinhao Gao <gaojinhao@huawei.com>
---
 hw/hyperv/vmbus.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series

Patch

diff --git a/hw/hyperv/vmbus.c b/hw/hyperv/vmbus.c
index a91623aaac..9eda2341f3 100644
--- a/hw/hyperv/vmbus.c
+++ b/hw/hyperv/vmbus.c
@@ -1303,10 +1303,21 @@  typedef struct VMBusChanReqSave {
     ScatterGatherEntry *sgl;
 } VMBusChanReqSave;
 
+static int vmbus_chan_req_pre_load(void *opaque)
+{
+    VMBusChanReqSave *req_save = VMBusChanReqSave(opaque);
+
+    g_free(req_save.msg);
+    req_save.msg = NULL;
+    req_save.msglen = 0;
+    return 0;
+}
+
 static const VMStateDescription vmstate_vmbus_chan_req = {
     .name = "vmbus/vmbus_chan_req",
     .version_id = 0,
     .minimum_version_id = 0,
+    .pre_load = vmbus_chan_req_pre_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(chan_idx, VMBusChanReqSave),
         VMSTATE_UINT16(pkt_type, VMBusChanReqSave),