Skip to contentSkip to conversation

usb:xhci fix panic in xhci_free_virt_devices_depth_first

PATCH Superseded Yu Chen 4 participants 8 messages Nov 6, 2017 – Nov 7, 2017

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yu Chen

author
#0 lore raw

PATCH v2 1/1 usb:xhci fix panic in xhci_free_virt_devices_depth_first

From: Yu Chen <[email protected]>

Check vdev->real_port 0 to avoid panic

[    9.261347] [<ffffff800884a390>] xhci_free_virt_devices_depth_first+0x58/0x108
[    9.261352] [<ffffff800884a814>] xhci_mem_cleanup+0x1bc/0x570
[    9.261355] [<ffffff8008842de8>] xhci_stop+0x140/0x1c8
[    9.261365] [<ffffff80087ed304>] usb_remove_hcd+0xfc/0x1d0
[    9.261369] [<ffffff80088551c4>] xhci_plat_remove+0x6c/0xa8
[    9.261377] [<ffffff80086e928c>] platform_drv_remove+0x2c/0x70
[    9.261384] [<ffffff80086e6ea0>] __device_release_driver+0x80/0x108
[    9.261387] [<ffffff80086e7a1c>] device_release_driver+0x2c/0x40
[    9.261392] [<ffffff80086e5f28>] bus_remove_device+0xe0/0x120
[    9.261396] [<ffffff80086e2e34>] device_del+0x114/0x210
[    9.261399] [<ffffff80086e9e00>] platform_device_del+0x30/0xa0
[    9.261403] [<ffffff8008810bdc>] dwc3_otg_work+0x204/0x488
[    9.261407] [<ffffff80088133fc>] event_work+0x304/0x5b8
[    9.261414] [<ffffff80080e31b0>] process_one_work+0x148/0x490
[    9.261417] [<ffffff80080e3548>] worker_thread+0x50/0x4a0
[    9.261421] [<ffffff80080e9ea0>] kthread+0xe8/0x100
[    9.261427] [<ffffff8008083680>] ret_from_fork+0x10/0x50

The problem can occur if xhci_plat_remove() is called shortly after xhci_plat_probe(). While xhci_free_virt_devices_depth_first been called before the device has been setup and get real_port initialized. The problem occurred on Hikey960 and was reproduced by Guenter Roeck on Kevin with chromeos-4.4.

---
 drivers/usb/host/xhci-mem.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/host/xhci-mem.c +4 -0
index 2a82c927ded2..0361b4a58f59 100644
@@ -947,6 +947,9 @@ void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
 	if (!vdev)
 		return;
 
+	if (WARN_ON(!vdev->real_port))
+		goto out;
+
 	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 */
@@ -960,6 +963,7 @@ void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
 			}
 		}
 	}
+out:
 	/* we are now at a leaf device */
 	xhci_free_virt_device(xhci, slot_id);
 }
Signature · 1 line
2.15.0-rc2
maintainer
#1 lore raw
Quoted 42 lines · Yu Chen

From: Yu Chen <[email protected]>

Check vdev->real_port 0 to avoid panic

[    9.261347] [<ffffff800884a390>] xhci_free_virt_devices_depth_first+0x58/0x108
[    9.261352] [<ffffff800884a814>] xhci_mem_cleanup+0x1bc/0x570
[    9.261355] [<ffffff8008842de8>] xhci_stop+0x140/0x1c8
[    9.261365] [<ffffff80087ed304>] usb_remove_hcd+0xfc/0x1d0
[    9.261369] [<ffffff80088551c4>] xhci_plat_remove+0x6c/0xa8
[    9.261377] [<ffffff80086e928c>] platform_drv_remove+0x2c/0x70
[    9.261384] [<ffffff80086e6ea0>] __device_release_driver+0x80/0x108
[    9.261387] [<ffffff80086e7a1c>] device_release_driver+0x2c/0x40
[    9.261392] [<ffffff80086e5f28>] bus_remove_device+0xe0/0x120
[    9.261396] [<ffffff80086e2e34>] device_del+0x114/0x210
[    9.261399] [<ffffff80086e9e00>] platform_device_del+0x30/0xa0
[    9.261403] [<ffffff8008810bdc>] dwc3_otg_work+0x204/0x488
[    9.261407] [<ffffff80088133fc>] event_work+0x304/0x5b8
[    9.261414] [<ffffff80080e31b0>] process_one_work+0x148/0x490
[    9.261417] [<ffffff80080e3548>] worker_thread+0x50/0x4a0
[    9.261421] [<ffffff80080e9ea0>] kthread+0xe8/0x100
[    9.261427] [<ffffff8008083680>] ret_from_fork+0x10/0x50

The problem can occur if xhci_plat_remove() is called shortly after xhci_plat_probe(). While xhci_free_virt_devices_depth_first been called before the device has been setup and get real_port initialized. The problem occurred on Hikey960 and was reproduced by Guenter Roeck on Kevin with chromeos-4.4.

---
 drivers/usb/host/xhci-mem.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/host/xhci-mem.c +0 -0
index 2a82c927ded2..0361b4a58f59 100644

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

Quoted 4 lines
drivers/usb/host/xhci-mem.c +1 -0
@@ -947,6 +947,9 @@ void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
 	if (!vdev)
 		return;
 
+	if (WARN_ON(!vdev->real_port))

Ok, now you are sending a lot of mess to the kernel log, so what can a user do about it?

How can this ever happen? Is it a hardware error, or a kernel driver logic error?

thanks,

greg k-h

author
#2 lore raw

Hi,

Quoted 51 lines · Greg KH
Quoted 42 lines · Yu Chen

From: Yu Chen <[email protected]>

Check vdev->real_port 0 to avoid panic

[    9.261347] [<ffffff800884a390>] xhci_free_virt_devices_depth_first+0x58/0x108
[    9.261352] [<ffffff800884a814>] xhci_mem_cleanup+0x1bc/0x570
[    9.261355] [<ffffff8008842de8>] xhci_stop+0x140/0x1c8
[    9.261365] [<ffffff80087ed304>] usb_remove_hcd+0xfc/0x1d0
[    9.261369] [<ffffff80088551c4>] xhci_plat_remove+0x6c/0xa8
[    9.261377] [<ffffff80086e928c>] platform_drv_remove+0x2c/0x70
[    9.261384] [<ffffff80086e6ea0>] __device_release_driver+0x80/0x108
[    9.261387] [<ffffff80086e7a1c>] device_release_driver+0x2c/0x40
[    9.261392] [<ffffff80086e5f28>] bus_remove_device+0xe0/0x120
[    9.261396] [<ffffff80086e2e34>] device_del+0x114/0x210
[    9.261399] [<ffffff80086e9e00>] platform_device_del+0x30/0xa0
[    9.261403] [<ffffff8008810bdc>] dwc3_otg_work+0x204/0x488
[    9.261407] [<ffffff80088133fc>] event_work+0x304/0x5b8
[    9.261414] [<ffffff80080e31b0>] process_one_work+0x148/0x490
[    9.261417] [<ffffff80080e3548>] worker_thread+0x50/0x4a0
[    9.261421] [<ffffff80080e9ea0>] kthread+0xe8/0x100
[    9.261427] [<ffffff8008083680>] ret_from_fork+0x10/0x50

The problem can occur if xhci_plat_remove() is called shortly after xhci_plat_probe(). While xhci_free_virt_devices_depth_first been called before the device has been setup and get real_port initialized. The problem occurred on Hikey960 and was reproduced by Guenter Roeck on Kevin with chromeos-4.4.

---
 drivers/usb/host/xhci-mem.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/host/xhci-mem.c +0 -0
index 2a82c927ded2..0361b4a58f59 100644

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

Quoted 4 lines
drivers/usb/host/xhci-mem.c +1 -0
@@ -947,6 +947,9 @@ void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
 	if (!vdev)
 		return;
 
+	if (WARN_ON(!vdev->real_port))

Ok, now you are sending a lot of mess to the kernel log, so what can a user do about it?

Quoted 9 lines

How can this ever happen? Is it a hardware error, or a kernel driver logic error?

thanks,

greg k-h

.

The problem is a driver logic error, it can reproduced if xhci_plat_remove() is called shortly after xhci_plat_probe() while xhci_alloc_virt_device has been called but real_port has not been initialized in xhci_setup_addressable_virt_dev. A simple process is as below:

	xhci_plat_probe()
               |
	usb_add_hcd()					xhci_plat_remove()
	       |						|
	find some device				usb_remove_hcd()
	       |						|
	hub_port_connect() -> usb_alloc_dev()		usb_disconnect()
	       |						|
	before hub_enable_device()			xhci_stop()
								|
							xhci_mem_cleanup()
								|
							xhci_free_virt_devices_depth_first()
								|
							real_port is 0 access xhci->rh_bw[vdev->real_port-1]

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

maintainer
#3 lore raw
Quoted 59 lines · Chen Yu

Hi,

Quoted 56 lines · Greg KH
Quoted 42 lines · Yu Chen

From: Yu Chen <[email protected]>

Check vdev->real_port 0 to avoid panic

[    9.261347] [<ffffff800884a390>] xhci_free_virt_devices_depth_first+0x58/0x108
[    9.261352] [<ffffff800884a814>] xhci_mem_cleanup+0x1bc/0x570
[    9.261355] [<ffffff8008842de8>] xhci_stop+0x140/0x1c8
[    9.261365] [<ffffff80087ed304>] usb_remove_hcd+0xfc/0x1d0
[    9.261369] [<ffffff80088551c4>] xhci_plat_remove+0x6c/0xa8
[    9.261377] [<ffffff80086e928c>] platform_drv_remove+0x2c/0x70
[    9.261384] [<ffffff80086e6ea0>] __device_release_driver+0x80/0x108
[    9.261387] [<ffffff80086e7a1c>] device_release_driver+0x2c/0x40
[    9.261392] [<ffffff80086e5f28>] bus_remove_device+0xe0/0x120
[    9.261396] [<ffffff80086e2e34>] device_del+0x114/0x210
[    9.261399] [<ffffff80086e9e00>] platform_device_del+0x30/0xa0
[    9.261403] [<ffffff8008810bdc>] dwc3_otg_work+0x204/0x488
[    9.261407] [<ffffff80088133fc>] event_work+0x304/0x5b8
[    9.261414] [<ffffff80080e31b0>] process_one_work+0x148/0x490
[    9.261417] [<ffffff80080e3548>] worker_thread+0x50/0x4a0
[    9.261421] [<ffffff80080e9ea0>] kthread+0xe8/0x100
[    9.261427] [<ffffff8008083680>] ret_from_fork+0x10/0x50

The problem can occur if xhci_plat_remove() is called shortly after xhci_plat_probe(). While xhci_free_virt_devices_depth_first been called before the device has been setup and get real_port initialized. The problem occurred on Hikey960 and was reproduced by Guenter Roeck on Kevin with chromeos-4.4.

---
 drivers/usb/host/xhci-mem.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/host/xhci-mem.c +0 -0
index 2a82c927ded2..0361b4a58f59 100644

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

Quoted 4 lines
drivers/usb/host/xhci-mem.c +1 -0
@@ -947,6 +947,9 @@ void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
 	if (!vdev)
 		return;
 
+	if (WARN_ON(!vdev->real_port))

Ok, now you are sending a lot of mess to the kernel log, so what can a user do about it?

How can this ever happen? Is it a hardware error, or a kernel driver logic error?

thanks,

Quoted 8 lines
Quoted 4 lines

greg k-h

.

The problem is a driver logic error, it can reproduced if xhci_plat_remove() is called shortly after xhci_plat_probe() while xhci_alloc_virt_device has been called but real_port has not been initialized in xhci_setup_addressable_virt_dev.

Who is calling xhci_plat_remove() like this?

Quoted 17 lines

A simple process is as below:

	xhci_plat_probe()
               |
	usb_add_hcd()					xhci_plat_remove()
	       |						|
	find some device				usb_remove_hcd()
	       |						|
	hub_port_connect() -> usb_alloc_dev()		usb_disconnect()
	       |						|
	before hub_enable_device()			xhci_stop()
								|
							xhci_mem_cleanup()
								|
							xhci_free_virt_devices_depth_first()
								|
							real_port is 0 access xhci->rh_bw[vdev->real_port-1]
Quoted 2 lines

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

Then the bug needs to be fixed, throwing a huge kernel trace message into the kernel log is not "fixing" the problem at all, right?

thanks,

greg k-h

author
#4 lore raw
Quoted 20 lines · Greg KH
Quoted 17 lines

A simple process is as below:

	xhci_plat_probe()
               |
	usb_add_hcd()					xhci_plat_remove()
	       |						|
	find some device				usb_remove_hcd()
	       |						|
	hub_port_connect() -> usb_alloc_dev()		usb_disconnect()
	       |						|
	before hub_enable_device()			xhci_stop()
								|
							xhci_mem_cleanup()
								|
							xhci_free_virt_devices_depth_first()
								|
							real_port is 0 access xhci->rh_bw[vdev->real_port-1]
Quoted 2 lines

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

Quoted 9 lines

Then the bug needs to be fixed, throwing a huge kernel trace message into the kernel log is not "fixing" the problem at all, right?

thanks,

greg k-h

.

You are right, the way that xhci_plat_remove() to be called needs to be fixed. But there is still possibility for this crash.
What do you think if just add an "xhci_warn" instead of "WARN_ON"?
+ if (!vdev->real_port) {
+ xhci_warn(xhci, "Bad vdev->real_port\n");
+ goto out;
+ }
+

Best regards
Yu Chen

#5 lore raw
Quoted 37 lines · Chen Yu
Quoted 20 lines · Greg KH
Quoted 17 lines

A simple process is as below:

	xhci_plat_probe()
                |
	usb_add_hcd()					xhci_plat_remove()
	       |						|
	find some device				usb_remove_hcd()
	       |						|
	hub_port_connect() -> usb_alloc_dev()		usb_disconnect()
	       |						|
	before hub_enable_device()			xhci_stop()
								|
							xhci_mem_cleanup()
								|
							xhci_free_virt_devices_depth_first()
								|
							real_port is 0 access xhci->rh_bw[vdev->real_port-1]
Quoted 2 lines

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

Quoted 9 lines

Then the bug needs to be fixed, throwing a huge kernel trace message into the kernel log is not "fixing" the problem at all, right?

thanks,

greg k-h

.

You are right, the way that xhci_plat_remove() to be called needs to be fixed. But there is still possibility for this crash.
What do you think if just add an "xhci_warn" instead of "WARN_ON"?

Review

Quoted 5 lines
(quoted diff) +5 -0
+       if (!vdev->real_port) {
+               xhci_warn(xhci, "Bad vdev->real_port\n");
+               goto out;
+       }
+

This patch solves the issue, just drop all the error messages.

vdev->real_port is not set until the the device enable/address stage, and we know it won't have any children yet then, so no need to worry about a child having tt pointers to this device.

The "goto out" to xhci_free_virt_device() you do is fine here.

xhci_plat_remove() is the .remove callback for the xhci platform driver. It might get called before a device is properly enabled/addressed. Not really a error. A unlikely but possible situation.

xhci_free_tt_info() already has a similar check

Thanks
-Mathias

#6 lore raw
Quoted 58 lines · Mathias Nyman
Quoted 45 lines · Chen Yu
Quoted 26 lines · Greg KH
Quoted 17 lines

A simple process is as below:

        xhci_plat_probe()
                |
        usb_add_hcd()
xhci_plat_remove()
               |                                                |
        find some device                                usb_remove_hcd()
               |                                                |
        hub_port_connect() -> usb_alloc_dev()           usb_disconnect()
               |                                                |
        before hub_enable_device()                      xhci_stop()
                                                                |

xhci_mem_cleanup()

                                                                |
Quoted 7 lines

xhci_free_virt_devices_depth_first()
|
real_port is 0
access xhci->rh_bw[vdev->real_port-1]

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

Quoted 9 lines

Then the bug needs to be fixed, throwing a huge kernel trace message into the kernel log is not "fixing" the problem at all, right?

thanks,

greg k-h

.

You are right, the way that xhci_plat_remove() to be called needs to be fixed.
But there is still possibility for this crash.
What do you think if just add an "xhci_warn" instead of "WARN_ON"?

Review

Quoted 5 lines
(quoted diff) +5 -0
+       if (!vdev->real_port) {
+               xhci_warn(xhci, "Bad vdev->real_port\n");
+               goto out;
+       }
+

This patch solves the issue, just drop all the error messages.

vdev->real_port is not set until the the device enable/address stage, and we know it won't have any children yet then, so no need to worry about a child having tt pointers to this device.

Quoted 6 lines

The "goto out" to xhci_free_virt_device() you do is fine here.

xhci_plat_remove() is the .remove callback for the xhci platform driver. It might get called before a device is properly enabled/addressed. Not really a error. A unlikely but possible situation.

Agreed. I can reproduce the problem with a well timed bind/unbind loop.

Guenter

Quoted 5 lines

xhci_free_tt_info() already has a similar check

Thanks
-Mathias

author
#7 lore raw
In reply to Mathias Nyman
Quoted 50 lines · Mathias Nyman
Quoted 37 lines · Chen Yu
Quoted 20 lines · Greg KH
Quoted 17 lines

A simple process is as below:

    xhci_plat_probe()
                |
    usb_add_hcd()                    xhci_plat_remove()
           |                        |
    find some device                usb_remove_hcd()
           |                        |
    hub_port_connect() -> usb_alloc_dev()        usb_disconnect()
           |                        |
    before hub_enable_device()            xhci_stop()
                                |
                            xhci_mem_cleanup()
                                |
                            xhci_free_virt_devices_depth_first()
                                |
                            real_port is 0 access xhci->rh_bw[vdev->real_port-1]
Quoted 2 lines

The problem came from https://bugs.96boards.org/show_bug.cgi?id=535
Also look at crbug.com/700041

Quoted 9 lines

Then the bug needs to be fixed, throwing a huge kernel trace message into the kernel log is not "fixing" the problem at all, right?

thanks,

greg k-h

.

You are right, the way that xhci_plat_remove() to be called needs to be fixed. But there is still possibility for this crash.
What do you think if just add an "xhci_warn" instead of "WARN_ON"?

Review

Quoted 5 lines
(quoted diff) +5 -0
+       if (!vdev->real_port) {
+               xhci_warn(xhci, "Bad vdev->real_port\n");
+               goto out;
+       }
+

This patch solves the issue, just drop all the error messages.

vdev->real_port is not set until the the device enable/address stage, and we know it won't have any children yet then, so no need to worry about a child having tt pointers to this device.

Quoted 7 lines

The "goto out" to xhci_free_virt_device() you do is fine here.

xhci_plat_remove() is the .remove callback for the xhci platform driver. It might get called before a device is properly enabled/addressed. Not really a error. A unlikely but possible situation.

xhci_free_tt_info() already has a similar check

Thanks a lot for your analysis and suggestion!
The check within xhci_free_tt_info() is a better one. I will upload a new patch like the check in xhci_free_tt_info().

Quoted 7 lines

Thanks
-Mathias

.