Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1459426rwb; Tue, 27 Sep 2022 13:27:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM57tc9V4rP5ct2S+o0jkSlxisWVtyBwq/W1/GYHv++eKkZ22CBv8y1hHYlU/irGsgr52lHa X-Received: by 2002:a17:906:eec1:b0:782:6384:76be with SMTP id wu1-20020a170906eec100b00782638476bemr23587040ejb.756.1664310468371; Tue, 27 Sep 2022 13:27:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664310468; cv=none; d=google.com; s=arc-20160816; b=fDYFVUhrwyqd0TZRLjTBhMfCxFUcG2dAZdOPKWmDs99Wjx+MCMgM1bKUoHcX5Ms27H rQc7JsPbax9s7hcB4wrRuXRgPZIHCme8kWDeRHG9sQoS6Qzdzd1O6g9BhzvbQ5X1QvQ3 mhv6vv6uFtcn+ftaBJXiNi4zz6dmQuRYM6ZPKGdTNarlRjKASZRsQaCBaJN6k4oHPR/j aO9vzlOWyct6o3WM4lKNYBChErfNG1dj5zm2lMESDKt7Fe4bp+tjEd8s6FB04jvC9S1J 6kz+7rrRkEq7x3tr80OtaJBNex/4L1GGusBPeaJ2uKCJPkjNqJFX4QlMCGXi80IcDuhZ nB8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=F72lE+m5jUyXJTZtAHspzg4fSLtIEdJfsL6DV7/dUu4=; b=FbviGeyKd6prYAXsC4fg30iR0oLzXiSKX/AycNJnln27ok0rNjb2ybCkGtMLxSBm/I oyIam0k1qGx59scUAFqgbS/1klCVOAQ8d+SynPleDzVHhzdhRndMXsryzjAJvqNohOwW gw5TN1nZIgnUSRMrNmsEiRz15jo02YSTOnMe2Qkyp42Y3RmFuDk2/5znMF9jaTETGEIV iPy2EynaU4gXTkmUKp0m2uHgwgOHLHnVcscKVQfNeRZE2XjKRHx/i2al13PSYKx8N7XG qB4VAQOMlNDCZ33ohX3AIzLFplrpXGZ7JlkkTKXkm/mSdpVTxV/pRmeKkiAk1oKdyT47 R36A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Mn8MbU84; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h11-20020a056402280b00b0044f8511f3e5si2644399ede.582.2022.09.27.13.27.10; Tue, 27 Sep 2022 13:27:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Mn8MbU84; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232949AbiI0USe (ORCPT + 99 others); Tue, 27 Sep 2022 16:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232943AbiI0USB (ORCPT ); Tue, 27 Sep 2022 16:18:01 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D745819286 for ; Tue, 27 Sep 2022 13:17:23 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id x23-20020a634857000000b0043c700f6441so6165951pgk.21 for ; Tue, 27 Sep 2022 13:17:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date; bh=F72lE+m5jUyXJTZtAHspzg4fSLtIEdJfsL6DV7/dUu4=; b=Mn8MbU84gVPCY9rdXSLA84U8EL0aqabNS24qrZWJ2AnahzfD2GFj4FE4Z0n05/O/Zt V/aIs40HUCDBjONjB/LyF+BOhm2UoyobiRZGpXu8lr1esxucb3JZeu+8OKc2pVc2hh+x UbnmqFSEJ68jJdDpoYY6FLvV3Seo1t7Gv9OPvEIWZJuj7+TsSj0WhD3vvSxJxaBFJcr0 ld8bUZgzL82VhOu3rnVqg118AzBJEQHny195aliXzFCnm2SSzbP2u1ixIV6zu1pdNmo5 8zrLlDxN1AZsVn+LXYQQ7GgkBbtgIuNb5lkEruA8D7OJ2ywQrsl3f6gNazJf9QttXIYO gHbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date; bh=F72lE+m5jUyXJTZtAHspzg4fSLtIEdJfsL6DV7/dUu4=; b=D0hoDPMDUDgOlTFaWaLzOUM7Bzfsp090tRxTwTkV+Q9OvuvpNNxOm9dAMX3TT+Q2n1 Lg7PTMGQp/THnUrd6Z9KF9Udr9Fucto5SjSruCdYqLfEtjvBcH60XbtOESUC24GVnZ2N h+5sOvvgymgfKnxKHxuhbTwJ2zuN43yROaFQKiEa71iZujB3Oj6Ujbh9jJeWVcfKnq6/ /O4WF5hnFJjlea7u7LWMfMGXkbNvpQLpw/35yzX6NDBKuurmvt/RzVoRYIc7u4Fewy7F HNS7ylulcuAfZyqAno/Y5uD38SmXxrhXdloY/ur9u0oAaHvsEWk/OBPXsMkwYeBQI89n rrSw== X-Gm-Message-State: ACrzQf230O58RLHO0vHwyOaXnT1lv0+k74BMcsTrRAaBawr0onquqx+F LoxNmvy4pbLqFvKL8X0u67ZL8LqFScsqHzs5dAMD1XleexwLcia/ufecPVBE5bbNa5KhNfmzZu3 Xo9XEdZj9PDSWhv1LcuMZ2MDTDaj8Wf7XnFrDNczc4sHC8Vo82aKystiopWkizZOghqjJxW2IKo UavAw4JZGdVSsGvc/FM/8= X-Received: from abps.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:9b4]) (user=abhishekpandit job=sendgmr) by 2002:aa7:9e0d:0:b0:540:94a7:9051 with SMTP id y13-20020aa79e0d000000b0054094a79051mr31047543pfq.59.1664309843219; Tue, 27 Sep 2022 13:17:23 -0700 (PDT) Date: Tue, 27 Sep 2022 13:17:20 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20220927131717.v3.1.Ic8eabc8ed89a07c3d52726dd017539069faac6c4@changeid> Subject: [PATCH v3] Bluetooth: Call shutdown for HCI_USER_CHANNEL From: Abhishek Pandit-Subedi To: linux-bluetooth@vger.kernel.org Cc: Abhishek Pandit-Subedi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Johan Hedberg , Luiz Augusto von Dentz , Marcel Holtmann , Paolo Abeni , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Abhishek Pandit-Subedi 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 --- 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