Bluetooth: Fix a few module init/deinit bugs
Subsystems (3)
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.
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 | 1 + net/bluetooth/iso.c | 6 +----- net/bluetooth/mgmt.c | 1 + 3 files changed, 3 insertions(+), 5 deletions(-)
base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
Signature · 1 line
2.39.5
PATCH 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 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/mgmt.c | 1 + 1 file changed, 1 insertion(+)
net/bluetooth/mgmt.c +1 -0
@@ -10487,6 +10487,7 @@ int mgmt_init(void)void mgmt_exit (void ) { +iso_exit ();hci_mgmt_chan_unregister (&chan); }
Signature · 1 line
2.39.5
Luiz Augusto von Dentz
maintainerRe: [PATCH 2/3] Bluetooth: Call iso_exit() on module unload
Hi Aaron,
Quoted 49 lines · Aaron Thompson
From: Aaron Thompson <[email protected]>
If
iso_init()has been called,iso_exit()must be called on module unload. Without that, the struct proto thatiso_init()registered withproto_register()becomes invalid, which could cause unpredictable problems later. In my case, withCONFIG_LIST_HARDENEDandCONFIG_BUG_ON_DATA_CORRUPTIONenabled, loading the module again usually triggers thisBUG():
list_addcorruption.next->prevshould 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 ...Fixes ccf74f2390d6 ("Bluetooth: Add BTPROTO_ISO socket type")Signed-off-by Aaron Thompson <[email protected]>--- net/bluetooth/mgmt.c | 1 + 1 file changed, 1 insertion(+)net/bluetooth/mgmt.c +1
-1@@ -10487,6 +10487,7 @@ int mgmt_init(void)void mgmt_exit (void ) { +iso_exit ();hci_mgmt_chan_unregister (&chan); } --
Quoted 1 line
2.39.5
I had it under bt_exit with the rest of the exit functions so that we don't have to move it once ISO sockets become stable.
Signature · 1 line
Luiz Augusto von Dentz
Re: [PATCH 2/3] Bluetooth: Call iso_exit() on module unload
Hi Luiz,
Quoted 4 lines · Luiz Augusto von Dentz
I had it under
bt_exitwith the rest of the exit functions so that we don't have to move it once ISO sockets become stable.
Sure thing. I was just guessing based on where the call to iso_init() is. Thanks for the review!
-- Aaron
PATCH 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