linux-hyperv.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hv_netvsc: Fix memory leak when removing rndis device
@ 2020-01-13 19:27 Mohammed Gamal
  2020-01-13 19:55 ` Haiyang Zhang
  2020-01-13 20:02 ` Stephen Hemminger
  0 siblings, 2 replies; 3+ messages in thread
From: Mohammed Gamal @ 2020-01-13 19:27 UTC (permalink / raw)
  To: linux-hyperv, sthemmin, haiyangz, netdev
  Cc: kys, sashal, vkuznets, cavery, linux-kernel, Mohammed Gamal

kmemleak detects the following memory leak when hot removing
a network device:

unreferenced object 0xffff888083f63600 (size 256):
  comm "kworker/0:1", pid 12, jiffies 4294831717 (age 1113.676s)
  hex dump (first 32 bytes):
    00 40 c7 33 80 88 ff ff 00 00 00 00 10 00 00 00  .@.3............
    00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00  .....N..........
  backtrace:
    [<00000000d4a8f5be>] rndis_filter_device_add+0x117/0x11c0 [hv_netvsc]
    [<000000009c02d75b>] netvsc_probe+0x5e7/0xbf0 [hv_netvsc]
    [<00000000ddafce23>] vmbus_probe+0x74/0x170 [hv_vmbus]
    [<00000000046e64f1>] really_probe+0x22f/0xb50
    [<000000005cc35eb7>] driver_probe_device+0x25e/0x370
    [<0000000043c642b2>] bus_for_each_drv+0x11f/0x1b0
    [<000000005e3d09f0>] __device_attach+0x1c6/0x2f0
    [<00000000a72c362f>] bus_probe_device+0x1a6/0x260
    [<0000000008478399>] device_add+0x10a3/0x18e0
    [<00000000cf07b48c>] vmbus_device_register+0xe7/0x1e0 [hv_vmbus]
    [<00000000d46cf032>] vmbus_add_channel_work+0x8ab/0x1770 [hv_vmbus]
    [<000000002c94bb64>] process_one_work+0x919/0x17d0
    [<0000000096de6781>] worker_thread+0x87/0xb40
    [<00000000fbe7397e>] kthread+0x333/0x3f0
    [<000000004f844269>] ret_from_fork+0x3a/0x50

rndis_filter_device_add() allocates an instance of struct rndis_device
which never gets deallocated and rndis_filter_device_remove() sets
net_device->extension which points to the rndis_device struct to NULL
without ever freeing the structure first, leaving it dangling.

This patch fixes this by freeing the structure before setting
net_device->extension to NULL

Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
---
 drivers/net/hyperv/rndis_filter.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/net/hyperv/rndis_filter.c b/drivers/net/hyperv/rndis_filter.c
index 857c4bea451c..d2e094f521a4 100644
--- a/drivers/net/hyperv/rndis_filter.c
+++ b/drivers/net/hyperv/rndis_filter.c
@@ -1443,6 +1443,7 @@ void rndis_filter_device_remove(struct hv_device *dev,
 	/* Halt and release the rndis device */
 	rndis_filter_halt_device(net_dev, rndis_dev);
 
+	kfree(rndis_dev);
 	net_dev->extension = NULL;
 
 	netvsc_device_remove(dev);
-- 
2.21.0


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

* RE: [PATCH] hv_netvsc: Fix memory leak when removing rndis device
  2020-01-13 19:27 [PATCH] hv_netvsc: Fix memory leak when removing rndis device Mohammed Gamal
@ 2020-01-13 19:55 ` Haiyang Zhang
  2020-01-13 20:02 ` Stephen Hemminger
  1 sibling, 0 replies; 3+ messages in thread
From: Haiyang Zhang @ 2020-01-13 19:55 UTC (permalink / raw)
  To: Mohammed Gamal, linux-hyperv, Stephen Hemminger, netdev
  Cc: KY Srinivasan, sashal, vkuznets, cavery, linux-kernel



> -----Original Message-----
> From: Mohammed Gamal <mgamal@redhat.com>
> Sent: Monday, January 13, 2020 2:28 PM
> To: linux-hyperv@vger.kernel.org; Stephen Hemminger
> <sthemmin@microsoft.com>; Haiyang Zhang <haiyangz@microsoft.com>;
> netdev@vger.kernel.org
> Cc: KY Srinivasan <kys@microsoft.com>; sashal@kernel.org; vkuznets
> <vkuznets@redhat.com>; cavery <cavery@redhat.com>; linux-
> kernel@vger.kernel.org; Mohammed Gamal <mgamal@redhat.com>
> Subject: [PATCH] hv_netvsc: Fix memory leak when removing rndis device
> 
> kmemleak detects the following memory leak when hot removing a network
> device:
> 
> unreferenced object 0xffff888083f63600 (size 256):
>   comm "kworker/0:1", pid 12, jiffies 4294831717 (age 1113.676s)
>   hex dump (first 32 bytes):
>     00 40 c7 33 80 88 ff ff 00 00 00 00 10 00 00 00  .@.3............
>     00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00  .....N..........
>   backtrace:
>     [<00000000d4a8f5be>] rndis_filter_device_add+0x117/0x11c0 [hv_netvsc]
>     [<000000009c02d75b>] netvsc_probe+0x5e7/0xbf0 [hv_netvsc]
>     [<00000000ddafce23>] vmbus_probe+0x74/0x170 [hv_vmbus]
>     [<00000000046e64f1>] really_probe+0x22f/0xb50
>     [<000000005cc35eb7>] driver_probe_device+0x25e/0x370
>     [<0000000043c642b2>] bus_for_each_drv+0x11f/0x1b0
>     [<000000005e3d09f0>] __device_attach+0x1c6/0x2f0
>     [<00000000a72c362f>] bus_probe_device+0x1a6/0x260
>     [<0000000008478399>] device_add+0x10a3/0x18e0
>     [<00000000cf07b48c>] vmbus_device_register+0xe7/0x1e0 [hv_vmbus]
>     [<00000000d46cf032>] vmbus_add_channel_work+0x8ab/0x1770 [hv_vmbus]
>     [<000000002c94bb64>] process_one_work+0x919/0x17d0
>     [<0000000096de6781>] worker_thread+0x87/0xb40
>     [<00000000fbe7397e>] kthread+0x333/0x3f0
>     [<000000004f844269>] ret_from_fork+0x3a/0x50
> 
> rndis_filter_device_add() allocates an instance of struct rndis_device which
> never gets deallocated and rndis_filter_device_remove() sets net_device-
> >extension which points to the rndis_device struct to NULL without ever freeing
> the structure first, leaving it dangling.
> 
> This patch fixes this by freeing the structure before setting net_device-
> >extension to NULL
> 
> Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
> ---
>  drivers/net/hyperv/rndis_filter.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/net/hyperv/rndis_filter.c b/drivers/net/hyperv/rndis_filter.c
> index 857c4bea451c..d2e094f521a4 100644
> --- a/drivers/net/hyperv/rndis_filter.c
> +++ b/drivers/net/hyperv/rndis_filter.c
> @@ -1443,6 +1443,7 @@ void rndis_filter_device_remove(struct hv_device
> *dev,
>  	/* Halt and release the rndis device */
>  	rndis_filter_halt_device(net_dev, rndis_dev);
> 
> +	kfree(rndis_dev);
>  	net_dev->extension = NULL;

The struct rndis_device *should* be freed in free_netvsc_device_rcu()
==> free_netvsc_device():

static void free_netvsc_device(struct rcu_head *head)
{
        struct netvsc_device *nvdev
                = container_of(head, struct netvsc_device, rcu);
        int i;

        kfree(nvdev->extension);

So we no longer free it in the rndis_filter_device_remove().

But, the commit 02400fcee2542ee334a2394e0d9f6efd969fe782 did
have a bug:
	Date: Tue, 20 Mar 2018 15:03:03 -0700
	[PATCH] hv_netvsc: use RCU to fix concurrent rx and queue changes

It should have removed the following line when moving the free() to 
free_netvsc_device():
>  	net_dev->extension = NULL;
Then, the leak of rndis_dev will be fixed. I suggested you to fix it in this
way.

Thanks,
- Haiyang


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

* Re: [PATCH] hv_netvsc: Fix memory leak when removing rndis device
  2020-01-13 19:27 [PATCH] hv_netvsc: Fix memory leak when removing rndis device Mohammed Gamal
  2020-01-13 19:55 ` Haiyang Zhang
@ 2020-01-13 20:02 ` Stephen Hemminger
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2020-01-13 20:02 UTC (permalink / raw)
  To: Mohammed Gamal
  Cc: linux-hyperv, sthemmin, haiyangz, netdev, kys, sashal, vkuznets,
	cavery, linux-kernel

On Mon, 13 Jan 2020 21:27:52 +0200
Mohammed Gamal <mgamal@redhat.com> wrote:

> kmemleak detects the following memory leak when hot removing
> a network device:
> 
> unreferenced object 0xffff888083f63600 (size 256):
>   comm "kworker/0:1", pid 12, jiffies 4294831717 (age 1113.676s)
>   hex dump (first 32 bytes):
>     00 40 c7 33 80 88 ff ff 00 00 00 00 10 00 00 00  .@.3............
>     00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00  .....N..........
>   backtrace:
>     [<00000000d4a8f5be>] rndis_filter_device_add+0x117/0x11c0 [hv_netvsc]
>     [<000000009c02d75b>] netvsc_probe+0x5e7/0xbf0 [hv_netvsc]
>     [<00000000ddafce23>] vmbus_probe+0x74/0x170 [hv_vmbus]
>     [<00000000046e64f1>] really_probe+0x22f/0xb50
>     [<000000005cc35eb7>] driver_probe_device+0x25e/0x370
>     [<0000000043c642b2>] bus_for_each_drv+0x11f/0x1b0
>     [<000000005e3d09f0>] __device_attach+0x1c6/0x2f0
>     [<00000000a72c362f>] bus_probe_device+0x1a6/0x260
>     [<0000000008478399>] device_add+0x10a3/0x18e0
>     [<00000000cf07b48c>] vmbus_device_register+0xe7/0x1e0 [hv_vmbus]
>     [<00000000d46cf032>] vmbus_add_channel_work+0x8ab/0x1770 [hv_vmbus]
>     [<000000002c94bb64>] process_one_work+0x919/0x17d0
>     [<0000000096de6781>] worker_thread+0x87/0xb40
>     [<00000000fbe7397e>] kthread+0x333/0x3f0
>     [<000000004f844269>] ret_from_fork+0x3a/0x50
> 
> rndis_filter_device_add() allocates an instance of struct rndis_device
> which never gets deallocated and rndis_filter_device_remove() sets
> net_device->extension which points to the rndis_device struct to NULL
> without ever freeing the structure first, leaving it dangling.
> 
> This patch fixes this by freeing the structure before setting
> net_device->extension to NULL
> 
> Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
> ---
>  drivers/net/hyperv/rndis_filter.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/net/hyperv/rndis_filter.c b/drivers/net/hyperv/rndis_filter.c
> index 857c4bea451c..d2e094f521a4 100644
> --- a/drivers/net/hyperv/rndis_filter.c
> +++ b/drivers/net/hyperv/rndis_filter.c
> @@ -1443,6 +1443,7 @@ void rndis_filter_device_remove(struct hv_device *dev,
>  	/* Halt and release the rndis device */
>  	rndis_filter_halt_device(net_dev, rndis_dev);
>  
> +	kfree(rndis_dev);
>  	net_dev->extension = NULL;
>  
>  	netvsc_device_remove(dev);

That is one way, but maybe safer to just remove the line that sets
extension to NULL. That way netvsc_device_remove will clean it up:
    netvsc_device_remove -> free_netvsc_device_rcu -> free_netvsc_device.

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

end of thread, other threads:[~2020-01-13 20:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-13 19:27 [PATCH] hv_netvsc: Fix memory leak when removing rndis device Mohammed Gamal
2020-01-13 19:55 ` Haiyang Zhang
2020-01-13 20:02 ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).