Re: [PATCH 1/2] can: m_can: m_can_plat_probe(): free can_net device in case probe fails

2021-02-05 Thread Marc Kleine-Budde
On 05.02.2021 15:25:58, Xulin Sun wrote:
> The can_net device is allocated through kvzalloc(), if the subsequent probe
> cases fail to initialize, it should free the can_net device that has been
> successfully allocated before.
> 
> To fix below memory leaks call trace:
> 
> unreferenced object 0xfc08418b (size 32768):
> comm "kworker/0:1", pid 22, jiffies 4294893966 (age 931.976s)
> hex dump (first 32 bytes):
> 63 61 6e 25 64 00 00 00 00 00 00 00 00 00 00 00 can%d...
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
> backtrace:
> [<3faec9cc>] __kmalloc+0x1a4/0x3e0
> [<560b1cad>] kvmalloc_node+0xa0/0xb0
> [<93bada32>] alloc_netdev_mqs+0x60/0x380
> [<41ddbb53>] alloc_candev_mqs+0x6c/0x14c
> [] m_can_class_allocate_dev+0x64/0x18c
> [<9fef1617>] m_can_plat_probe+0x2c/0x1f4
> [<6fdcc497>] platform_drv_probe+0x5c/0xb0
> [] really_probe+0xec/0x41c
> [<3ffa5158>] driver_probe_device+0x60/0xf0
> [<5986c77e>] __device_attach_driver+0xb0/0x100
> [<757823bc>] bus_for_each_drv+0x8c/0xe0
> [<59253919>] __device_attach+0xdc/0x180
> [<35c2b9f1>] device_initial_probe+0x28/0x34
> [<82e2c85c>] bus_probe_device+0xa4/0xb0
> [] deferred_probe_work_func+0x7c/0xb0
> [<01b85f22>] process_one_work+0x1ec/0x480
> 
> Signed-off-by: Xulin Sun 

This patch doesn't apply to net/master, since v5.10 there is a
similar fix:

85816aba460c can: m_can: Fix freeing of can device from peripherials

Please update to latest v5.10.x. If you're on a kernel that's still
supported, and you're using the latest stable of that kernel, and it
doesn't have that patch applied, ask on linux-stable to pick up that
patch.

Marc

-- 
Pengutronix e.K. | Marc Kleine-Budde   |
Embedded Linux   | https://www.pengutronix.de  |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917- |


signature.asc
Description: PGP signature


[PATCH 1/2] can: m_can: m_can_plat_probe(): free can_net device in case probe fails

2021-02-04 Thread Xulin Sun
The can_net device is allocated through kvzalloc(), if the subsequent probe
cases fail to initialize, it should free the can_net device that has been
successfully allocated before.

To fix below memory leaks call trace:

unreferenced object 0xfc08418b (size 32768):
comm "kworker/0:1", pid 22, jiffies 4294893966 (age 931.976s)
hex dump (first 32 bytes):
63 61 6e 25 64 00 00 00 00 00 00 00 00 00 00 00 can%d...
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
backtrace:
[<3faec9cc>] __kmalloc+0x1a4/0x3e0
[<560b1cad>] kvmalloc_node+0xa0/0xb0
[<93bada32>] alloc_netdev_mqs+0x60/0x380
[<41ddbb53>] alloc_candev_mqs+0x6c/0x14c
[] m_can_class_allocate_dev+0x64/0x18c
[<9fef1617>] m_can_plat_probe+0x2c/0x1f4
[<6fdcc497>] platform_drv_probe+0x5c/0xb0
[] really_probe+0xec/0x41c
[<3ffa5158>] driver_probe_device+0x60/0xf0
[<5986c77e>] __device_attach_driver+0xb0/0x100
[<757823bc>] bus_for_each_drv+0x8c/0xe0
[<59253919>] __device_attach+0xdc/0x180
[<35c2b9f1>] device_initial_probe+0x28/0x34
[<82e2c85c>] bus_probe_device+0xa4/0xb0
[] deferred_probe_work_func+0x7c/0xb0
[<01b85f22>] process_one_work+0x1ec/0x480

Signed-off-by: Xulin Sun 
---
 drivers/net/can/m_can/m_can_platform.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/m_can/m_can_platform.c 
b/drivers/net/can/m_can/m_can_platform.c
index 38ea5e600fb8..0a2655c94018 100644
--- a/drivers/net/can/m_can/m_can_platform.c
+++ b/drivers/net/can/m_can/m_can_platform.c
@@ -67,8 +67,10 @@ static int m_can_plat_probe(struct platform_device *pdev)
return -ENOMEM;
 
priv = devm_kzalloc(>dev, sizeof(*priv), GFP_KERNEL);
-   if (!priv)
-   return -ENOMEM;
+   if (!priv) {
+   ret = -ENOMEM;
+   goto failed_ret;
+   }
 
mcan_class->device_data = priv;
 
@@ -113,7 +115,11 @@ static int m_can_plat_probe(struct platform_device *pdev)
 
ret = m_can_class_register(mcan_class);
 
+   return ret;
+
 failed_ret:
+   free_candev(mcan_class->net);
+
return ret;
 }
 
-- 
2.17.1