diff mbox series

netdevsim: Fix memory leak of nsim_dev->fa_cookie

Message ID 1668234485-27635-1-git-send-email-wangyufen@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series netdevsim: Fix memory leak of nsim_dev->fa_cookie | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 1 blamed authors not CCed: idosch@mellanox.com; 1 maintainers not CCed: idosch@mellanox.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 7 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

wangyufen Nov. 12, 2022, 6:28 a.m. UTC
kmemleak reports this issue:

unreferenced object 0xffff8881bac872d0 (size 8):
  comm "sh", pid 58603, jiffies 4481524462 (age 68.065s)
  hex dump (first 8 bytes):
    04 00 00 00 de ad be ef                          ........
  backtrace:
    [<00000000c80b8577>] __kmalloc+0x49/0x150
    [<000000005292b8c6>] nsim_dev_trap_fa_cookie_write+0xc1/0x210 [netdevsim]
    [<0000000093d78e77>] full_proxy_write+0xf3/0x180
    [<000000005a662c16>] vfs_write+0x1c5/0xaf0
    [<000000007aabf84a>] ksys_write+0xed/0x1c0
    [<000000005f1d2e47>] do_syscall_64+0x3b/0x90
    [<000000006001c6ec>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

The issue occurs in the following scenarios:

nsim_dev_trap_fa_cookie_write()
  kmalloc() fa_cookie
  nsim_dev->fa_cookie = fa_cookie
..
nsim_drv_remove()

nsim_dev->fa_cookie alloced, but the nsim_dev_trap_report_work()
job has not been done, the flow action cookie has not been assigned
to the metadata. To fix, add kfree(nsim_dev->fa_cookie) to
nsim_drv_remove().

Fixes: d3cbb907ae57 ("netdevsim: add ACL trap reporting cookie as a metadata")
Signed-off-by: Wang Yufen <wangyufen@huawei.com>
Cc: Jiri Pirko <jiri@mellanox.com>
---
 drivers/net/netdevsim/dev.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Jakub Kicinski Nov. 15, 2022, 2:50 a.m. UTC | #1
On Sat, 12 Nov 2022 14:28:05 +0800 Wang Yufen wrote:
> nsim_dev_trap_fa_cookie_write()
>   kmalloc() fa_cookie
>   nsim_dev->fa_cookie = fa_cookie
> ..
> nsim_drv_remove()
> 
> nsim_dev->fa_cookie alloced, but the nsim_dev_trap_report_work()
> job has not been done, the flow action cookie has not been assigned
> to the metadata. To fix, add kfree(nsim_dev->fa_cookie) to
> nsim_drv_remove().

I don't see the path thru nsim_dev_trap_report_work() which would free
the fa_cookie.

The fix looks right, but the commit message seems incorrect. Isn't the
leak always there, without any race?
wangyufen Nov. 15, 2022, 3:38 a.m. UTC | #2
在 2022/11/15 10:50, Jakub Kicinski 写道:
> On Sat, 12 Nov 2022 14:28:05 +0800 Wang Yufen wrote:
>> nsim_dev_trap_fa_cookie_write()
>>    kmalloc() fa_cookie
>>    nsim_dev->fa_cookie = fa_cookie
>> ..
>> nsim_drv_remove()
>>
>> nsim_dev->fa_cookie alloced, but the nsim_dev_trap_report_work()
>> job has not been done, the flow action cookie has not been assigned
>> to the metadata. To fix, add kfree(nsim_dev->fa_cookie) to
>> nsim_drv_remove().
> I don't see the path thru nsim_dev_trap_report_work() which would free
> the fa_cookie.
>
> The fix looks right, but the commit message seems incorrect. Isn't the
> leak always there, without any race?

Sorry, I didn't make it clear.

The detailed process of nsim_dev_trap_report_work() is as follows:

nsim_dev_trap_report_work()
   nsim_dev_trap_report_work()
     ...
     devlink_trap_report()
       devlink_trap_report_metadata_set()
       <-- fa_cookie is assigned to metadata->fa_cookie here, and will be freed in net_dm_hw_metadata_free()
Jakub Kicinski Nov. 15, 2022, 4:48 a.m. UTC | #3
On Tue, 15 Nov 2022 11:38:26 +0800 wangyufen wrote:
> Sorry, I didn't make it clear.
> 
> The detailed process of nsim_dev_trap_report_work() is as follows:
> 
> nsim_dev_trap_report_work()
>    nsim_dev_trap_report_work()
>      ...
>      devlink_trap_report()
>        devlink_trap_report_metadata_set()
>        <-- fa_cookie is assigned to metadata->fa_cookie here, and will be freed in net_dm_hw_metadata_free()

What's assigned here and freed in net_dm_hw_metadata_free() is a copy
made with net_dm_hw_metadata_copy(), no?  
Could you double check the whole path?
diff mbox series

Patch

diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index a7880c7..68e56e4 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -1683,6 +1683,7 @@  void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev)
 				  ARRAY_SIZE(nsim_devlink_params));
 	devl_resources_unregister(devlink);
 	kfree(nsim_dev->vfconfigs);
+	kfree(nsim_dev->fa_cookie);
 	devl_unlock(devlink);
 	devlink_free(devlink);
 	dev_set_drvdata(&nsim_bus_dev->dev, NULL);