Bluetooth: Fix a few module init/deinit bugs
Subsystems (3)
v2 cover Bluetooth: Fix a few module init/deinit bugs
Hi all,
These patches fix a few bugs in init and deinit for the bluetooth module. I ran into the first one when I started running a kernel with debugfs disabled on my laptop, and I ran into the next two when I was working on the fix for the first one.
v2:
* Move iso_exit() call to bt_exit() (Luiz Augusto von Dentz)
Aaron Thompson (3):
Bluetooth: ISO: Fix multiple init when debugfs is disabled
Bluetooth: Call iso_exit() on module unload
Bluetooth: Remove debugfs directory on module init failure
net/bluetooth/af_bluetooth.c | 3 +++ net/bluetooth/iso.c | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-)
base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
Signature · 1 line
2.39.5
PATCH v2 1/3 Bluetooth: ISO: Fix multiple init when debugfs is disabled
From: Aaron Thompson <[email protected]>
If bt_debugfs is not created successfully, which happens if either CONFIG_DEBUG_FS or CONFIG_DEBUG_FS_ALLOW_ALL is unset, then iso_init() returns early and does not set iso_inited to true. This means that a subsequent call to iso_init() will result in duplicate calls to proto_register(), bt_sock_register(), etc.
With CONFIG_LIST_HARDENED and CONFIG_BUG_ON_DATA_CORRUPTION enabled, the duplicate call to proto_register() triggers this BUG():
list_add double add: new=ffffffffc0b280d0, prev=ffffffffbab56250,
next=ffffffffc0b280d0.
------------[ cut here ]------------
kernel BUG at lib/list_debug.c:35!
Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
CPU: 2 PID: 887 Comm: bluetoothd Not tainted 6.10.11-1-ao-desktop #1
RIP: 0010:__list_add_valid_or_report+0x9a/0xa0
...
__list_add_valid_or_report+0x9a/0xa0
proto_register+0x2b5/0x340
iso_init+0x23/0x150 [bluetooth]
set_iso_socket_func+0x68/0x1b0 [bluetooth]
kmem_cache_free+0x308/0x330
hci_sock_sendmsg+0x990/0x9e0 [bluetooth]
__sock_sendmsg+0x7b/0x80
sock_write_iter+0x9a/0x110
do_iter_readv_writev+0x11d/0x220
vfs_writev+0x180/0x3e0
do_writev+0xca/0x100
...This change removes the early return. The check for iso_debugfs being NULL was unnecessary, it is always NULL when iso_inited is false.
--- net/bluetooth/iso.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
net/bluetooth/iso.c +1 -5
@@ -2301,13 +2301,9 @@ int iso_init(void)hci_register_cb (&iso_cb); -if (IS_ERR_OR_NULL (bt_debugfs)) -return 0 ; - -if (!iso_debugfs) { +if (!IS_ERR_OR_NULL (bt_debugfs)) iso_debugfs =debugfs_create_file ("iso" ,0444 , bt_debugfs,NULL , &iso_debugfs_fops); - } iso_inited = true;
Signature · 1 line
2.39.5
PATCH v2 2/3 Bluetooth: Call iso_exit() on module unload
In reply to Aaron ThompsonFrom: Aaron Thompson <[email protected]>
If iso_init() has been called, iso_exit() must be called on module unload. Without that, the struct proto that iso_init() registered with proto_register() becomes invalid, which could cause unpredictable problems later. In my case, with CONFIG_LIST_HARDENED and CONFIG_BUG_ON_DATA_CORRUPTION enabled, loading the module again usually triggers this BUG():
list_add corruption. next->prev should be prev (ffffffffb5355fd0),
but was 0000000000000068. (next=ffffffffc0a010d0).
------------[ cut here ]------------
kernel BUG at lib/list_debug.c:29!
Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
CPU: 1 PID: 4159 Comm: modprobe Not tainted 6.10.11-4+bt2-ao-desktop #1
RIP: 0010:__list_add_valid_or_report+0x61/0xa0
...
__list_add_valid_or_report+0x61/0xa0
proto_register+0x299/0x320
hci_sock_init+0x16/0xc0 [bluetooth]
bt_init+0x68/0xd0 [bluetooth]
__pfx_bt_init+0x10/0x10 [bluetooth]
do_one_initcall+0x80/0x2f0
do_init_module+0x8b/0x230
__do_sys_init_module+0x15f/0x190
do_syscall_64+0x68/0x110
...--- net/bluetooth/af_bluetooth.c | 2 ++ 1 file changed, 2 insertions(+)
net/bluetooth/af_bluetooth.c +2 -0
@@ -830,6 +830,8 @@ static int __init bt_init(void) static void __exit bt_exit(void) { +iso_exit (); +mgmt_exit ();sco_exit ();
Signature · 1 line
2.39.5
PATCH v2 3/3 Bluetooth: Remove debugfs directory on module init failure
In reply to Aaron ThompsonFrom: Aaron Thompson <[email protected]>
If bt_init() fails, the debugfs directory currently is not removed. If the module is loaded again after that, the debugfs directory is not set up properly due to the existing directory.
# modprobe bluetooth # ls -laF /sys/kernel/debug/bluetooth total 0 drwxr-xr-x 2 root root 0 Sep 27 14:26 ./ drwx------ 31 root root 0 Sep 27 14:25 ../ -r--r--r-- 1 root root 0 Sep 27 14:26 l2cap -r--r--r-- 1 root root 0 Sep 27 14:26 sco # modprobe -r bluetooth # ls -laF /sys/kernel/debug/bluetooth ls: cannot access '/sys/kernel/debug/bluetooth': No such file or directory #
# modprobe bluetooth modprobe: ERROR: could not insert 'bluetooth': Invalid argument # dmesg | tail -n 6 Bluetooth: Core ver 2.22 NET: Registered PF_BLUETOOTH protocol family Bluetooth: HCI device and connection manager initialized Bluetooth: HCI socket layer initialized Bluetooth: Faking l2cap_init() failure for testing NET: Unregistered PF_BLUETOOTH protocol family # ls -laF /sys/kernel/debug/bluetooth total 0 drwxr-xr-x 2 root root 0 Sep 27 14:31 ./ drwx------ 31 root root 0 Sep 27 14:26 ../ #
# modprobe bluetooth # dmesg | tail -n 7 Bluetooth: Core ver 2.22 debugfs: Directory 'bluetooth' with parent '/' already present! NET: Registered PF_BLUETOOTH protocol family Bluetooth: HCI device and connection manager initialized Bluetooth: HCI socket layer initialized Bluetooth: L2CAP socket layer initialized Bluetooth: SCO socket layer initialized # ls -laF /sys/kernel/debug/bluetooth total 0 drwxr-xr-x 2 root root 0 Sep 27 14:31 ./ drwx------ 31 root root 0 Sep 27 14:26 ../ #
--- net/bluetooth/af_bluetooth.c | 1 + 1 file changed, 1 insertion(+)
net/bluetooth/af_bluetooth.c +1 -0
@@ -825,6 +825,7 @@ static int __init bt_init(void)bt_sysfs_cleanup (); cleanup_led:bt_leds_cleanup (); +debugfs_remove_recursive (bt_debugfs);return err; }
Signature · 1 line
2.39.5
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 8 lines · On Fri, 4 Oct 2024 23:04:07 +0000 you wrote:
Hi all,
These patches fix a few bugs in init and deinit for the bluetooth module. I ran into the first one when I started running a kernel with debugfs disabled on my laptop, and I ran into the next two when I was working on the fix for the first one.
[...]
Here is the summary with links:
- [v2,1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled
https://git.kernel.org/bluetooth/bluetooth-next/c/6259a9d4578d - [v2,2/3] Bluetooth: Call iso_exit() on module unload https://git.kernel.org/bluetooth/bluetooth-next/c/d18b99bb8795 - [v2,3/3] Bluetooth: Remove debugfs directory on module init failure https://git.kernel.org/bluetooth/bluetooth-next/c/f5a04a514aa2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html