Bluetooth: btusb: Fix memory leak in play_deferred
PATCH v4 1/1 Bluetooth: btusb: Fix memory leak in play_deferred
Currently we are calling usb_submit_urb directly to submit deferred tx urbs after unanchor them.
So the usb_giveback_urb_bh would failed to unref it in usb_unanchor_urb and cause memory leak:
unreferenced object 0xffffffc0ce0fa400 (size 256):
...
backtrace:
[<ffffffc00034a9a8>] __save_stack_trace+0x48/0x6c
[<ffffffc00034b088>] create_object+0x138/0x254
[<ffffffc0009d5504>] kmemleak_alloc+0x58/0x8c
[<ffffffc000345f78>] __kmalloc+0x1d4/0x2a0
[<ffffffc0006765bc>] usb_alloc_urb+0x30/0x60
[<ffffffbffc128598>] alloc_ctrl_urb+0x38/0x120 [btusb]
[<ffffffbffc129e7c>] btusb_send_frame+0x64/0xf8 [btusb]Put those urbs in tx_anchor to avoid the leak, and also fix the error handling.
---
Changes in v4:
Drop the rest deferred urbs when error happens.
Changes in v3:
Put the deferred urbs in tx_anchor.
Changes in v2:
Call usb_free_urb instead of reusing submit_tx_urb.
drivers/bluetooth/btusb.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/bluetooth/btusb.c +19 -2
@@ -3266,13 +3266,30 @@ static void play_deferred(struct btusb_data *data)int err;while ((urb =usb_get_from_anchor (&data->deferred))) { +usb_anchor_urb (urb, &data->tx_anchor); + err =usb_submit_urb (urb,GFP_ATOMIC ); -if (err <0 ) +if (err <0 ) { +if (err != -EPERM && err != -ENODEV ) +BT_ERR ("%s urb %p submission failed (%d)" , + data->hdev->name, urb, -err); +kfree (urb->setup_packet); +usb_unanchor_urb (urb); +usb_free_urb (urb);break ; + } data->tx_in_flight++; +usb_free_urb (urb); + } + +/* Cleanup the rest deferred urbs. */ +while ((urb =usb_get_from_anchor (&data->deferred))) { +BT_ERR ("%s urb %p submission failed (%d)" , + data->hdev->name, urb, -err); +kfree (urb->setup_packet); +usb_free_urb (urb); } -usb_scuttle_anchored_urbs (&data->deferred); }static int btusb_resume (struct usb_interface *intf)
Signature · 1 line
2.1.4
Marcel Holtmann
maintainerHi Oliver,
Quoted 59 lines
Currently we are calling
usb_submit_urbdirectly to submit deferred tx urbs after unanchor them.So the
usb_giveback_urb_bhwould failed to unref it inusb_unanchor_urband cause memory leak:
unreferenced object0xffffffc0ce0fa400(size 256):
...
backtrace:[<ffffffc00034a9a8>] __save_stack_trace+0x48/0x6c [<ffffffc00034b088>] create_object+0x138/0x254 [<ffffffc0009d5504>] kmemleak_alloc+0x58/0x8c [<ffffffc000345f78>] __kmalloc+0x1d4/0x2a0 [<ffffffc0006765bc>] usb_alloc_urb+0x30/0x60 [<ffffffbffc128598>] alloc_ctrl_urb+0x38/0x120 [btusb] [<ffffffbffc129e7c>] btusb_send_frame+0x64/0xf8 [btusb]Put those urbs in
tx_anchorto avoid the leak, and also fix the error handling.Signed-off-by Jeffy Chen <[email protected]>---
Changes in v4:
Drop the rest deferred urbs when error happens.Changes in v3:
Put the deferred urbs intx_anchor.Changes in v2:
Callusb_free_urbinstead of reusingsubmit_tx_urb.
drivers/bluetooth/btusb.c| 21 +++++++++++++++++++--1 file changed, 19 insertions(+), 2 deletions(-)drivers/bluetooth/btusb.c +12
-1@@ -3266,13 +3266,30 @@ static void play_deferred(struct btusb_data *data)int err;while ((urb =usb_get_from_anchor (&data->deferred))) { +usb_anchor_urb (urb, &data->tx_anchor); + err =usb_submit_urb (urb,GFP_ATOMIC ); -if (err <0 ) +if (err <0 ) { +if (err != -EPERM && err != -ENODEV ) +BT_ERR ("%s urb %p submission failed (%d)" , + data->hdev->name, urb, -err); +kfree (urb->setup_packet); +usb_unanchor_urb (urb); +usb_free_urb (urb);break ; + } data->tx_in_flight++; +usb_free_urb (urb); + }
drivers/bluetooth/btusb.c static void play_deferred(struct btusb_data *data) · line 3293
Quoted 10 lines
drivers/bluetooth/btusb.c +7
-1+ +/* Cleanup the rest deferred urbs. */ +while ((urb =usb_get_from_anchor (&data->deferred))) { +BT_ERR ("%s urb %p submission failed (%d)" , + data->hdev->name, urb, -err); +kfree (urb->setup_packet); +usb_free_urb (urb); } -usb_scuttle_anchored_urbs (&data->deferred); }
if you think this is a valid approach, please provide an ack.
Regards
Marcel
kbuild test robot
build botreproduce: wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree make.cross…
Hi Jeffy,
[auto build test WARNING on bluetooth-next/master]
[also build test WARNING on v4.13-rc1]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Jeffy-Chen/Bluetooth-btusb-Fix-memory-leak-in-play_deferred/20170719-044201 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git master config: arm-exynos_defconfig (attached as .config) compiler: arm-linux-gnueabi-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm
Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
All warnings (new ones prefixed by >>):
In file included from drivers/bluetooth/btusb.c:32:0:
drivers/bluetooth/btusb.c: In function 'btusb_resume':
>> include/net/bluetooth/bluetooth.h:136:26: warning: 'err' may be used uninitialized in this function [-Wmaybe-uninitialized]
#define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__)
^~~~~~
drivers/bluetooth/btusb.c:3266:6: note: 'err' was declared here
int err;
^~~
--
In file included from drivers//bluetooth/btusb.c:32:0:
drivers//bluetooth/btusb.c: In function 'btusb_resume':
>> include/net/bluetooth/bluetooth.h:136:26: warning: 'err' may be used uninitialized in this function [-Wmaybe-uninitialized]
#define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__)
^~~~~~
drivers//bluetooth/btusb.c:3266:6: note: 'err' was declared here
int err;
^~~vim +/err +136 include/net/bluetooth/bluetooth.h
3ed7003e7 Joe Perches 2012-02-16 133 3ed7003e7 Joe Perches 2012-02-16 134 #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__) 594b31ea7 Frederic Danis 2015-09-23 135 #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__) 3ed7003e7 Joe Perches 2012-02-16 @136 #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__) 3ed7003e7 Joe Perches 2012-02-16 137 #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__)
^1da177e4 Linus Torvalds 2005-04-16 138
:::::: The code at line 136 was first introduced by commit
:::::: 3ed7003e724a04482e0ef1e794eece8c1c177b37 Bluetooth: Add logging functions bt_info and bt_err
:::::: TO: Joe Perches <[email protected]> :::::: CC: Johan Hedberg <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botreproduce:
Hi Jeffy,
[auto build test WARNING on bluetooth-next/master]
[also build test WARNING on v4.13-rc1 next-20170719]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Jeffy-Chen/Bluetooth-btusb-Fix-memory-leak-in-play_deferred/20170719-044201 base: https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git master config: i386-allyesconfig (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce: # save the attached .config to linux build tree make ARCH=i386
Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
All warnings (new ones prefixed by >>):
drivers/bluetooth/btusb.c: In function 'btusb_resume':
>> drivers/bluetooth/btusb.c:3288:3: warning: 'err' may be used uninitialized in this function [-Wmaybe-uninitialized]
BT_ERR("%s urb %p submission failed (%d)",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/bluetooth/btusb.c:3266:6: note: 'err' was declared here
int err;
^~~vim +/err +3288 drivers/bluetooth/btusb.c
3262
3263 static void play_deferred(struct btusb_data *data)
3264 {
3265 struct urb *urb;
3266 int err;
3267
3268 while ((urb = usb_get_from_anchor(&data->deferred))) {
3269 usb_anchor_urb(urb, &data->tx_anchor);
3270
3271 err = usb_submit_urb(urb, GFP_ATOMIC);
3272 if (err < 0) {
3273 if (err != -EPERM && err != -ENODEV)
3274 BT_ERR("%s urb %p submission failed (%d)",
3275 data->hdev->name, urb, -err);
3276 kfree(urb->setup_packet);
3277 usb_unanchor_urb(urb);
3278 usb_free_urb(urb);
3279 break;
3280 }
3281
3282 data->tx_in_flight++;
3283 usb_free_urb(urb);
3284 }
3285
3286 /* Cleanup the rest deferred urbs. */
3287 while ((urb = usb_get_from_anchor(&data->deferred))) {
> 3288 BT_ERR("%s urb %p submission failed (%d)", 3289 data->hdev->name, urb, -err);
3290 kfree(urb->setup_packet);
3291 usb_free_urb(urb);
3292 }
3293 }
3294
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation