Bluetooth: Fix memory leak of hci device
Subsystems (2)
PATCH 1/1 Bluetooth: Fix memory leak of hci device
Fault injection test reported memory leak of hci device as follows:
unreferenced object 0xffff88800b858000 (size 8192):
comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N..
backtrace:
[<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208
[<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591
[<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240
[<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397
[<00000000cbac9243>] really_probe drivers/base/dd.c:517
[<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751
[<00000000202135cb>] driver_probe_device drivers/base/dd.c:782
[<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899
[<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427
[<00000000c9551f0b>] __device_attach drivers/base/dd.c:971
[<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487
[<000000007bb8b95a>] device_add drivers/base/core.c:3364
[<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171
[<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239
[<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294
[<00000000cbac9243>] really_probe drivers/base/dd.c:517hci_alloc_dev() do not init the device's flag. And hci_free_dev() using put_device() to free the memory allocated for this device, but it calls just kfree(dev) only in case of HCI_UNREGISTER flag is set. So any error handing before hci_register_dev() success will cause memory leak.
To avoid this behaviour we need to set hdev HCI_UNREGISTER flag in hci_alloc_dev_priv().
net/bluetooth/hci_core.c +5 -0
@@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout);INIT_DELAYED_WORK (&hdev->ncmd_timer, hci_ncmd_timeout); +/* We need to set HCI_UNREGISTER flag to correctly release +* the device in hci_free_dev() +*/ +hci_dev_set_flag (hdev,HCI_UNREGISTER ); +hci_request_setup (hdev);hci_init_sysfs (hdev);
Signature · 1 line
2.25.1
Marcel Holtmann
maintainerHi Wei,
Quoted 41 lines
Fault injection test reported memory leak of hci device as follows:
unreferenced object
0xffff88800b858000(size 8192):
comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes):00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N.. backtrace: [<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208 [<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591 [<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240 [<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 [<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751 [<00000000202135cb>] driver_probe_device drivers/base/dd.c:782 [<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899 [<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427 [<00000000c9551f0b>] __device_attach drivers/base/dd.c:971 [<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487 [<000000007bb8b95a>] device_add drivers/base/core.c:3364 [<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171 [<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239 [<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294 [<00000000cbac9243>] really_probe drivers/base/dd.c:517
hci_alloc_dev()do not init the device's flag. Andhci_free_dev()usingput_device()to free the memory allocated for this device, but it calls just kfree(dev) only in case ofHCI_UNREGISTERflag is set. So any error handing beforehci_register_dev()success will cause memory leak.To avoid this behaviour we need to set hdev
HCI_UNREGISTERflag inhci_alloc_dev_priv().Signed-off-by Wei Yongjun <[email protected]>net/bluetooth/hci_core.c +0
-0
net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 3883
Quoted 8 lines
net/bluetooth/hci_core.c +5
-0@@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout);INIT_DELAYED_WORK (&hdev->ncmd_timer, hci_ncmd_timeout); +/* We need to set HCI_UNREGISTER flag to correctly release +* the device in hci_free_dev() +*/ +hci_dev_set_flag (hdev,HCI_UNREGISTER ); +
I can see the point in the bug report, but I don’t see that the fix is correct. Can you prove that this fix is correct when hci_register_dev is actually called.
Regards
Marcel
Luiz Augusto von Dentz
maintainerHi Wei, Marcel,
Quoted 47 lines · Marcel Holtmann
Hi Wei,
Quoted 44 lines
Fault injection test reported memory leak of hci device as follows:
unreferenced object
0xffff88800b858000(size 8192):
comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes):00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N.. backtrace: [<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208 [<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591 [<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240 [<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 [<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751 [<00000000202135cb>] driver_probe_device drivers/base/dd.c:782 [<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899 [<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427 [<00000000c9551f0b>] __device_attach drivers/base/dd.c:971 [<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487 [<000000007bb8b95a>] device_add drivers/base/core.c:3364 [<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171 [<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239 [<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294 [<00000000cbac9243>] really_probe drivers/base/dd.c:517
hci_alloc_dev()do not init the device's flag. Andhci_free_dev()usingput_device()to free the memory allocated for this device, but it calls just kfree(dev) only in case ofHCI_UNREGISTERflag is set. So any error handing beforehci_register_dev()success will cause memory leak.To avoid this behaviour we need to set hdev
HCI_UNREGISTERflag inhci_alloc_dev_priv().Signed-off-by Wei Yongjun <[email protected]>net/bluetooth/hci_core.c +0
-0@@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout);INIT_DELAYED_WORK (&hdev->ncmd_timer, hci_ncmd_timeout);
Quoted 7 lines
net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 3883
Quoted 5 lines
net/bluetooth/hci_core.c +5
-0+/* We need to set HCI_UNREGISTER flag to correctly release +* the device in hci_free_dev() +*/ +hci_dev_set_flag (hdev,HCI_UNREGISTER ); +I can see the point in the bug report, but I don’t see that the fix is correct. Can you prove that this fix is correct when
hci_register_devis actually called.
I also wonder where is the actual check for HCI_UNREGISTER that the commit description says prevents the kfree? hci_free_dev itself just calls put_device, so perhaps it is actually talking about the check in bt_host_release, anyway in for this to work the HCI_UNREGISTER would have to be cleared by hci_register_dev otherwise the likes of hci_dev_do_open don't work as it checks if HCI_UNREGISTER had been called.
We also would need to check if it is safe to call hci_release_dev if the workqueues, etc, had not been initialized yet, or perhaps don't really use HCI_UNREGISTER and just do something like this:
net/bluetooth/hci_sysfs.c +3 -0
@@ -86,6 +86,9 @@ static void bt_host_release(struct device *dev)if (hci_dev_test_flag (hdev,HCI_UNREGISTER ))hci_release_dev (hdev); +else +kfree (hdev); +module_put (THIS_MODULE );
If this doesn't fix all the leaks that probably means part of the hci_release_dev still needs to be executed which can probably be done by having the check for HCI_UNREGISTER around the code that does actually depend on hci_register_dev.
Signature · 1 line
Luiz Augusto von Dentz
Marcel Holtmann
maintainerHi Luiz,
Quoted 77 lines
Quoted 44 lines
Quoted 44 lines
Fault injection test reported memory leak of hci device as follows:
unreferenced object
0xffff88800b858000(size 8192):
comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N..
backtrace:[<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208 [<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591 [<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240 [<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 [<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751 [<00000000202135cb>] driver_probe_device drivers/base/dd.c:782 [<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899 [<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427 [<00000000c9551f0b>] __device_attach drivers/base/dd.c:971 [<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487 [<000000007bb8b95a>] device_add drivers/base/core.c:3364 [<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171 [<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239 [<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294 [<00000000cbac9243>] really_probe drivers/base/dd.c:517
hci_alloc_dev()do not init the device's flag. Andhci_free_dev()usingput_device()to free the memory allocated for this device, but it calls just kfree(dev) only in case ofHCI_UNREGISTERflag is set. So any error handing beforehci_register_dev()success will cause memory leak.To avoid this behaviour we need to set hdev
HCI_UNREGISTERflag inhci_alloc_dev_priv().Signed-off-by Wei Yongjun <[email protected]>net/bluetooth/hci_core.c +0
-0@@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout);INIT_DELAYED_WORK (&hdev->ncmd_timer, hci_ncmd_timeout);Quoted 7 lines
net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 3883
Quoted 5 lines
net/bluetooth/hci_core.c +5
-0+/* We need to set HCI_UNREGISTER flag to correctly release +* the device in hci_free_dev() +*/ +hci_dev_set_flag (hdev,HCI_UNREGISTER ); +I can see the point in the bug report, but I don’t see that the fix is correct. Can you prove that this fix is correct when
hci_register_devis actually called.I also wonder where is the actual check for
HCI_UNREGISTERthat the commit description says prevents the kfree?hci_free_devitself just callsput_device, so perhaps it is actually talking about the check inbt_host_release, anyway in for this to work theHCI_UNREGISTERwould have to be cleared byhci_register_devotherwise the likes ofhci_dev_do_opendon't work as it checks ifHCI_UNREGISTERhad been called.We also would need to check if it is safe to call
hci_release_devif the workqueues, etc, had not been initialized yet, or perhaps don't really useHCI_UNREGISTERand just do something like this:net/bluetooth/hci_sysfs.c +3
-0@@ -86,6 +86,9 @@ static void bt_host_release(struct device *dev)if (hci_dev_test_flag (hdev,HCI_UNREGISTER ))hci_release_dev (hdev); +else +kfree (hdev); +module_put (THIS_MODULE );
Quoted 4 lines
If this doesn't fix all the leaks that probably means part of the
hci_release_devstill needs to be executed which can probably be done by having the check forHCI_UNREGISTERaround the code that does actually depend onhci_register_dev.
what we really need to do is move towards devm integration so that all allocation are tied to the driver model. However this might need a cleaner struct device integration.
Regards
Marcel
Hi Luiz, Marcel
Quoted 77 lines
Hi Luiz,
Quoted 75 lines
Quoted 44 lines
Quoted 44 lines
Fault injection test reported memory leak of hci device as follows:
unreferenced object
0xffff88800b858000(size 8192):
comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes):00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N.. backtrace: [<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208 [<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591 [<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240 [<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 [<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751 [<00000000202135cb>] driver_probe_device drivers/base/dd.c:782 [<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899 [<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427 [<00000000c9551f0b>] __device_attach drivers/base/dd.c:971 [<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487 [<000000007bb8b95a>] device_add drivers/base/core.c:3364 [<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171 [<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239 [<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294 [<00000000cbac9243>] really_probe drivers/base/dd.c:517
hci_alloc_dev()do not init the device's flag. Andhci_free_dev()usingput_device()to free the memory allocated for this device, but it calls just kfree(dev) only in case ofHCI_UNREGISTERflag is set. So any error handing beforehci_register_dev()success will cause memory leak.To avoid this behaviour we need to set hdev
HCI_UNREGISTERflag inhci_alloc_dev_priv().Signed-off-by Wei Yongjun <[email protected]>net/bluetooth/hci_core.c +0
-0@@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout);INIT_DELAYED_WORK (&hdev->ncmd_timer, hci_ncmd_timeout);Quoted 6 lines
net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 3883
Quoted 5 lines
net/bluetooth/hci_core.c +5
-0+/* We need to set HCI_UNREGISTER flag to correctly release +* the device in hci_free_dev() +*/ +hci_dev_set_flag (hdev,HCI_UNREGISTER ); +I can see the point in the bug report, but I don’t see that the fix is correct. Can you prove that this fix is correct when
hci_register_devis actually called.I also wonder where is the actual check for
HCI_UNREGISTERthat the commit description says prevents the kfree?hci_free_devitself just callsput_device, so perhaps it is actually talking about the check inbt_host_release, anyway in for this to work theHCI_UNREGISTERwould have to be cleared byhci_register_devotherwise the likes ofhci_dev_do_opendon't work as it checks ifHCI_UNREGISTERhad been called.We also would need to check if it is safe to call
hci_release_devif the workqueues, etc, had not been initialized yet, or perhaps don't really useHCI_UNREGISTERand just do something like this:net/bluetooth/hci_sysfs.c +3
-0@@ -86,6 +86,9 @@ static void bt_host_release(struct device *dev)if (hci_dev_test_flag (hdev,HCI_UNREGISTER ))hci_release_dev (hdev); +else +kfree (hdev); +module_put (THIS_MODULE );
Quoted 6 lines
Quoted 4 lines
If this doesn't fix all the leaks that probably means part of the
hci_release_devstill needs to be executed which can probably be done by having the check forHCI_UNREGISTERaround the code that does actually depend onhci_register_dev.what we really need to do is move towards devm integration so that all allocation are tied to the driver model. However this might need a cleaner struct device integration.
I have tested with your fix with fault injection, and all the
memory leak is gone.
Regards,
Wei Yongjun