Skip to contentSkip to conversation

xhci: Fix memory use after free in xhci_free_virt_device

RFC PATCH In review Guenter Roeck 3 participants 14 messages Nov 15, 2016 – Dec 12, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xhci: Fix memory use after free in xhci_free_virt_device

The following use-after-free reports were seen on resume with a specific USB hub.

BUG: KASAN: use-after-free in xhci_free_virt_device+0x8c/0x21c
	at addr ffffffc0cc1a2eb0
BUG: KASAN: use-after-free in xhci_update_tt_active_eps+0x9c/0xdc
	at addr ffffffc0cc1a2eb0

Relevant traceback for the first case is:

xhci_free_virt_device+0x8c/0x21c
xhci_mem_cleanup+0x294/0x81c
xhci_resume+0x410/0x618
xhci_plat_resume+0x54/0x74
platform_pm_resume+0x74/0x84

which points to the following code in xhci_free_virt_device().

	if (dev->tt_info)
                old_active_eps = dev->tt_info->active_eps;

Problem with this code is that xhci_mem_cleanup() cleans up devices starting with slot 1, and dev->tt_info for a device with higher slot number can point back to the tt_info associated with device 1. In lsusb, this looks as follows.

/: Bus 05.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
|__ Port 1: Dev 4, If 0, Class=Hub, Driver=hub/4p, 480M
|__ Port 3: Dev 7, If 0, Class=Vendor Specific Class, Driver=, 12M

When the higher-numbered device is cleared, it tries to access the already released tt_info from slot 1 to get the value of old_active_eps.

The problem is not seen with all USB hubs since not all USB hubs require the cleanup handling in xhci_resume().

---
Marked as RFC because I don't really like this fix at all and would prefer a different solution.

It might be possible, for example, to change the code in xhci_mem_cleanup() from

	for (i = 1; i < MAX_HC_SLOTS; ++i)
		xhci_free_virt_device(xhci, i);
to
	for (i = MAX_HC_SLOTS - 1; i >= 1; i--)
		xhci_free_virt_device(xhci, i);

if it is guaranteed that tt_info never points to a device with higher slot number.

 drivers/usb/host/xhci-mem.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/host/xhci-mem.c +6 -0
index 6afe32381209..97a61c57a2ed 100644
@@ -873,8 +873,14 @@ static void xhci_free_tt_info(struct xhci_hcd *xhci,
 	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
 		/* Multi-TT hubs will have more than one entry */
 		if (tt_info->slot_id == slot_id) {
+			int i;
+
 			slot_found = true;
 			list_del(&tt_info->tt_list);
+			for (i = 0; i < MAX_HC_SLOTS; i++)
+				if (xhci->devs[i] &&
+				    xhci->devs[i]->tt_info == tt_info)
+					xhci->devs[i]->tt_info = NULL;
 			kfree(tt_info);
 		} else if (slot_found) {
 			break;
Signature · 1 line
2.5.0
#1 lore raw
Quoted 30 lines · Guenter Roeck

The following use-after-free reports were seen on resume with a specific USB hub.

BUG: KASAN: use-after-free in xhci_free_virt_device+0x8c/0x21c
	at addr ffffffc0cc1a2eb0
BUG: KASAN: use-after-free in xhci_update_tt_active_eps+0x9c/0xdc
	at addr ffffffc0cc1a2eb0

Relevant traceback for the first case is:

xhci_free_virt_device+0x8c/0x21c
xhci_mem_cleanup+0x294/0x81c
xhci_resume+0x410/0x618
xhci_plat_resume+0x54/0x74
platform_pm_resume+0x74/0x84

which points to the following code in xhci_free_virt_device().

	if (dev->tt_info)
                 old_active_eps = dev->tt_info->active_eps;

Problem with this code is that xhci_mem_cleanup() cleans up devices starting with slot 1, and dev->tt_info for a device with higher slot number can point back to the tt_info associated with device 1. In lsusb, this looks as follows.

/: Bus 05.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
|__ Port 1: Dev 4, If 0, Class=Hub, Driver=hub/4p, 480M
|__ Port 3: Dev 7, If 0, Class=Vendor Specific Class, Driver=, 12M

Quoted 8 lines

When the higher-numbered device is cleared, it tries to access the already released tt_info from slot 1 to get the value of old_active_eps.

The problem is not seen with all USB hubs since not all USB hubs require the cleanup handling in xhci_resume().

---

Thanks, nice catch.

Quoted 3 lines

Marked as RFC because I don't really like this fix at all and would prefer a different solution.

I just started digging into this and see if I can come up with some solution. It's currently a bit messy the whole thing how the tt_info and bw_table are allocated, freed, pointed to and added and removed to/from lists

-Mathias

#2 lore raw

PATCH 1/1 xhci: free xhci virtual devices with leaf nodes first

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

---

Guenter Roeck, does this work for you?

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

---
 drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +36 -2
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 	xhci->devs[slot_id] = NULL;
 }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
+				vdev = xhci->devs[i];
+				if (vdev && (vdev->tt_info == tt_info))
+					xhci_free_virt_devices_depth_first(
+						xhci, i);
+			}
+		}
+	}
+	/* we are now at a leaf device */
+	xhci_free_virt_device(xhci, slot_id);
+}
+
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 		struct usb_device *udev, gfp_t flags)
 {
@@ -1829,8 +1863,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		}
 	}
 
-	for (i = 1; i < MAX_HC_SLOTS; ++i)
-		xhci_free_virt_device(xhci, i);
+	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
+		xhci_free_virt_devices_depth_first(xhci, i);
 
 	dma_pool_destroy(xhci->segment_pool);
 	xhci->segment_pool = NULL;
Signature · 1 line
1.9.1
#3 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 12 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

Quoted 6 lines

---

Guenter Roeck, does this work for you?

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

I'll give it a try. One concern, though: xhci_free_virt_device() is called from multiple places, and does not always remove all devices. Is it save to assume that all other callers remove children first ?

Thanks,
Guenter

Quoted 61 lines
---
 drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +36 -2
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 	xhci->devs[slot_id] = NULL;
 }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
+				vdev = xhci->devs[i];
+				if (vdev && (vdev->tt_info == tt_info))
+					xhci_free_virt_devices_depth_first(
+						xhci, i);
+			}
+		}
+	}
+	/* we are now at a leaf device */
+	xhci_free_virt_device(xhci, slot_id);
+}
+
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 		struct usb_device *udev, gfp_t flags)
 {
@@ -1829,8 +1863,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		}
 	}
 
-	for (i = 1; i < MAX_HC_SLOTS; ++i)
-		xhci_free_virt_device(xhci, i);
+	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
+		xhci_free_virt_devices_depth_first(xhci, i);
 
 	dma_pool_destroy(xhci->segment_pool);
 	xhci->segment_pool = NULL;
#4 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 15 lines · Guenter Roeck
Quoted 14 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

---

Quoted 9 lines
Quoted 4 lines

Guenter Roeck, does this work for you?

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

I'll give it a try. One concern, though: xhci_free_virt_device() is called from multiple places, and does not always remove all devices. Is it save to assume that all other callers remove children first ?

This should be the only place that xhci does a massive xhci_free_virt_device(),

In the other places it's initiated per device by usb core which should handle child-parent relationships, or then just error paths when failing to allocate a device in the first place (no children yet)

-Mathias

#5 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

In reply to Mathias Nyman

Hi,

Quoted 41 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

---

Guenter Roeck, does this work for you?

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

---
 drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +11 -0
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 	xhci->devs[slot_id] = NULL;
 }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1002

Quoted 10 lines
drivers/usb/host/xhci-mem.c +10 -0
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
		if (tt_info->slot_id != slot_id)
                	continue;

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1004

Quoted 2 lines
drivers/usb/host/xhci-mem.c +2 -0
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {

off-by-one here ? Why is i starting from 1?

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1006

Quoted 2 lines
drivers/usb/host/xhci-mem.c +2 -0
+				vdev = xhci->devs[i];
+				if (vdev && (vdev->tt_info == tt_info))
			if (!vdev || vdev->tt_info != tt_info)
                        	continue;
Quoted 13 lines
drivers/usb/host/xhci-mem.c +9 -0
+					xhci_free_virt_devices_depth_first(
+						xhci, i);
+			}
+		}
+	}
+	/* we are now at a leaf device */
+	xhci_free_virt_device(xhci, slot_id);
+}
+
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 		struct usb_device *udev, gfp_t flags)
 {

drivers/usb/host/xhci-mem.c void xhci_mem_cleanup(struct xhci_hcd *xhci) · line 1866

Quoted 6 lines
drivers/usb/host/xhci-mem.c +1 -2
@@ -1829,8 +1863,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		}
 	}
 
-	for (i = 1; i < MAX_HC_SLOTS; ++i)
-		xhci_free_virt_device(xhci, i);
+	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)

converting MAX_HC_SLOTS to HCS_MAX_SLOTS(xhci->hcs_params1) seems unrelated to $subject. Perhaps just mention on commit log?

Signature · 1 line
balbi
#6 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 59 lines · Felipe Balbi

Hi,

Quoted 41 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

---

Guenter Roeck, does this work for you?

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

---
  drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
  1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +11 -0
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  	xhci->devs[slot_id] = NULL;
  }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1002

Quoted 10 lines
drivers/usb/host/xhci-mem.c +10 -0
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
		if (tt_info->slot_id != slot_id)
                 	continue;
Quoted 6 lines

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1004

Quoted 2 lines
drivers/usb/host/xhci-mem.c +2 -0
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {

off-by-one here ? Why is i starting from 1?

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1005

Quoted 1 line
drivers/usb/host/xhci-mem.c +1 -0
+				vdev = xhci->devs[i];

slit_id 0 is reserved and xhci->devs[0] is not used, so ne need to check it.

All other places that check xhci->devs[0] are avtually buggy

-Mathias

#7 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Hi,

Quoted 9 lines · Mathias Nyman
Quoted 6 lines

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1004

Quoted 2 lines
drivers/usb/host/xhci-mem.c +2 -0
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {

off-by-one here ? Why is i starting from 1?

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1005

Quoted 1 line
drivers/usb/host/xhci-mem.c +1 -0
+				vdev = xhci->devs[i];

slit_id 0 is reserved and xhci->devs[0] is not used, so ne need to check it.

hmm... it's reserved for the HW, sure. Do you need to over allocate the array by 1 just to keep this first member unused? Couldn't you handle the +1/-1 (depending on the case) in xhci driver itself? Saves a bit of memory there.

Quoted 1 line

All other places that check xhci->devs[0] are avtually buggy

fair enough, sounds like an accessor guaranteeing the 'rules of engagement' for this would be useful.

Signature · 1 line
balbi
#8 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 14 lines · Felipe Balbi

Hi,

Quoted 9 lines · Mathias Nyman
Quoted 6 lines

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1004

Quoted 2 lines
drivers/usb/host/xhci-mem.c +2 -0
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {

off-by-one here ? Why is i starting from 1?

drivers/usb/host/xhci-mem.c void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) · line 1005

Quoted 1 line
drivers/usb/host/xhci-mem.c +1 -0
+				vdev = xhci->devs[i];

slit_id 0 is reserved and xhci->devs[0] is not used, so ne need to check it.

Quoted 5 lines

hmm... it's reserved for the HW, sure. Do you need to over allocate the array by 1 just to keep this first member unused? Couldn't you handle the +1/-1 (depending on the case) in xhci driver itself? Saves a bit of memory there.

There are many things that needs fixing in this area, but not in this patch

-Mathias

#9 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

In reply to Mathias Nyman
Quoted 9 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

Quoted 7 lines

---

Guenter Roeck, does this work for you?

Sorry, I didn't have time this week. I'll test it first thing next week.

Guenter

Quoted 63 lines

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

---
 drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +36 -2
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 	xhci->devs[slot_id] = NULL;
 }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
+				vdev = xhci->devs[i];
+				if (vdev && (vdev->tt_info == tt_info))
+					xhci_free_virt_devices_depth_first(
+						xhci, i);
+			}
+		}
+	}
+	/* we are now at a leaf device */
+	xhci_free_virt_device(xhci, slot_id);
+}
+
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 		struct usb_device *udev, gfp_t flags)
 {
@@ -1829,8 +1863,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		}
 	}
 
-	for (i = 1; i < MAX_HC_SLOTS; ++i)
-		xhci_free_virt_device(xhci, i);
+	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
+		xhci_free_virt_devices_depth_first(xhci, i);
 
 	dma_pool_destroy(xhci->segment_pool);
 	xhci->segment_pool = NULL;
author Tested-by on v1 1/1
#10 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

In reply to Mathias Nyman
Quoted 9 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

Quoted 7 lines

---

Guenter Roeck, does this work for you?

Yes, it does.

Thanks,
Guenter

Quoted 65 lines

A rework of how tt_info is stored and used might be needed, but that will take some time and won't go to stable as easily.

---
 drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +36 -2
index 6afe323..b3a5cd8 100644
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 	xhci->devs[slot_id] = NULL;
 }
 
+/*
+ * Free a virt_device structure.
+ * If the virt_device added a tt_info (a hub) and has children pointing to
+ * that tt_info, then free the child first. Recursive.
+ * We can't rely on udev at this point to find child-parent relationships.
+ */
+void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
+{
+	struct xhci_virt_device *vdev;
+	struct list_head *tt_list_head;
+	struct xhci_tt_bw_info *tt_info, *next;
+	int i;
+
+	vdev = xhci->devs[slot_id];
+	if (!vdev)
+		return;
+
+	tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
+	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
+		/* is this a hub device that added a tt_info to the tts list */
+		if (tt_info->slot_id == slot_id) {
+			/* are any devices using this tt_info? */
+			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
+				vdev = xhci->devs[i];
+				if (vdev && (vdev->tt_info == tt_info))
+					xhci_free_virt_devices_depth_first(
+						xhci, i);
+			}
+		}
+	}
+	/* we are now at a leaf device */
+	xhci_free_virt_device(xhci, slot_id);
+}
+
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 		struct usb_device *udev, gfp_t flags)
 {
@@ -1829,8 +1863,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		}
 	}
 
-	for (i = 1; i < MAX_HC_SLOTS; ++i)
-		xhci_free_virt_device(xhci, i);
+	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
+		xhci_free_virt_devices_depth_first(xhci, i);
 
 	dma_pool_destroy(xhci->segment_pool);
 	xhci->segment_pool = NULL;
Signature · 1 line
1.9.1
#11 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 15 lines · Guenter Roeck
Quoted 14 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

---

Quoted 9 lines
Quoted 2 lines

Guenter Roeck, does this work for you?

Yes, it does.

Thanks,
Guenter

Thanks, I'll send it forward with proper Reported-by and Tested-by tags after 4.10-rc1

-Mathias

#12 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 20 lines · Mathias Nyman
Quoted 19 lines · Guenter Roeck
Quoted 9 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

Quoted 7 lines

---

Guenter Roeck, does this work for you?

Yes, it does.

Quoted 9 lines
Quoted 5 lines

Thanks,
Guenter

Thanks, I'll send it forward with proper Reported-by and Tested-by tags after 4.10-rc1

Do you have this patch sitting in some branch, by any chance ? I would like to pick it up if possible.

Thanks,
Guenter

#13 lore raw

Re: [RFT PATCH 1/1] xhci: free xhci virtual devices with leaf nodes first

Quoted 27 lines · Guenter Roeck
Quoted 26 lines · Mathias Nyman
Quoted 24 lines · Guenter Roeck
Quoted 9 lines · Mathias Nyman

the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.

This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.

Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.

Quoted 7 lines

---

Guenter Roeck, does this work for you?

Yes, it does.

Thanks,
Guenter

Quoted 6 lines
Quoted 3 lines

Thanks, I'll send it forward with proper Reported-by and Tested-by tags after 4.10-rc1

Do you have this patch sitting in some branch, by any chance ? I would like to pick it up if possible.

Now in

git://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git for-usb-linus

Branch is not very well tested yet

-Mathias