[PATCH 2/2] Bluetooth: hci_serdev: Fix crash with HCI_UART_INIT_PENDING

From: Samuel Holland
Date: Sat Aug 01 2020 - 11:43:58 EST


When using HCI_UART_INIT_PENDING, hci_register_dev is not called from
hci_uart_register_device. Instead, it is called by hci_uart_init_work,
which is enqueued by hci_uart_init_ready (as hu->init_ready). In the
case of the hci_h5 proto, hci_uart_init_ready is called only after
handshaking with the hardware. If that handshake never completes,
hci_register_dev is never called. When later unregistering the device,
the kernel crashes:

Unable to handle kernel write to read-only memory at virtual address 0000000000000008
Internal error: Oops: 96000045 [#1] PREEMPT SMP
Modules linked in: hci_uart(-) btrtl bnep bluetooth 8723cs(C) [...]
CPU: 3 PID: 516 Comm: rmmod Tainted: G C 5.8.0-rc7-00277-gd87641cb644c-dirty #1
Hardware name: Pine64 PinePhone Braveheart (1.1) (DT)
pstate: 80400005 (Nzcv daif +PAN -UAO BTYPE=--)
pc : hci_unregister_dev+0x58/0x4b0 [bluetooth]
lr : hci_unregister_dev+0x54/0x4b0 [bluetooth]
sp : ffffffc011b5bc60
x29: ffffffc011b5bc60 x28: ffffff8075d23800
x27: 0000000000000000 x26: 0000000000000000
x25: 0000000000000000 x24: 0000000000000000
x23: 0000000000000000 x22: ffffffc008d86000
x21: ffffff8079dd2000 x20: ffffff8078026000
x19: ffffffc008f4f6d0 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000000
x15: 0000000000000004 x14: ffffff807b15c110
x13: 0000000000000000 x12: ffffff80778e6720
x11: ffffff80778e66c8 x10: ffffff807ac6ada2
x9 : ffffffc010c95864 x8 : 0000000000210d00
x7 : 0000000000000000 x6 : 0000000000000001
x5 : 0000000000000001 x4 : 0000000000000001
x3 : 0000000000000000 x2 : 0000000000000000
x1 : 0000000000000000 x0 : 0000000000000000
Call trace:
hci_unregister_dev+0x58/0x4b0 [bluetooth]
hci_uart_unregister_device+0x40/0x74 [hci_uart]
h5_serdev_remove+0x18/0x20 [hci_uart]
serdev_drv_remove+0x28/0x4c
__device_release_driver+0x174/0x210
driver_detach+0xc4/0x100
bus_remove_driver+0x5c/0xb0
driver_unregister+0x34/0x60
h5_deinit+0x14/0xd10 [hci_uart]
hci_uart_exit+0xc/0x30 [hci_uart]
__arm64_sys_delete_module+0x1b0/0x260
el0_svc_common.constprop.0+0x60/0x100
do_el0_svc+0x20/0x30
el0_sync_handler+0x88/0x1b4
el0_sync+0x138/0x140
Code: aa1303e0 79408297 95f69770 a9400682 (f9000441)
---[ end trace b68f9044c8f92379 ]---

Fix the crash by flushing the hu->init_ready work to ensure
hci_register_dev is not being called concurrently, and then only
calling hci_unregister_dev if the HCI was previously registered.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: fdee6d8fc630 ("Bluetooth: hci_serdev: Fix HCI_UART_INIT_PENDING not working")
Signed-off-by: Samuel Holland <samuel@xxxxxxxxxxxx>
---
drivers/bluetooth/hci_serdev.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index 599855e4c57c..3977bba485c2 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -355,7 +355,10 @@ void hci_uart_unregister_device(struct hci_uart *hu)
struct hci_dev *hdev = hu->hdev;

clear_bit(HCI_UART_PROTO_READY, &hu->flags);
- hci_unregister_dev(hdev);
+
+ cancel_work_sync(&hu->init_ready);
+ if (test_bit(HCI_UART_REGISTERED, &hu->flags))
+ hci_unregister_dev(hdev);
hci_free_dev(hdev);

cancel_work_sync(&hu->write_work);
--
2.26.2