diff mbox series

virtio-9p-device: fix memleak in virtio_9p_device_unrealize

Message ID 20200114074020.54696-1-pannengyuan@huawei.com (mailing list archive)
State New, archived
Headers show
Series virtio-9p-device: fix memleak in virtio_9p_device_unrealize | expand

Commit Message

Pan Nengyuan Jan. 14, 2020, 7:40 a.m. UTC
From: Pan Nengyuan <pannengyuan@huawei.com>

v->vq forgot to cleanup in virtio_9p_device_unrealize, the memory leak
stack is as follow:

Direct leak of 14336 byte(s) in 2 object(s) allocated from:
  #0 0x7f819ae43970 (/lib64/libasan.so.5+0xef970)  ??:?
  #1 0x7f819872f49d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
  #2 0x55a3a58da624 (./x86_64-softmmu/qemu-system-x86_64+0x2c14624)  /mnt/sdb/qemu/hw/virtio/virtio.c:2327
  #3 0x55a3a571bac7 (./x86_64-softmmu/qemu-system-x86_64+0x2a55ac7)  /mnt/sdb/qemu/hw/9pfs/virtio-9p-device.c:209
  #4 0x55a3a58e7bc6 (./x86_64-softmmu/qemu-system-x86_64+0x2c21bc6)  /mnt/sdb/qemu/hw/virtio/virtio.c:3504
  #5 0x55a3a5ebfb37 (./x86_64-softmmu/qemu-system-x86_64+0x31f9b37)  /mnt/sdb/qemu/hw/core/qdev.c:876

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
---
 hw/9pfs/virtio-9p-device.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Christian Schoenebeck Jan. 14, 2020, 10:08 a.m. UTC | #1
On Dienstag, 14. Januar 2020 08:40:20 CET pannengyuan@huawei.com wrote:
> From: Pan Nengyuan <pannengyuan@huawei.com>
> 
> v->vq forgot to cleanup in virtio_9p_device_unrealize, the memory leak
> stack is as follow:
> 
> Direct leak of 14336 byte(s) in 2 object(s) allocated from:
>   #0 0x7f819ae43970 (/lib64/libasan.so.5+0xef970)  ??:?
>   #1 0x7f819872f49d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
>   #2 0x55a3a58da624 (./x86_64-softmmu/qemu-system-x86_64+0x2c14624) 
> /mnt/sdb/qemu/hw/virtio/virtio.c:2327 #3 0x55a3a571bac7
> (./x86_64-softmmu/qemu-system-x86_64+0x2a55ac7) 
> /mnt/sdb/qemu/hw/9pfs/virtio-9p-device.c:209 #4 0x55a3a58e7bc6
> (./x86_64-softmmu/qemu-system-x86_64+0x2c21bc6) 
> /mnt/sdb/qemu/hw/virtio/virtio.c:3504 #5 0x55a3a5ebfb37
> (./x86_64-softmmu/qemu-system-x86_64+0x31f9b37) 
> /mnt/sdb/qemu/hw/core/qdev.c:876
> 
> Reported-by: Euler Robot <euler.robot@huawei.com>
> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
> ---
>  hw/9pfs/virtio-9p-device.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
> index b5a7c03f26..b146387ae2 100644
> --- a/hw/9pfs/virtio-9p-device.c
> +++ b/hw/9pfs/virtio-9p-device.c
> @@ -215,6 +215,7 @@ static void virtio_9p_device_unrealize(DeviceState *dev,
> Error **errp) V9fsVirtioState *v = VIRTIO_9P(dev);
>      V9fsState *s = &v->state;
> 
> +    virtio_delete_queue(v->vq);
>      virtio_cleanup(vdev);
>      v9fs_device_unrealize_common(s, errp);
>  }

Looks like you are using an old interface. The new one is

	void virtio_del_queue(VirtIODevice *vdev, int n);

Best regards,
Christian Schoenebeck
Christian Schoenebeck Jan. 14, 2020, 2:17 p.m. UTC | #2
On Dienstag, 14. Januar 2020 11:08:59 CET Christian Schoenebeck wrote:
> On Dienstag, 14. Januar 2020 08:40:20 CET pannengyuan@huawei.com wrote:
> > From: Pan Nengyuan <pannengyuan@huawei.com>
> > 
> > v->vq forgot to cleanup in virtio_9p_device_unrealize, the memory leak
> > stack is as follow:
> > 
> > Direct leak of 14336 byte(s) in 2 object(s) allocated from:
> >   #0 0x7f819ae43970 (/lib64/libasan.so.5+0xef970)  ??:?
> >   #1 0x7f819872f49d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
> >   #2 0x55a3a58da624 (./x86_64-softmmu/qemu-system-x86_64+0x2c14624)
> > 
> > /mnt/sdb/qemu/hw/virtio/virtio.c:2327 #3 0x55a3a571bac7
> > (./x86_64-softmmu/qemu-system-x86_64+0x2a55ac7)
> > /mnt/sdb/qemu/hw/9pfs/virtio-9p-device.c:209 #4 0x55a3a58e7bc6
> > (./x86_64-softmmu/qemu-system-x86_64+0x2c21bc6)
> > /mnt/sdb/qemu/hw/virtio/virtio.c:3504 #5 0x55a3a5ebfb37
> > (./x86_64-softmmu/qemu-system-x86_64+0x31f9b37)
> > /mnt/sdb/qemu/hw/core/qdev.c:876
> > 
> > Reported-by: Euler Robot <euler.robot@huawei.com>
> > Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
> > ---
> > 
> >  hw/9pfs/virtio-9p-device.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
> > index b5a7c03f26..b146387ae2 100644
> > --- a/hw/9pfs/virtio-9p-device.c
> > +++ b/hw/9pfs/virtio-9p-device.c
> > @@ -215,6 +215,7 @@ static void virtio_9p_device_unrealize(DeviceState
> > *dev, Error **errp) V9fsVirtioState *v = VIRTIO_9P(dev);
> > 
> >      V9fsState *s = &v->state;
> > 
> > +    virtio_delete_queue(v->vq);
> > 
> >      virtio_cleanup(vdev);
> >      v9fs_device_unrealize_common(s, errp);
> >  
> >  }
> 
> Looks like you are using an old interface. The new one is
> 
> 	void virtio_del_queue(VirtIODevice *vdev, int n);

Ah, my bad, it is actually the other way around, that is I was not up to date; 
virtio_delete_queue() was apparently introduced a week ago:
https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg00723.html

The old virtio_del_queue() still exists though:
https://github.com/qemu/qemu/blob/master/hw/virtio/virtio.c#L2339

Having said that, it would probably still make sense to use virtio_del_queue() 
instead for now to make it easier for stable branches to merge this fix?

Best regards,
Christian Schoenebeck
Pan Nengyuan Jan. 15, 2020, 1:28 a.m. UTC | #3
On 1/14/2020 10:17 PM, Christian Schoenebeck wrote:
> On Dienstag, 14. Januar 2020 11:08:59 CET Christian Schoenebeck wrote:
>> On Dienstag, 14. Januar 2020 08:40:20 CET pannengyuan@huawei.com wrote:
>>> From: Pan Nengyuan <pannengyuan@huawei.com>
>>>
>>> v->vq forgot to cleanup in virtio_9p_device_unrealize, the memory leak
>>> stack is as follow:
>>>
>>> Direct leak of 14336 byte(s) in 2 object(s) allocated from:
>>>   #0 0x7f819ae43970 (/lib64/libasan.so.5+0xef970)  ??:?
>>>   #1 0x7f819872f49d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
>>>   #2 0x55a3a58da624 (./x86_64-softmmu/qemu-system-x86_64+0x2c14624)
>>>
>>> /mnt/sdb/qemu/hw/virtio/virtio.c:2327 #3 0x55a3a571bac7
>>> (./x86_64-softmmu/qemu-system-x86_64+0x2a55ac7)
>>> /mnt/sdb/qemu/hw/9pfs/virtio-9p-device.c:209 #4 0x55a3a58e7bc6
>>> (./x86_64-softmmu/qemu-system-x86_64+0x2c21bc6)
>>> /mnt/sdb/qemu/hw/virtio/virtio.c:3504 #5 0x55a3a5ebfb37
>>> (./x86_64-softmmu/qemu-system-x86_64+0x31f9b37)
>>> /mnt/sdb/qemu/hw/core/qdev.c:876
>>>
>>> Reported-by: Euler Robot <euler.robot@huawei.com>
>>> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
>>> ---
>>>
>>>  hw/9pfs/virtio-9p-device.c | 1 +
>>>  1 file changed, 1 insertion(+)
>>>
>>> diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
>>> index b5a7c03f26..b146387ae2 100644
>>> --- a/hw/9pfs/virtio-9p-device.c
>>> +++ b/hw/9pfs/virtio-9p-device.c
>>> @@ -215,6 +215,7 @@ static void virtio_9p_device_unrealize(DeviceState
>>> *dev, Error **errp) V9fsVirtioState *v = VIRTIO_9P(dev);
>>>
>>>      V9fsState *s = &v->state;
>>>
>>> +    virtio_delete_queue(v->vq);
>>>
>>>      virtio_cleanup(vdev);
>>>      v9fs_device_unrealize_common(s, errp);
>>>  
>>>  }
>>
>> Looks like you are using an old interface. The new one is
>>
>> 	void virtio_del_queue(VirtIODevice *vdev, int n);
> 
> Ah, my bad, it is actually the other way around, that is I was not up to date; 
> virtio_delete_queue() was apparently introduced a week ago:
> https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg00723.html
> 
> The old virtio_del_queue() still exists though:
> https://github.com/qemu/qemu/blob/master/hw/virtio/virtio.c#L2339
> 
> Having said that, it would probably still make sense to use virtio_del_queue() 
> instead for now to make it easier for stable branches to merge this fix?
> 

virtio_delete_queue makes the cleanup more clear and it tends to replace the old one gradually.
So I think it would probably still use virtio_delete_queue.

Given that, maybe we can split this patch?
For example:
1. use virtio_del_queue to fix memleak, it's easier for stable to merge.
2. rename virtio_del_queue to virtio_delete_queue

> Best regards,
> Christian Schoenebeck
> 
> 
> 
> 
> .
>
Christian Schoenebeck Jan. 15, 2020, 11:40 a.m. UTC | #4
On Mittwoch, 15. Januar 2020 02:28:03 CET Pan Nengyuan wrote:
> >>> diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
> >>> index b5a7c03f26..b146387ae2 100644
> >>> --- a/hw/9pfs/virtio-9p-device.c
> >>> +++ b/hw/9pfs/virtio-9p-device.c
> >>> @@ -215,6 +215,7 @@ static void virtio_9p_device_unrealize(DeviceState
> >>> *dev, Error **errp) V9fsVirtioState *v = VIRTIO_9P(dev);
> >>> 
> >>>      V9fsState *s = &v->state;
> >>> 
> >>> +    virtio_delete_queue(v->vq);
> >>> 
> >>>      virtio_cleanup(vdev);
> >>>      v9fs_device_unrealize_common(s, errp);
> >>>  
> >>>  }
> >> 
> >> Looks like you are using an old interface. The new one is
> >> 
> >> 	void virtio_del_queue(VirtIODevice *vdev, int n);
> > 
> > Ah, my bad, it is actually the other way around, that is I was not up to
> > date; virtio_delete_queue() was apparently introduced a week ago:
> > https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg00723.html
> > 
> > The old virtio_del_queue() still exists though:
> > https://github.com/qemu/qemu/blob/master/hw/virtio/virtio.c#L2339
> > 
> > Having said that, it would probably still make sense to use
> > virtio_del_queue() instead for now to make it easier for stable branches
> > to merge this fix?
> virtio_delete_queue makes the cleanup more clear and it tends to replace the
> old one gradually. So I think it would probably still use
> virtio_delete_queue.
> 
> Given that, maybe we can split this patch?
> For example:
> 1. use virtio_del_queue to fix memleak, it's easier for stable to merge.
> 2. rename virtio_del_queue to virtio_delete_queue

Exactly what I had in mind. :) Thanks!

Best regards,
Christian Schoenebeck
diff mbox series

Patch

diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c
index b5a7c03f26..b146387ae2 100644
--- a/hw/9pfs/virtio-9p-device.c
+++ b/hw/9pfs/virtio-9p-device.c
@@ -215,6 +215,7 @@  static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
     V9fsVirtioState *v = VIRTIO_9P(dev);
     V9fsState *s = &v->state;
 
+    virtio_delete_queue(v->vq);
     virtio_cleanup(vdev);
     v9fs_device_unrealize_common(s, errp);
 }