Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
From: Samuel Holland <samuel@sholland.org>
To: Marcel Holtmann <marcel@holtmann.org>,
	Johan Hedberg <johan.hedberg@gmail.com>
Cc: Hans de Goede <hdegoede@redhat.com>,
	linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	Samuel Holland <samuel@sholland.org>,
	stable@vger.kernel.org
Subject: [PATCH 2/2] Bluetooth: hci_serdev: Fix crash with HCI_UART_INIT_PENDING
Date: Sat,  1 Aug 2020 10:43:46 -0500
Message-ID: <20200801154346.63882-2-samuel@sholland.org> (raw)
In-Reply-To: <20200801154346.63882-1-samuel@sholland.org>

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@vger.kernel.org
Fixes: fdee6d8fc630 ("Bluetooth: hci_serdev: Fix HCI_UART_INIT_PENDING not working")
Signed-off-by: Samuel Holland <samuel@sholland.org>
---
 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


  reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-01 15:43 [PATCH 1/2] Bluetooth: hci_h5: Stop erroneously setting HCI_UART_REGISTERED Samuel Holland
2020-08-01 15:43 ` Samuel Holland [this message]
2020-08-01 16:24 ` Samuel Holland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200801154346.63882-2-samuel@sholland.org \
    --to=samuel@sholland.org \
    --cc=hdegoede@redhat.com \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcel@holtmann.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git