Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Deepin-Kernel-SIG] [Upstream] [linux 6.6-y] Sync some btmtk bugfix from mainline #606

Open
wants to merge 5 commits into
base: linux-6.6.y
Choose a base branch
from

Conversation

opsiff
Copy link
Member

@opsiff opsiff commented Feb 17, 2025

Fixes:
commit 1d734a2: ("Bluetooth: btusb: mediatek: add ISO data transmission functions")

Commit:
Bluetooth: btusb: mediatek: Add locks for usb_driver_claim_interface()
Bluetooth: btmtk: adjust the position to init iso data anchor
Bluetooth: btusb: mediatek: change the conditions for ISO interface
Bluetooth: btusb: mediatek: add intf release flow when usb disconnect
Bluetooth: btusb: mediatek: move Bluetooth power off command position

Summary by Sourcery

Fix several bugs related to the MediaTek Bluetooth USB driver, including a potential deadlock, incorrect interface handling, and ISO data transmission issues. Introduce a disconnect handler to release the ISO interface upon disconnection. Adjust the initialization timing of the ISO data anchor and the Bluetooth power off command.

Bug Fixes:

  • Fix potential deadlock by adding locks for usb_driver_claim_interface() when called outside of probe routines.
  • Fix ISO interface claim condition and add an interface release flow during USB disconnect.
  • Fix incorrect positioning of the Bluetooth power off command.
  • Fix the initialization timing of the ISO data anchor by moving it after the interface claim to avoid potential issues.
  • Fix a bug where ISO packet transmission could fail due to incorrect interface conditions by changing the conditions for ISO interface

ChrisCH-Lu and others added 5 commits February 17, 2025 23:52
mainline inclusion
from mainline-v6.13-rc1
category: bugfix

Move MediaTek Bluetooth power off command before releasing
usb ISO interface.

Signed-off-by: Chris Lu <chris.lu@mediatek.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit ad0c6f603bb0b07846fda484c59a176a8cd02838)
mainline inclusion
from mainline-v6.13-rc1
category: bugfix

MediaTek claim an special usb intr interface for ISO data transmission.
The interface need to be released before unregistering hci device when
usb disconnect. Removing BT usb dongle without properly releasing the
interface may cause Kernel panic while unregister hci device.

Signed-off-by: Chris Lu <chris.lu@mediatek.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit 489304e67087abddc2666c5af0159cb95afdcf59)
mainline inclusion
from mainline-v6.13-rc1
category: bugfix

Change conditions for Bluetooth driver claiming and releasing usb
ISO interface for MediaTek ISO data transmission.

Signed-off-by: Chris Lu <chris.lu@mediatek.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit defc33b5541e0a7e45cc2d99d72fbe80a597afc5)
mainline inclusion
from mainline-v6.13-rc1
category: bugfix

MediaTek iso data anchor init should be moved to where MediaTek
claims iso data interface.
If there is an unexpected BT usb disconnect during setup flow,
it will cause a NULL pointer crash issue when releasing iso
anchor since the anchor wasn't been init yet. Adjust the position
to do iso data anchor init.

[   17.137991] pc : usb_kill_anchored_urbs+0x60/0x168
[   17.137998] lr : usb_kill_anchored_urbs+0x44/0x168
[   17.137999] sp : ffffffc0890cb5f0
[   17.138000] x29: ffffffc0890cb5f0 x28: ffffff80bb6c2e80
[   17.144081] gpio gpiochip0: registered chardev handle for 1 lines
[   17.148421]  x27: 0000000000000000
[   17.148422] x26: ffffffd301ff4298 x25: 0000000000000003 x24: 00000000000000f0
[   17.148424] x23: 0000000000000000 x22: 00000000ffffffff x21: 0000000000000001
[   17.148425] x20: ffffffffffffffd8 x19: ffffff80c0f25560 x18: 0000000000000000
[   17.148427] x17: ffffffd33864e408 x16: ffffffd33808f7c8 x15: 0000000000200000
[   17.232789] x14: e0cd73cf80ffffff x13: 50f2137c0a0338c9 x12: 0000000000000001
[   17.239912] x11: 0000000080150011 x10: 0000000000000002 x9 : 0000000000000001
[   17.247035] x8 : 0000000000000000 x7 : 0000000000008080 x6 : 8080000000000000
[   17.254158] x5 : ffffffd33808ebc0 x4 : fffffffe033dcf20 x3 : 0000000080150011
[   17.261281] x2 : ffffff8087a91400 x1 : 0000000000000000 x0 : ffffff80c0f25588
[   17.268404] Call trace:
[   17.270841]  usb_kill_anchored_urbs+0x60/0x168
[   17.275274]  btusb_mtk_release_iso_intf+0x2c/0xd8 [btusb (HASH:5afe 6)]
[   17.284226]  btusb_mtk_disconnect+0x14/0x28 [btusb (HASH:5afe 6)]
[   17.292652]  btusb_disconnect+0x70/0x140 [btusb (HASH:5afe 6)]
[   17.300818]  usb_unbind_interface+0xc4/0x240
[   17.305079]  device_release_driver_internal+0x18c/0x258
[   17.310296]  device_release_driver+0x1c/0x30
[   17.314557]  bus_remove_device+0x140/0x160
[   17.318643]  device_del+0x1c0/0x330
[   17.322121]  usb_disable_device+0x80/0x180
[   17.326207]  usb_disconnect+0xec/0x300
[   17.329948]  hub_quiesce+0x80/0xd0
[   17.333339]  hub_disconnect+0x44/0x190
[   17.337078]  usb_unbind_interface+0xc4/0x240
[   17.341337]  device_release_driver_internal+0x18c/0x258
[   17.346551]  device_release_driver+0x1c/0x30
[   17.350810]  usb_driver_release_interface+0x70/0x88
[   17.355677]  proc_ioctl+0x13c/0x228
[   17.359157]  proc_ioctl_default+0x50/0x80
[   17.363155]  usbdev_ioctl+0x830/0xd08
[   17.366808]  __arm64_sys_ioctl+0x94/0xd0
[   17.370723]  invoke_syscall+0x6c/0xf8
[   17.374377]  el0_svc_common+0x84/0xe0
[   17.378030]  do_el0_svc+0x20/0x30
[   17.381334]  el0_svc+0x34/0x60
[   17.384382]  el0t_64_sync_handler+0x88/0xf0
[   17.388554]  el0t_64_sync+0x180/0x188
[   17.392208] Code: f9400677 f100a2f4 54fffea0 d503201f (b8350288)
[   17.398289] ---[ end trace 0000000000000000 ]---

Fixes: ceac1cb0259d ("Bluetooth: btusb: mediatek: add ISO data transmission functions")
Signed-off-by: Chris Lu <chris.lu@mediatek.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit 61c5a3def90ac729a538e5ca5ff7f461cff72776)
mainline inclusion
from mainline-v6.14-rc1
category: bugfix

The documentation for usb_driver_claim_interface() says that "the
device lock" is needed when the function is called from places other
than probe(). This appears to be the lock for the USB interface
device. The Mediatek btusb code gets called via this path:

  Workqueue: hci0 hci_power_on [bluetooth]
  Call trace:
   usb_driver_claim_interface
   btusb_mtk_claim_iso_intf
   btusb_mtk_setup
   hci_dev_open_sync
   hci_power_on
   process_scheduled_works
   worker_thread
   kthread

With the above call trace the device lock hasn't been claimed. Claim
it.

Without this fix, we'd sometimes see the error "Failed to claim iso
interface". Sometimes we'd even see worse errors, like a NULL pointer
dereference (where `intf->dev.driver` was NULL) with a trace like:

  Call trace:
   usb_suspend_both
   usb_runtime_suspend
   __rpm_callback
   rpm_suspend
   pm_runtime_work
   process_scheduled_works

Both errors appear to be fixed with the proper locking.

Fixes: ceac1cb0259d ("Bluetooth: btusb: mediatek: add ISO data transmission functions")
Signed-off-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
(cherry picked from commit e9087e828827e5a5c85e124ce77503f2b81c3491)
Copy link

sourcery-ai bot commented Feb 17, 2025

Reviewer's Guide by Sourcery

This pull request synchronizes several bug fixes from the mainline kernel related to the MediaTek Bluetooth USB driver (btmtk). These fixes address issues related to claiming and releasing the ISO interface, handling USB disconnects, and power management. The changes involve adding locking, adjusting initialization order, modifying conditional checks, and implementing a disconnect flow.

Sequence diagram for claiming the ISO interface

sequenceDiagram
  participant btusb_mtk_claim_iso_intf
  participant device_lock
  participant usb_driver_claim_interface
  participant device_unlock
  participant set_bit
  participant init_usb_anchor

  btusb_mtk_claim_iso_intf->>device_lock: device_lock(&btmtk_data->isopkt_intf->dev)
  activate device_lock
  device_lock-->>btusb_mtk_claim_iso_intf: 
  deactivate device_lock
  btusb_mtk_claim_iso_intf->>usb_driver_claim_interface: usb_driver_claim_interface(&btusb_driver, btmtk_data->isopkt_intf, data)
  activate usb_driver_claim_interface
  usb_driver_claim_interface-->>btusb_mtk_claim_iso_intf: err
  deactivate usb_driver_claim_interface
  alt err < 0
    btusb_mtk_claim_iso_intf->>btusb_mtk_claim_iso_intf: btmtk_data->isopkt_intf = NULL
    btusb_mtk_claim_iso_intf->>btusb_mtk_claim_iso_intf: bt_dev_err(data->hdev, "Failed to claim iso interface")
  else err >= 0
    btusb_mtk_claim_iso_intf->>set_bit: set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)
    activate set_bit
    set_bit-->>btusb_mtk_claim_iso_intf: 
    deactivate set_bit
    btusb_mtk_claim_iso_intf->>init_usb_anchor: init_usb_anchor(&btmtk_data->isopkt_anchor)
    activate init_usb_anchor
    init_usb_anchor-->>btusb_mtk_claim_iso_intf: 
    deactivate init_usb_anchor
  end
  btusb_mtk_claim_iso_intf->>device_unlock: device_unlock(&btmtk_data->isopkt_intf->dev)
  activate device_unlock
  device_unlock-->>btusb_mtk_claim_iso_intf: 
  deactivate device_unlock
Loading

File-Level Changes

Change Details Files
Added locking around usb_driver_claim_interface() to prevent race conditions when claiming the ISO interface.
  • Added device_lock() before claiming the ISO interface.
  • Added device_unlock() after claiming the ISO interface.
drivers/bluetooth/btusb.c
Adjusted the position of ISO data anchor initialization.
  • Moved init_usb_anchor() to be called directly after claiming the ISO interface.
drivers/bluetooth/btusb.c
drivers/bluetooth/btmtk.c
Modified the conditions for claiming the ISO interface.
  • Added a check to ensure the ISO interface is only claimed if it hasn't already been claimed.
  • The ISO interface is now retrieved using usb_ifnum_to_if() only if it hasn't been claimed yet.
drivers/bluetooth/btusb.c
Implemented a flow to release the ISO interface when the USB device is disconnected.
  • Added a btusb_mtk_disconnect() function that calls btusb_mtk_release_iso_intf() to release the ISO interface.
  • Assigned the btusb_mtk_disconnect function to the disconnect callback in the btusb_probe function.
drivers/bluetooth/btusb.c
Moved the Bluetooth power off command to the shutdown function.
  • The ISO interface is now released in the shutdown function.
  • The btmtk_usb_shutdown function is called before releasing the ISO interface.
drivers/bluetooth/btusb.c
Refactored the btusb_mtk_release_iso_intf function to accept hci_dev as an argument.
  • The function now accepts hci_dev instead of btusb_data.
  • The function now uses hci_get_priv to get the btmtk_data struct.
drivers/bluetooth/btusb.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @opsiff - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It looks like btusb_mtk_release_iso_intf now takes struct hci_dev *hdev instead of struct btusb_data *data, but the commit message still refers to data.
  • Consider adding a comment explaining why btusb_mtk_disconnect is needed.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sourcery-ai[bot]
Once this PR has been reviewed and has the lgtm label, please ask for approval from opsiff. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants