mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred
@ 2017-07-18  9:53 Jeffy Chen
  2017-07-18  9:58 ` Marcel Holtmann
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jeffy Chen @ 2017-07-18  9:53 UTC (permalink / raw)
  To: linux-kernel
  Cc: xiyou.wangcong, briannorris, dianders, Jeffy Chen, Johan Hedberg,
	Marcel Holtmann, Gustavo Padovan, linux-bluetooth

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.

Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---

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(-)

diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c
index 0d533b2..dbf4a05 100644
--- a/drivers/bluetooth/btusb.c
+++ b/drivers/bluetooth/btusb.c
@@ -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)
-- 
2.1.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred
  2017-07-18  9:53 [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred Jeffy Chen
@ 2017-07-18  9:58 ` Marcel Holtmann
  2017-07-19 14:53 ` kbuild test robot
  2017-07-19 19:21 ` kbuild test robot
  2 siblings, 0 replies; 4+ messages in thread
From: Marcel Holtmann @ 2017-07-18  9:58 UTC (permalink / raw)
  To: Jeffy Chen, Oliver Neukum
  Cc: LKML, xiyou.wangcong, Brian Norris, Douglas Anderson,
	Johan Hedberg, Gustavo F. Padovan, linux-bluetooth

Hi Oliver,

> 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.
> 
> Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
> ---
> 
> 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(-)
> 
> diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c
> index 0d533b2..dbf4a05 100644
> --- a/drivers/bluetooth/btusb.c
> +++ b/drivers/bluetooth/btusb.c
> @@ -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);
> }

if you think this is a valid approach, please provide an ack.

Regards

Marcel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred
  2017-07-18  9:53 [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred Jeffy Chen
  2017-07-18  9:58 ` Marcel Holtmann
@ 2017-07-19 14:53 ` kbuild test robot
  2017-07-19 19:21 ` kbuild test robot
  2 siblings, 0 replies; 4+ messages in thread
From: kbuild test robot @ 2017-07-19 14:53 UTC (permalink / raw)
  To: Jeffy Chen
  Cc: kbuild-all, linux-kernel, xiyou.wangcong, briannorris, dianders,
	Jeffy Chen, Johan Hedberg, Marcel Holtmann, Gustavo Padovan,
	linux-bluetooth

[-- Attachment #1: Type: text/plain, Size: 2768 bytes --]

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 <joe@perches.com>
:::::: CC: Johan Hedberg <johan.hedberg@intel.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 27439 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred
  2017-07-18  9:53 [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred Jeffy Chen
  2017-07-18  9:58 ` Marcel Holtmann
  2017-07-19 14:53 ` kbuild test robot
@ 2017-07-19 19:21 ` kbuild test robot
  2 siblings, 0 replies; 4+ messages in thread
From: kbuild test robot @ 2017-07-19 19:21 UTC (permalink / raw)
  To: Jeffy Chen
  Cc: kbuild-all, linux-kernel, xiyou.wangcong, briannorris, dianders,
	Jeffy Chen, Johan Hedberg, Marcel Holtmann, Gustavo Padovan,
	linux-bluetooth

[-- Attachment #1: Type: text/plain, Size: 2374 bytes --]

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 59810 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-07-19 19:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-18  9:53 [PATCH v4] Bluetooth: btusb: Fix memory leak in play_deferred Jeffy Chen
2017-07-18  9:58 ` Marcel Holtmann
2017-07-19 14:53 ` kbuild test robot
2017-07-19 19:21 ` kbuild test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®