Skip to contentSkip to conversation

xhci: Fix memory leak in xhci_kick_epctx when poweroff GuestOS

PATCH In review [email protected] 3 participants 5 messages Jan 10, 2020 – Jan 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xhci: Fix memory leak in xhci_kick_epctx when poweroff GuestOS

From: Chen Qun <[email protected]>

start vm with libvirt, when GuestOS running, enter poweroff command using the xhci keyboard, then ASAN shows memory leak stack:

Direct leak of 80 byte(s) in 5 object(s) allocated from:

    #0 0xfffd1e6431cb in __interceptor_malloc (/lib64/libasan.so.4+0xd31cb)
    #1 0xfffd1e107163 in g_malloc (/lib64/libglib-2.0.so.0+0x57163)
    #2 0xaaad39051367 in qemu_sglist_init /qemu/dma-helpers.c:43
    #3 0xaaad3947c407 in pci_dma_sglist_init /qemu/include/hw/pci/pci.h:842
    #4 0xaaad3947c407 in xhci_xfer_create_sgl /qemu/hw/usb/hcd-xhci.c:1446
    #5 0xaaad3947c407 in xhci_setup_packet /qemu/hw/usb/hcd-xhci.c:1618
    #6 0xaaad3948625f in xhci_submit /qemu/hw/usb/hcd-xhci.c:1827
    #7 0xaaad3948625f in xhci_fire_transfer /qemu/hw/usb/hcd-xhci.c:1839
    #8 0xaaad3948625f in xhci_kick_epctx /qemu/hw/usb/hcd-xhci.c:1991
    #9 0xaaad3948f537 in xhci_doorbell_write /qemu/hw/usb/hcd-xhci.c:3158
    #10 0xaaad38bcbfc7 in memory_region_write_accessor /qemu/memory.c:483
    #11 0xaaad38bc654f in access_with_adjusted_size /qemu/memory.c:544
    #12 0xaaad38bd1877 in memory_region_dispatch_write /qemu/memory.c:1482
    #13 0xaaad38b1c77f in flatview_write_continue /qemu/exec.c:3167
    #14 0xaaad38b1ca83 in flatview_write /qemu/exec.c:3207
    #15 0xaaad38b268db in address_space_write /qemu/exec.c:3297
    #16 0xaaad38bf909b in kvm_cpu_exec /qemu/accel/kvm/kvm-all.c:2383
    #17 0xaaad38bb063f in qemu_kvm_cpu_thread_fn /qemu/cpus.c:1246
    #18 0xaaad39821c93 in qemu_thread_start /qemu/util/qemu-thread-posix.c:519
    #19 0xfffd1c8378bb  (/lib64/libpthread.so.0+0x78bb)
    #20 0xfffd1c77616b  (/lib64/libc.so.6+0xd616b)
---
 hw/usb/hcd-xhci.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c +1 -0
index 80988bb305..0d3d96d05a 100644
@@ -2000,6 +2000,7 @@ static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
         if (xfer != NULL && xfer->running_retry) {
             DPRINTF("xhci: xfer nacked, stopping schedule\n");
             epctx->retry = xfer;
+            xhci_xfer_unmap(xfer);
             break;
         }
         if (count++ > TRANSFER_LIMIT) {
Signature · 1 line
2.23.0
#1 lore raw
Quoted 39 lines · On 1/10/20 11:58 AM, [email protected] wrote:

From: Chen Qun <[email protected]>

start vm with libvirt, when GuestOS running, enter poweroff command using the xhci keyboard, then ASAN shows memory leak stack:

Direct leak of 80 byte(s) in 5 object(s) allocated from:

     #0 0xfffd1e6431cb in __interceptor_malloc (/lib64/libasan.so.4+0xd31cb)
     #1 0xfffd1e107163 in g_malloc (/lib64/libglib-2.0.so.0+0x57163)
     #2 0xaaad39051367 in qemu_sglist_init /qemu/dma-helpers.c:43
     #3 0xaaad3947c407 in pci_dma_sglist_init /qemu/include/hw/pci/pci.h:842
     #4 0xaaad3947c407 in xhci_xfer_create_sgl /qemu/hw/usb/hcd-xhci.c:1446
     #5 0xaaad3947c407 in xhci_setup_packet /qemu/hw/usb/hcd-xhci.c:1618
     #6 0xaaad3948625f in xhci_submit /qemu/hw/usb/hcd-xhci.c:1827
     #7 0xaaad3948625f in xhci_fire_transfer /qemu/hw/usb/hcd-xhci.c:1839
     #8 0xaaad3948625f in xhci_kick_epctx /qemu/hw/usb/hcd-xhci.c:1991
     #9 0xaaad3948f537 in xhci_doorbell_write /qemu/hw/usb/hcd-xhci.c:3158
     #10 0xaaad38bcbfc7 in memory_region_write_accessor /qemu/memory.c:483
     #11 0xaaad38bc654f in access_with_adjusted_size /qemu/memory.c:544
     #12 0xaaad38bd1877 in memory_region_dispatch_write /qemu/memory.c:1482
     #13 0xaaad38b1c77f in flatview_write_continue /qemu/exec.c:3167
     #14 0xaaad38b1ca83 in flatview_write /qemu/exec.c:3207
     #15 0xaaad38b268db in address_space_write /qemu/exec.c:3297
     #16 0xaaad38bf909b in kvm_cpu_exec /qemu/accel/kvm/kvm-all.c:2383
     #17 0xaaad38bb063f in qemu_kvm_cpu_thread_fn /qemu/cpus.c:1246
     #18 0xaaad39821c93 in qemu_thread_start /qemu/util/qemu-thread-posix.c:519
     #19 0xfffd1c8378bb  (/lib64/libpthread.so.0+0x78bb)
     #20 0xfffd1c77616b  (/lib64/libc.so.6+0xd616b)
---
  hw/usb/hcd-xhci.c | 1 +
  1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c +0 -0
index 80988bb305..0d3d96d05a 100644

hw/usb/hcd-xhci.c static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid) · line 2003

Quoted 4 lines
hw/usb/hcd-xhci.c +1 -0
@@ -2000,6 +2000,7 @@ static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
          if (xfer != NULL && xfer->running_retry) {
              DPRINTF("xhci: xfer nacked, stopping schedule\n");
              epctx->retry = xfer;
+            xhci_xfer_unmap(xfer);

Shouldn't we use xhci_ep_free_xfer() instead?
Also, it would be cleaner if you set it to NULL.

Quoted 4 lines
hw/usb/hcd-xhci.c +0 -0
              break;
          }
          if (count++ > TRANSFER_LIMIT) {
#2 lore raw
Quoted 59 lines

-----Original Message-----
From: Philippe Mathieu-Daudé [mailto:[email protected]]
Sent: Friday, January 10, 2020 9:14 PM
To: Chenqun (kuhn) <[email protected]>; qemu- [email protected]; [email protected]
Cc: [email protected]; Pannengyuan <[email protected]>; Zhanghailiang <[email protected]>
Subject: Re: [PATCH] xhci: Fix memory leak in xhci_kick_epctx when poweroff GuestOS

Quoted 10 lines · On 1/10/20 11:58 AM, [email protected] wrote:

From: Chen Qun <[email protected]>

start vm with libvirt, when GuestOS running, enter poweroff command using the xhci keyboard, then ASAN shows memory leak stack:

Direct leak of 80 byte(s) in 5 object(s) allocated from:

     #0 0xfffd1e6431cb in __interceptor_malloc (/lib64/libasan.so.4+0xd31cb)
     #1 0xfffd1e107163 in g_malloc (/lib64/libglib-2.0.so.0+0x57163)
     #2 0xaaad39051367 in qemu_sglist_init /qemu/dma-helpers.c:43
     #3 0xaaad3947c407 in pci_dma_sglist_init

/qemu/include/hw/pci/pci.h:842

Quoted 1 line
     #4 0xaaad3947c407 in xhci_xfer_create_sgl /qemu/hw/usb/hcd-

xhci.c:1446

Quoted 5 lines
     #5 0xaaad3947c407 in xhci_setup_packet /qemu/hw/usb/hcd-xhci.c:1618
     #6 0xaaad3948625f in xhci_submit /qemu/hw/usb/hcd-xhci.c:1827
     #7 0xaaad3948625f in xhci_fire_transfer /qemu/hw/usb/hcd-xhci.c:1839
     #8 0xaaad3948625f in xhci_kick_epctx /qemu/hw/usb/hcd-xhci.c:1991
     #9 0xaaad3948f537 in xhci_doorbell_write /qemu/hw/usb/hcd-

xhci.c:3158

Quoted 1 line
     #10 0xaaad38bcbfc7 in memory_region_write_accessor

/qemu/memory.c:483

Quoted 2 lines
     #11 0xaaad38bc654f in access_with_adjusted_size /qemu/memory.c:544
     #12 0xaaad38bd1877 in memory_region_dispatch_write

/qemu/memory.c:1482

Quoted 6 lines
     #13 0xaaad38b1c77f in flatview_write_continue /qemu/exec.c:3167
     #14 0xaaad38b1ca83 in flatview_write /qemu/exec.c:3207
     #15 0xaaad38b268db in address_space_write /qemu/exec.c:3297
     #16 0xaaad38bf909b in kvm_cpu_exec /qemu/accel/kvm/kvm-all.c:2383
     #17 0xaaad38bb063f in qemu_kvm_cpu_thread_fn /qemu/cpus.c:1246
     #18 0xaaad39821c93 in qemu_thread_start /qemu/util/qemu-thread-

posix.c:519

hw/usb/hcd-xhci.c static void xhci_kick_epctx(XHCIEPContext *epctx,

Quoted 14 lines
     #19 0xfffd1c8378bb  (/lib64/libpthread.so.0+0x78bb)
     #20 0xfffd1c77616b  (/lib64/libc.so.6+0xd616b)
---
  hw/usb/hcd-xhci.c | 1 +
  1 file changed, 1 insertion(+)
hw/usb/hcd-xhci.c index renamed from hw/usb/hcd-xhci.c +0 -0

80988bb305..0d3d96d05a 100644

hw/usb/hcd-xhci.c +0 -0
@@ -2000,6 +2000,7 @@ static void xhci_kick_epctx(XHCIEPContext *epctx,

unsigned int streamid)

Quoted 8 lines

hw/usb/hcd-xhci.c static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid) · line 2003

Quoted 4 lines
hw/usb/hcd-xhci.c +1 -0
          if (xfer != NULL && xfer->running_retry) {
              DPRINTF("xhci: xfer nacked, stopping schedule\n");
              epctx->retry = xfer;
+            xhci_xfer_unmap(xfer);

Shouldn't we use xhci_ep_free_xfer() instead? Also, it would be cleaner if you set it to NULL.

Hi Philippe,
Thanks for your reply!
But, It is just a sglist leak. xhci_xfer_unmap() can free and set it to NULL. The xhci_ep_free_xfer() did't free a sglist.

By the way, xfer should be use for epctx->retry, we can't free it.

Thanks.

Quoted 4 lines
Quoted 4 lines
hw/usb/hcd-xhci.c +0 -0
              break;
          }
          if (count++ > TRANSFER_LIMIT) {
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines

hw/usb/hcd-xhci.c static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid) · line 2003

Quoted 9 lines
hw/usb/hcd-xhci.c +1 -0
index 80988bb305..0d3d96d05a 100644
@@ -2000,6 +2000,7 @@ static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
          if (xfer != NULL && xfer->running_retry) {
              DPRINTF("xhci: xfer nacked, stopping schedule\n");
              epctx->retry = xfer;
+            xhci_xfer_unmap(xfer);

Shouldn't we use xhci_ep_free_xfer() instead?

No, xhci will try to run the transfer again later.

xhci will re-create the sgl then, so freeing the
sgl here is correct. Patch added to usb queue.

thanks,
Gerd

#4 lore raw
Quoted 22 lines

-----Original Message-----
From: Gerd Hoffmann [mailto:[email protected]]
Sent: Monday, January 13, 2020 3:48 PM
To: Philippe Mathieu-Daudé <[email protected]>
Cc: Chenqun (kuhn) <[email protected]>; qemu- [email protected]; [email protected]; Pannengyuan <[email protected]>; Zhanghailiang <[email protected]>
Subject: Re: [PATCH] xhci: Fix memory leak in xhci_kick_epctx when poweroff GuestOS

Quoted 5 lines

hw/usb/hcd-xhci.c static void xhci_kick_epctx(XHCIEPContext

Quoted 5 lines
hw/usb/hcd-xhci.c index renamed from hw/usb/hcd-xhci.c +0 -0

80988bb305..0d3d96d05a 100644

hw/usb/hcd-xhci.c +0 -0
@@ -2000,6 +2000,7 @@ static void xhci_kick_epctx(XHCIEPContext

*epctx, unsigned int streamid)

Quoted 5 lines
Quoted 4 lines
hw/usb/hcd-xhci.c +1 -0
          if (xfer != NULL && xfer->running_retry) {
              DPRINTF("xhci: xfer nacked, stopping schedule\n");
              epctx->retry = xfer;
+            xhci_xfer_unmap(xfer);
Quoted 6 lines
Quoted 1 line

Shouldn't we use xhci_ep_free_xfer() instead?

No, xhci will try to run the transfer again later.

xhci will re-create the sgl then, so freeing the sgl here is correct. Patch added to usb queue.

Hi Gerd,

I test every keyboard input, it will leak once.
I tested qemu-4.0.0 also had this leak .

Maybe we should cc to qemu-stable ?

Thanks.

Quoted 3 lines

thanks,
Gerd