2022-07-15 07:50:47

by Gaosheng Cui

[permalink] [raw]
Subject: [PATCH -next] mtd/ftl: Fix memleak in ftl_add_mtd()

When add_mtd_blktrans_dev failed, partition's memory will be freed
by kfree, but there are some structure members that are allocated
for memory independently, such as partition->VirtualBlockMap,
partition->EUNInfo, partition->ZferInfo, and partition->bam_cache,
so kfree(partition) may cause memory leaks, using ftl_freepart(partition)
will fix it.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Gaosheng Cui <[email protected]>
---
drivers/mtd/ftl.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
index f655d2905270..200271bdc7aa 100644
--- a/drivers/mtd/ftl.c
+++ b/drivers/mtd/ftl.c
@@ -1031,6 +1031,7 @@ static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
partition->mbd.devnum = -1;
if (!add_mtd_blktrans_dev(&partition->mbd))
return;
+ ftl_freepart(partition);
}

kfree(partition);
--
2.25.1


2022-08-18 01:50:23

by Gaosheng Cui

[permalink] [raw]
Subject: Re: [PATCH -next] mtd/ftl: Fix memleak in ftl_add_mtd()

PING

?? 2022/7/15 15:11, Gaosheng Cui д??:
> When add_mtd_blktrans_dev failed, partition's memory will be freed
> by kfree, but there are some structure members that are allocated
> for memory independently, such as partition->VirtualBlockMap,
> partition->EUNInfo, partition->ZferInfo, and partition->bam_cache,
> so kfree(partition) may cause memory leaks, using ftl_freepart(partition)
> will fix it.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Gaosheng Cui <[email protected]>
> ---
> drivers/mtd/ftl.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
> index f655d2905270..200271bdc7aa 100644
> --- a/drivers/mtd/ftl.c
> +++ b/drivers/mtd/ftl.c
> @@ -1031,6 +1031,7 @@ static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
> partition->mbd.devnum = -1;
> if (!add_mtd_blktrans_dev(&partition->mbd))
> return;
> + ftl_freepart(partition);
> }
>
> kfree(partition);

2022-08-18 07:21:42

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH -next] mtd/ftl: Fix memleak in ftl_add_mtd()

----- Ursprüngliche Mail -----
> Von: "cuigaosheng" <[email protected]>
>
> PING

There seems to be a problem with your caps lock key. ;-)

> 在 2022/7/15 15:11, Gaosheng Cui 写道:
>> When add_mtd_blktrans_dev failed, partition's memory will be freed
>> by kfree, but there are some structure members that are allocated
>> for memory independently, such as partition->VirtualBlockMap,
>> partition->EUNInfo, partition->ZferInfo, and partition->bam_cache,
>> so kfree(partition) may cause memory leaks, using ftl_freepart(partition)
>> will fix it.

Is this leak observable via kmemleak or such?
I always thought ->remove_dev() unconditionally do a cleanup later.

>> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
>> Signed-off-by: Gaosheng Cui <[email protected]>
>> ---
>> drivers/mtd/ftl.c | 1 +
>> 1 file changed, 1 insertion(+)
>>
>> diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
>> index f655d2905270..200271bdc7aa 100644
>> --- a/drivers/mtd/ftl.c
>> +++ b/drivers/mtd/ftl.c
>> @@ -1031,6 +1031,7 @@ static void ftl_add_mtd(struct mtd_blktrans_ops *tr,
>> struct mtd_info *mtd)
>> partition->mbd.devnum = -1;
>> if (!add_mtd_blktrans_dev(&partition->mbd))
>> return;
>> + ftl_freepart(partition);
>> }
>>
> > kfree(partition);

2022-08-19 02:45:49

by Gaosheng Cui

[permalink] [raw]
Subject: Re: [PATCH -next] mtd/ftl: Fix memleak in ftl_add_mtd()

> There seems to be a problem with your caps lock key. ;-)

I wil adjust my caps lock key, Thanks for reminding.

> Is this leak observable via kmemleak or such?
> I always thought ->remove_dev() unconditionally do a cleanup later.

When I'm working on other issues with mtd,I needed to analyze the mtd driver
initialization process,during reading the code, refer to other driver
implementation,I found this problem, not kmemleak. Thanks.

在 2022/8/18 14:59, Richard Weinberger 写道:
> ----- Ursprüngliche Mail -----
>> Von: "cuigaosheng" <[email protected]>
>>
>> PING
> There seems to be a problem with your caps lock key. ;-)
>
>> 在 2022/7/15 15:11, Gaosheng Cui 写道:
>>> When add_mtd_blktrans_dev failed, partition's memory will be freed
>>> by kfree, but there are some structure members that are allocated
>>> for memory independently, such as partition->VirtualBlockMap,
>>> partition->EUNInfo, partition->ZferInfo, and partition->bam_cache,
>>> so kfree(partition) may cause memory leaks, using ftl_freepart(partition)
>>> will fix it.
> Is this leak observable via kmemleak or such?
> I always thought ->remove_dev() unconditionally do a cleanup later.
>
>>> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
>>> Signed-off-by: Gaosheng Cui <[email protected]>
>>> ---
>>> drivers/mtd/ftl.c | 1 +
>>> 1 file changed, 1 insertion(+)
>>>
>>> diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
>>> index f655d2905270..200271bdc7aa 100644
>>> --- a/drivers/mtd/ftl.c
>>> +++ b/drivers/mtd/ftl.c
>>> @@ -1031,6 +1031,7 @@ static void ftl_add_mtd(struct mtd_blktrans_ops *tr,
>>> struct mtd_info *mtd)
>>> partition->mbd.devnum = -1;
>>> if (!add_mtd_blktrans_dev(&partition->mbd))
>>> return;
>>> + ftl_freepart(partition);
>>> }
>>>
>>> kfree(partition);
> .