linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL
@ 2022-09-27 20:17 Abhishek Pandit-Subedi
  2022-09-27 21:10 ` [v3] " bluez.test.bot
  2022-09-28 21:50 ` [PATCH v3] " patchwork-bot+bluetooth
  0 siblings, 2 replies; 3+ messages in thread
From: Abhishek Pandit-Subedi @ 2022-09-27 20:17 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: Abhishek Pandit-Subedi, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Johan Hedberg, Luiz Augusto von Dentz,
	Marcel Holtmann, Paolo Abeni, linux-kernel, netdev

From: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>

Some drivers depend on shutdown being called for proper operation.
Unset HCI_USER_CHANNEL and call the full close routine since shutdown is
complementary to setup.

Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
---

Using hci_qca, we can get the controller into a bad state simply by
trying to bind to userchannel twice (open+bind+close, then open+bind).
Without running the shutdown routine, the device seems to get into a bad
state. A similar bug also occurs with btmtksdio (using MT7921).

This change properly runs the shutdown routine, which should be
complementary to setup. The reason it unsets the HCI_USER_CHANNEL flag
is that some drivers have complex operations in their shutdown routine
(including sending hci packets) and we need to support the normal data
path for them (including cmd_timeout + recovery mechanisms).

Note for v2: I've gotten a chance to test this on more devices
and figure out why it wasn't working before in v1. I found two problems:
I had a signal pending (SIGTERM) that was messing things up in the
socket release function and the HCI_USER_CHANNEL flag was preventing
hci_sync from operating properly during shutdown on Intel chipsets
(which use the sync functions to send a reset command + other commands
sometimes).

This was tested with hci_qca (QCA6174-A-3), btmtksdio (MT7921-SDIO)
and btusb (with AX200).


Changes in v3:
- Added hci_dev_shutdown to wrap disabling and re-enabling flag.

Changes in v2:
- Clear HCI_USER_CHANNEL flag at start of close and restore at end.
- Add comment explaning why we need to clear flag and run shutdown.

 net/bluetooth/hci_sync.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 422f7c6911d9..15c75ef4c271 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -4727,6 +4727,31 @@ static void hci_pend_le_actions_clear(struct hci_dev *hdev)
 	BT_DBG("All LE pending actions cleared");
 }
 
+static int hci_dev_shutdown(struct hci_dev *hdev)
+{
+	int err = 0;
+	/* Similar to how we first do setup and then set the exclusive access
+	 * bit for userspace, we must first unset userchannel and then clean up.
+	 * Otherwise, the kernel can't properly use the hci channel to clean up
+	 * the controller (some shutdown routines require sending additional
+	 * commands to the controller for example).
+	 */
+	bool was_userchannel =
+		hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
+
+	if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
+	    test_bit(HCI_UP, &hdev->flags)) {
+		/* Execute vendor specific shutdown routine */
+		if (hdev->shutdown)
+			err = hdev->shutdown(hdev);
+	}
+
+	if (was_userchannel)
+		hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
+
+	return err;
+}
+
 int hci_dev_close_sync(struct hci_dev *hdev)
 {
 	bool auto_off;
@@ -4746,13 +4771,7 @@ int hci_dev_close_sync(struct hci_dev *hdev)
 		hdev->adv_instance_timeout = 0;
 	}
 
-	if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
-	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
-	    test_bit(HCI_UP, &hdev->flags)) {
-		/* Execute vendor specific shutdown routine */
-		if (hdev->shutdown)
-			err = hdev->shutdown(hdev);
-	}
+	err = hci_dev_shutdown(hdev);
 
 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
 		cancel_delayed_work_sync(&hdev->cmd_timer);
-- 
2.38.0.rc1.362.ged0d419d3c-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL
  2022-09-27 20:17 [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL Abhishek Pandit-Subedi
@ 2022-09-27 21:10 ` bluez.test.bot
  2022-09-28 21:50 ` [PATCH v3] " patchwork-bot+bluetooth
  1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2022-09-27 21:10 UTC (permalink / raw)
  To: linux-bluetooth, abhishekpandit

[-- Attachment #1: Type: text/plain, Size: 1204 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=681203

---Test result---

Test Summary:
CheckPatch                    PASS      1.51 seconds
GitLint                       PASS      0.80 seconds
SubjectPrefix                 PASS      0.65 seconds
BuildKernel                   PASS      33.70 seconds
BuildKernel32                 PASS      29.38 seconds
Incremental Build with patchesPASS      42.26 seconds
TestRunner: Setup             PASS      498.42 seconds
TestRunner: l2cap-tester      PASS      16.50 seconds
TestRunner: iso-tester        PASS      15.38 seconds
TestRunner: bnep-tester       PASS      6.09 seconds
TestRunner: mgmt-tester       PASS      99.33 seconds
TestRunner: rfcomm-tester     PASS      9.83 seconds
TestRunner: sco-tester        PASS      9.19 seconds
TestRunner: ioctl-tester      PASS      10.31 seconds
TestRunner: smp-tester        PASS      9.30 seconds
TestRunner: userchan-tester   PASS      6.28 seconds



---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL
  2022-09-27 20:17 [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL Abhishek Pandit-Subedi
  2022-09-27 21:10 ` [v3] " bluez.test.bot
@ 2022-09-28 21:50 ` patchwork-bot+bluetooth
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+bluetooth @ 2022-09-28 21:50 UTC (permalink / raw)
  To: Abhishek Pandit-Subedi
  Cc: linux-bluetooth, abhishekpandit, davem, edumazet, kuba,
	johan.hedberg, luiz.dentz, marcel, pabeni, linux-kernel, netdev

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:

On Tue, 27 Sep 2022 13:17:20 -0700 you wrote:
> From: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> 
> Some drivers depend on shutdown being called for proper operation.
> Unset HCI_USER_CHANNEL and call the full close routine since shutdown is
> complementary to setup.
> 
> Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
> 
> [...]

Here is the summary with links:
  - [v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL
    https://git.kernel.org/bluetooth/bluetooth-next/c/8dbc3e75a0a5

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-28 21:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-27 20:17 [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL Abhishek Pandit-Subedi
2022-09-27 21:10 ` [v3] " bluez.test.bot
2022-09-28 21:50 ` [PATCH v3] " patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).