Skip to contentSkip to conversation

usb: usb host adapter hotplug

PATCH Superseded [email protected] 2 participants 14 messages Jun 3, 2014 – Jun 4, 2014

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover usb: usb host adapter hotplug

From: Gonglei <[email protected]>

add support for usb host adapter hotplug, as the same as other pci devices.

changes since v1:
* rework usb_bus_release function suggested by Gerd.
* add more completely resource cleanup for every usb host adapter.
* fix memory leak.

Gonglei (9):
usb: add usb_bus_release function
usb-ohci: Fix memory leak for ohci timer
usb-ohci: add exit function
usb-uhci: clean up uhci resource when pci-uhci exit
usb-ehci: add vmstate properity for EHCIState
usb-ehci: expose ehci_queues_rip_all
usb-ehci: add ehci-pci device exit function
usb-xhci: add exit function
usb: tag usb host controller as hotpluggable

 hw/usb/bus.c          |  7 +++++++
 hw/usb/hcd-ehci-pci.c | 34 +++++++++++++++++++++++++++++++++-
 hw/usb/hcd-ehci.c     |  4 ++--
 hw/usb/hcd-ehci.h     |  2 ++
 hw/usb/hcd-ohci.c     | 26 ++++++++++++++++++++++++--
 hw/usb/hcd-uhci.c     | 17 ++++++++++++++++-
 hw/usb/hcd-xhci.c     | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 include/hw/usb.h      |  1 +
 8 files changed, 131 insertions(+), 7 deletions(-)
Signature · 1 line
1.7.12.4
#1 lore raw

PATCH v2 1/9 usb: add usb_bus_release function

From: Gonglei <[email protected]>

add global variables releasing logic when the usb buses were removed or hot-unpluged.

---
 hw/usb/bus.c     | 7 +++++++
 include/hw/usb.h | 1 +
 2 files changed, 8 insertions(+)
hw/usb/bus.c +7 -0
index 927a47b..84e8bb4 100644
@@ -87,6 +87,13 @@ void usb_bus_new(USBBus *bus, size_t bus_size,
     QTAILQ_INSERT_TAIL(&busses, bus, next);
 }
 
+void usb_bus_release(USBBus *bus)
+{
+    assert(next_usb_bus > 0);
+
+    QTAILQ_REMOVE(&busses, bus, next);
+}
+
 USBBus *usb_bus_find(int busnr)
 {
     USBBus *bus;
include/hw/usb.h +1 -0
index 1919bdc..f0f9e73 100644
@@ -523,6 +523,7 @@ struct USBBusOps {
 
 void usb_bus_new(USBBus *bus, size_t bus_size,
                  USBBusOps *ops, DeviceState *host);
+void usb_bus_release(USBBus *bus);
 USBBus *usb_bus_find(int busnr);
 void usb_legacy_register(const char *typename, const char *usbdevice_name,
                          USBDevice *(*usbdevice_init)(USBBus *bus,
Signature · 1 line
1.7.12.4
#2 lore raw

PATCH v2 2/9 usb-ohci: Fix memory leak for ohci timer

In reply to [email protected]

From: Gonglei <[email protected]>

---
 hw/usb/hcd-ohci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/usb/hcd-ohci.c +3 -1
index cd87074..dc1adbf 100644
@@ -1371,8 +1371,10 @@ static int ohci_bus_start(OHCIState *ohci)
 /* Stop sending SOF tokens on the bus */
 static void ohci_bus_stop(OHCIState *ohci)
 {
-    if (ohci->eof_timer)
+    if (ohci->eof_timer) {
         timer_del(ohci->eof_timer);
+        timer_free(ohci->eof_timer);
+    }
     ohci->eof_timer = NULL;
 }
 
Signature · 1 line
1.7.12.4
#3 lore raw

PATCH v2 3/9 usb-ohci: add exit function

In reply to [email protected]

From: Gonglei <[email protected]>

clean up ohci resource when ohci pci device exit

---
 hw/usb/hcd-ohci.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
hw/usb/hcd-ohci.c +21 -0
index dc1adbf..b513a13 100644
@@ -1954,6 +1954,26 @@ static int usb_ohci_initfn_pci(PCIDevice *dev)
     return 0;
 }
 
+static void usb_ohci_exit(PCIDevice *dev)
+{
+    OHCIPCIState *ohci = PCI_OHCI(dev);
+    OHCIState *s = &ohci->state;
+
+    ohci_bus_stop(s);
+
+    if (s->async_td) {
+        usb_cancel_packet(&s->usb_packet);
+        s->async_td = 0;
+    }
+    ohci_stop_endpoints(s);
+
+    memory_region_destroy(&s->mem);
+
+    if (!ohci->masterbus) {
+        usb_bus_release(&s->bus);
+    }
+}
+
 #define TYPE_SYSBUS_OHCI "sysbus-ohci"
 #define SYSBUS_OHCI(obj) OBJECT_CHECK(OHCISysBusState, (obj), TYPE_SYSBUS_OHCI)
 
@@ -2094,6 +2114,7 @@ static void ohci_pci_class_init(ObjectClass *klass, void *data)
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 
     k->init = usb_ohci_initfn_pci;
+    k->exit = usb_ohci_exit;
     k->vendor_id = PCI_VENDOR_ID_APPLE;
     k->device_id = PCI_DEVICE_ID_APPLE_IPID_USB;
     k->class_id = PCI_CLASS_SERIAL_USB;
Signature · 1 line
1.7.12.4
#4 lore raw

PATCH v2 4/9 usb-uhci: clean up uhci resource when pci-uhci exit

In reply to [email protected]

From: Gonglei <[email protected]>

clean up uhci resource when uhci pci device exit.

---
 hw/usb/hcd-uhci.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
hw/usb/hcd-uhci.c +16 -0
index 9b1166b..4697a2d 100644
@@ -1294,7 +1294,23 @@ static void usb_uhci_exit(PCIDevice *dev)
 {
     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
 
+    if (s->frame_timer) {
+        timer_del(s->frame_timer);
+        timer_free(s->frame_timer);
+        s->frame_timer = NULL;
+    }
+
+    if (s->bh) {
+        qemu_bh_delete(s->bh);
+    }
+
+    uhci_async_cancel_all(s);
+
     memory_region_destroy(&s->io_bar);
+
+    if (!s->masterbus) {
+        usb_bus_release(&s->bus);
+    }
 }
 
 static Property uhci_properties[] = {
Signature · 1 line
1.7.12.4
#5 lore raw

PATCH v2 5/9 usb-ehci: add vmstate properity for EHCIState

In reply to [email protected]

From: Gonglei <[email protected]>

since hotunplug the ehci host adapter, we should
delete vm_change_state_handler also, so the
VMChangeStateEntry should be saved in EHCIState.

---
 hw/usb/hcd-ehci.c | 2 +-
 hw/usb/hcd-ehci.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
hw/usb/hcd-ehci.c +1 -1
index a3ae9f2..0e7cd3b 100644
@@ -2545,7 +2545,7 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp)
     s->device = dev;
 
     qemu_register_reset(ehci_reset, s);
-    qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
+    s->vmstate = qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
 }
 
 void usb_ehci_init(EHCIState *s, DeviceState *dev)
hw/usb/hcd-ehci.h +1 -0
index 1ad4b96..594d9d3 100644
@@ -316,6 +316,7 @@ struct EHCIState {
     uint32_t async_stepdown;
     uint32_t periodic_sched_active;
     bool int_req_by_async;
+    VMChangeStateEntry *vmstate;
 };
 
 extern const VMStateDescription vmstate_ehci;
Signature · 1 line
1.7.12.4
#6 lore raw

PATCH v2 6/9 usb-ehci: expose ehci_queues_rip_all

In reply to [email protected]

From: Gonglei <[email protected]>

As for usb-ehci-pci.c can call the function to
free ehci queue.

---
 hw/usb/hcd-ehci.c | 2 +-
 hw/usb/hcd-ehci.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
hw/usb/hcd-ehci.c +1 -1
index 0e7cd3b..a585a94 100644
@@ -746,7 +746,7 @@ static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
     }
 }
 
-static void ehci_queues_rip_all(EHCIState *ehci, int async)
+void ehci_queues_rip_all(EHCIState *ehci, int async)
 {
     EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
     const char *warn = async ? "guest stopped busy async schedule" : NULL;
hw/usb/hcd-ehci.h +1 -0
index 594d9d3..19e079e 100644
@@ -323,6 +323,7 @@ extern const VMStateDescription vmstate_ehci;
 
 void usb_ehci_init(EHCIState *s, DeviceState *dev);
 void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp);
+void ehci_queues_rip_all(EHCIState *ehci, int async);
 
 #define TYPE_PCI_EHCI "pci-ehci-usb"
 #define PCI_EHCI(obj) OBJECT_CHECK(EHCIPCIState, (obj), TYPE_PCI_EHCI)
Signature · 1 line
1.7.12.4
#7 lore raw

PATCH v2 7/9 usb-ehci: add ehci-pci device exit function

In reply to [email protected]

From: Gonglei <[email protected]>

clean up ehci resource when ehci pci device exit.

---
 hw/usb/hcd-ehci-pci.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
hw/usb/hcd-ehci-pci.c +33 -0
index 505741a..115d9b5 100644
@@ -84,6 +84,38 @@ static void usb_ehci_pci_init(Object *obj)
     usb_ehci_init(s, DEVICE(obj));
 }
 
+static void usb_ehci_pci_exit(PCIDevice *dev)
+{
+    EHCIPCIState *i = PCI_EHCI(dev);
+    EHCIState *s = &i->ehci;
+
+    if (s->frame_timer) {
+        timer_del(s->frame_timer);
+        timer_free(s->frame_timer);
+        s->frame_timer = NULL;
+    }
+    if (s->async_bh) {
+        qemu_bh_delete(s->async_bh);
+    }
+
+    ehci_queues_rip_all(s, 0);
+    ehci_queues_rip_all(s, 1);
+
+    memory_region_del_subregion(&s->mem, &s->mem_caps);
+    memory_region_del_subregion(&s->mem, &s->mem_opreg);
+    memory_region_del_subregion(&s->mem, &s->mem_ports);
+    memory_region_destroy(&s->mem_caps);
+    memory_region_destroy(&s->mem_opreg);
+    memory_region_destroy(&s->mem_ports);
+    memory_region_destroy(&s->mem);
+
+    usb_bus_release(&s->bus);
+
+    if (s->vmstate) {
+        qemu_del_vm_change_state_handler(s->vmstate);
+    }
+}
+
 static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
                                       uint32_t val, int l)
 {
@@ -121,6 +153,7 @@ static void ehci_class_init(ObjectClass *klass, void *data)
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 
     k->init = usb_ehci_pci_initfn;
+    k->exit = usb_ehci_pci_exit;
     k->class_id = PCI_CLASS_SERIAL_USB;
     k->config_write = usb_ehci_pci_write_config;
     dc->hotpluggable = false;
Signature · 1 line
1.7.12.4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] usb-ehci: add ehci-pci device exit function

Quoted 14 lines · On Di, 2014-06-03 at 18:54 +0800, [email protected] wrote:

From: Gonglei <[email protected]>

clean up ehci resource when ehci pci device exit.

---
 hw/usb/hcd-ehci-pci.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
hw/usb/hcd-ehci-pci.c +0 -0
index 505741a..115d9b5 100644

hw/usb/hcd-ehci-pci.c static void usb_ehci_pci_init(Object *obj) · line 88

Quoted 5 lines
hw/usb/hcd-ehci-pci.c +2 -0
@@ -84,6 +84,38 @@ static void usb_ehci_pci_init(Object *obj)
     usb_ehci_init(s, DEVICE(obj));
 }
 
+static void usb_ehci_pci_exit(PCIDevice *dev)
+{

Too much non-pci stuff in here. I think we should create a usb_ehci_unrealize function and do the cleanup there, then just call this from usb_ehci_pci_exit. You also don't need patch #6 then.

cheers,
Gerd

#9 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] usb-ehci: add ehci-pci device exit function

Quoted 26 lines

-----Original Message-----
From: Gerd Hoffmann [mailto:[email protected]]
Sent: Wednesday, June 04, 2014 2:10 PM
To: Gonglei (Arei)
Cc: [email protected]; Luonengjun; Huangweidong (C); Huangpeng (Peter)
Subject: Re: [PATCH v2 7/9] usb-ehci: add ehci-pci device exit function

Quoted 17 lines · On Di, 2014-06-03 at 18:54 +0800, [email protected] wrote:

From: Gonglei <[email protected]>

clean up ehci resource when ehci pci device exit.

---
 hw/usb/hcd-ehci-pci.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
hw/usb/hcd-ehci-pci.c +0 -0
index 505741a..115d9b5 100644
@@ -84,6 +84,38 @@ static void usb_ehci_pci_init(Object *obj)
     usb_ehci_init(s, DEVICE(obj));
 }
 
Quoted 7 lines

hw/usb/hcd-ehci-pci.c static void usb_ehci_pci_init(Object *obj) · line 88

Quoted 2 lines
hw/usb/hcd-ehci-pci.c +2 -0
+static void usb_ehci_pci_exit(PCIDevice *dev)
+{

Too much non-pci stuff in here. I think we should create a usb_ehci_unrealize function and do the cleanup there, then just call this from usb_ehci_pci_exit. You also don't need patch #6 then.

Good idea! I will do it in v3, Thanks!

Best regards,
-Gonglei

#10 lore raw

PATCH v2 8/9 usb-xhci: add exit function

In reply to [email protected]

From: Gonglei <[email protected]>

clean up xhci resource when xhci pci device exit.

---
 hw/usb/hcd-xhci.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
hw/usb/hcd-xhci.c +46 -0
index 54dea16..da8a1fb 100644
@@ -3617,6 +3617,51 @@ static int usb_xhci_initfn(struct PCIDevice *dev)
     return 0;
 }
 
+static void usb_xhci_exit(PCIDevice *dev)
+{
+    int i;
+    XHCIState *xhci = XHCI(dev);
+
+    for (i = 0; i < xhci->numslots; i++) {
+        xhci_disable_slot(xhci, i + 1);
+    }
+
+    if (xhci->mfwrap_timer) {
+        timer_del(xhci->mfwrap_timer);
+        timer_free(xhci->mfwrap_timer);
+        xhci->mfwrap_timer = NULL;
+    }
+
+    memory_region_del_subregion(&xhci->mem, &xhci->mem_cap);
+    memory_region_del_subregion(&xhci->mem, &xhci->mem_oper);
+    memory_region_del_subregion(&xhci->mem, &xhci->mem_runtime);
+    memory_region_del_subregion(&xhci->mem, &xhci->mem_doorbell);
+
+    memory_region_destroy(&xhci->mem_cap);
+    memory_region_destroy(&xhci->mem_oper);
+    memory_region_destroy(&xhci->mem_runtime);
+    memory_region_destroy(&xhci->mem_doorbell);
+
+    for (i = 0; i < xhci->numports; i++) {
+        XHCIPort *port = &xhci->ports[i];
+        memory_region_del_subregion(&xhci->mem, &port->mem);
+        memory_region_destroy(&port->mem);
+    }
+
+    /* destroy msix memory region */
+    if (dev->msix_table && dev->msix_pba
+        && dev->msix_entry_used) {
+        memory_region_del_subregion(&xhci->mem, &dev->msix_table_mmio);
+        memory_region_destroy(&dev->msix_table_mmio);
+        memory_region_del_subregion(&xhci->mem, &dev->msix_pba_mmio);
+        memory_region_destroy(&dev->msix_pba_mmio);
+    }
+
+    memory_region_destroy(&xhci->mem);
+
+    usb_bus_release(&xhci->bus);
+}
+
 static int usb_xhci_post_load(void *opaque, int version_id)
 {
     XHCIState *xhci = opaque;
@@ -3806,6 +3851,7 @@ static void xhci_class_init(ObjectClass *klass, void *data)
     dc->hotpluggable   = false;
     set_bit(DEVICE_CATEGORY_USB, dc->categories);
     k->init         = usb_xhci_initfn;
+    k->exit         = usb_xhci_exit;
     k->vendor_id    = PCI_VENDOR_ID_NEC;
     k->device_id    = PCI_DEVICE_ID_NEC_UPD720200;
     k->class_id     = PCI_CLASS_SERIAL_USB;
Signature · 1 line
1.7.12.4
#11 lore raw

PATCH v2 9/9 usb: tag usb host controller as hotpluggable

In reply to [email protected]

From: Gonglei <[email protected]>

usb host controller should be able to support hotplug/unplug, as the same as the other pci devices, which not enable multifunction capability.

BTW, the qemu have not the capability to support
hotplug mulitfuncition pci devices at present.

---
 hw/usb/hcd-ehci-pci.c | 1 -
 hw/usb/hcd-ohci.c     | 1 -
 hw/usb/hcd-uhci.c     | 1 -
 hw/usb/hcd-xhci.c     | 1 -
 4 files changed, 4 deletions(-)
hw/usb/hcd-ehci-pci.c +0 -1
index 115d9b5..319fbb9 100644
@@ -156,7 +156,6 @@ static void ehci_class_init(ObjectClass *klass, void *data)
     k->exit = usb_ehci_pci_exit;
     k->class_id = PCI_CLASS_SERIAL_USB;
     k->config_write = usb_ehci_pci_write_config;
-    dc->hotpluggable = false;
     dc->vmsd = &vmstate_ehci_pci;
     dc->props = ehci_pci_properties;
 }
hw/usb/hcd-ohci.c +0 -1
index b513a13..5dfcb6f 100644
@@ -2121,7 +2121,6 @@ static void ohci_pci_class_init(ObjectClass *klass, void *data)
     set_bit(DEVICE_CATEGORY_USB, dc->categories);
     dc->desc = "Apple USB Controller";
     dc->props = ohci_pci_properties;
-    dc->hotpluggable = false;
     dc->vmsd = &vmstate_ohci;
 }
 
hw/usb/hcd-uhci.c +0 -1
index 4697a2d..7f0d447 100644
@@ -1334,7 +1334,6 @@ static void uhci_class_init(ObjectClass *klass, void *data)
     k->device_id = info->device_id;
     k->revision  = info->revision;
     k->class_id  = PCI_CLASS_SERIAL_USB;
-    dc->hotpluggable = false;
     dc->vmsd = &vmstate_uhci;
     dc->props = uhci_properties;
     set_bit(DEVICE_CATEGORY_USB, dc->categories);
hw/usb/hcd-xhci.c +0 -1
index da8a1fb..c3ffa2f 100644
@@ -3848,7 +3848,6 @@ static void xhci_class_init(ObjectClass *klass, void *data)
     dc->vmsd    = &vmstate_xhci;
     dc->props   = xhci_properties;
     dc->reset   = xhci_reset;
-    dc->hotpluggable   = false;
     set_bit(DEVICE_CATEGORY_USB, dc->categories);
     k->init         = usb_xhci_initfn;
     k->exit         = usb_xhci_exit;
Signature · 1 line
1.7.12.4
#13 lore raw
Quoted 8 lines

-----Original Message-----
From: Gerd Hoffmann [mailto:[email protected]]
Sent: Wednesday, June 04, 2014 2:12 PM
To: Gonglei (Arei)
Cc: [email protected]; Luonengjun; Huangweidong (C); Huangpeng (Peter)
Subject: Re: [PATCH v2 0/9] usb: usb host adapter hotplug

Quoted 7 lines

Hi,

Quoted 2 lines

changes since v1:
* add more completely resource cleanup for every usb host adapter.

Looks good overall. How did you test this?

Thanks!

The steps of tested:
1. start a vm using below qemu command line:
./qemu-system-x86_64 -enable-kvm -m 4096 -smp 4 -name win7 -boot c -drive file=/mnt/sdb/gonglei/image/win7_32_2U -vnc 0.0.0.0:10 -monitor stdio

2. hot-plug/unplug an usb host adapters:
2.1) For ehci:
(qemu) device_add usb-ehci,id=ehci
(qemu) device_add usb-tablet,id=input0,bus=ehci.0,port=1 (qemu) device_del ehci
2.2) For uhci:
(qemu) device_add piix3-usb-uhci,id=uhci
(qemu) device_del uhci
2.3) For uhci:
(qemu) device_add pci-ohci,id=ohci
(qemu) device_del ohci
2.4) For uhci:
(qemu) device_add nec-usb-xhci,id=xhci
(qemu) device_del xhci

3. do step #2 repeating, more than 20 times.

BTW, I will add some qtestes for usb host adapters hot-plug/unplug later, and add tracing for exit function.

Best regards,
-Gonglei