kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check
@ 2020-06-22 15:35 Colin King
  2020-06-22 18:28 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Colin King @ 2020-06-22 15:35 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Barry Song, Stephen Rothwell, linux-mm
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

kzalloc failures return NULL on out of memory errors, so replace the
IS_ERR_OR_NULL check with the usual null pointer check.  Fix two memory
leaks with on acomp and acomp_ctx by ensuring these objects are free'd
on the error return path.

Addresses-Coverity: ("Resource leak")
Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for hardware acceleration")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 mm/zswap.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index 0d914ba6b4a0..14839cbac7ff 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 		return 0;
 
 	acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
-	if (IS_ERR_OR_NULL(acomp_ctx)) {
+	if (!acomp_ctx) {
 		pr_err("Could not initialize acomp_ctx\n");
 		return -ENOMEM;
 	}
 	acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
-	if (IS_ERR_OR_NULL(acomp)) {
+	if (!acomp) {
 		pr_err("could not alloc crypto acomp %s : %ld\n",
 				pool->tfm_name, PTR_ERR(acomp));
-		return -ENOMEM;
+		goto free_acomp_ctx;
 	}
 	acomp_ctx->acomp = acomp;
 
 	req = acomp_request_alloc(acomp_ctx->acomp);
-	if (IS_ERR_OR_NULL(req)) {
+	if (!req) {
 		pr_err("could not alloc crypto acomp %s : %ld\n",
 		       pool->tfm_name, PTR_ERR(acomp));
-		return -ENOMEM;
+		goto free_acomp;
 	}
 	acomp_ctx->req = req;
 
@@ -462,6 +462,12 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	*per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
 
 	return 0;
+
+free_acomp:
+	kfree(acomp);
+free_acomp_ctx:
+	kfree(acomp_ctx);
+	return -ENOMEM;
 }
 
 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
-- 
2.27.0

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

* Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check
  2020-06-22 15:35 [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check Colin King
@ 2020-06-22 18:28 ` Dan Carpenter
  2020-06-22 19:55   ` Song Bao Hua (Barry Song)
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2020-06-22 18:28 UTC (permalink / raw)
  To: Colin King
  Cc: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Barry Song, Stephen Rothwell, linux-mm, kernel-janitors,
	linux-kernel

On Mon, Jun 22, 2020 at 04:35:46PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> kzalloc failures return NULL on out of memory errors, so replace the
> IS_ERR_OR_NULL check with the usual null pointer check.  Fix two memory
> leaks with on acomp and acomp_ctx by ensuring these objects are free'd
> on the error return path.
> 
> Addresses-Coverity: ("Resource leak")
> Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for hardware acceleration")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  mm/zswap.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 0d914ba6b4a0..14839cbac7ff 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
> @@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
>  		return 0;
>  
>  	acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
> -	if (IS_ERR_OR_NULL(acomp_ctx)) {
> +	if (!acomp_ctx) {
>  		pr_err("Could not initialize acomp_ctx\n");
>  		return -ENOMEM;
>  	}
>  	acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
> -	if (IS_ERR_OR_NULL(acomp)) {
> +	if (!acomp) {

This should be IS_ERR(acomp).  Please preserve the error code.

>  		pr_err("could not alloc crypto acomp %s : %ld\n",
>  				pool->tfm_name, PTR_ERR(acomp));
> -		return -ENOMEM;
> +		goto free_acomp_ctx;
>  	}
>  	acomp_ctx->acomp = acomp;
>  
>  	req = acomp_request_alloc(acomp_ctx->acomp);
> -	if (IS_ERR_OR_NULL(req)) {
> +	if (!req) {
>  		pr_err("could not alloc crypto acomp %s : %ld\n",
>  		       pool->tfm_name, PTR_ERR(acomp));
> -		return -ENOMEM;
> +		goto free_acomp;
>  	}
>  	acomp_ctx->req = req;
>  
> @@ -462,6 +462,12 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
>  	*per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
>  
>  	return 0;
> +
> +free_acomp:
> +	kfree(acomp);

The kfree() isn't correct.  It needs to be:

	crypto_free_acomp(acomp);

> +free_acomp_ctx:
> +	kfree(acomp_ctx);
> +	return -ENOMEM;

regards,
dan carpenter

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

* RE: [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check
  2020-06-22 18:28 ` Dan Carpenter
@ 2020-06-22 19:55   ` Song Bao Hua (Barry Song)
  2020-06-23 11:12     ` Colin Ian King
  0 siblings, 1 reply; 5+ messages in thread
From: Song Bao Hua (Barry Song) @ 2020-06-22 19:55 UTC (permalink / raw)
  To: Dan Carpenter, Colin King
  Cc: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Stephen Rothwell, linux-mm, kernel-janitors, linux-kernel



> -----Original Message-----
> From: Dan Carpenter [mailto:dan.carpenter@oracle.com]
> Sent: Tuesday, June 23, 2020 6:28 AM
> To: Colin King <colin.king@canonical.com>
> Cc: Seth Jennings <sjenning@redhat.com>; Dan Streetman
> <ddstreet@ieee.org>; Vitaly Wool <vitaly.wool@konsulko.com>; Andrew
> Morton <akpm@linux-foundation.org>; Song Bao Hua (Barry Song)
> <song.bao.hua@hisilicon.com>; Stephen Rothwell <sfr@canb.auug.org.au>;
> linux-mm@kvack.org; kernel-janitors@vger.kernel.org;
> linux-kernel@vger.kernel.org
> Subject: Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and
> rework kzalloc failure check
> 
> On Mon, Jun 22, 2020 at 04:35:46PM +0100, Colin King wrote:
> > From: Colin Ian King <colin.king@canonical.com>
> >
> > kzalloc failures return NULL on out of memory errors, so replace the
> > IS_ERR_OR_NULL check with the usual null pointer check.  Fix two memory
> > leaks with on acomp and acomp_ctx by ensuring these objects are free'd
> > on the error return path.
> >
> > Addresses-Coverity: ("Resource leak")
> > Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for
> hardware acceleration")
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>


Colin, thanks for your patch. I am sorry I did the same thing with you here:
https://lkml.org/lkml/2020/6/22/347


> > ---
> >  mm/zswap.c | 16 +++++++++++-----
> >  1 file changed, 11 insertions(+), 5 deletions(-)
> >
> > diff --git a/mm/zswap.c b/mm/zswap.c
> > index 0d914ba6b4a0..14839cbac7ff 100644
> > --- a/mm/zswap.c
> > +++ b/mm/zswap.c
> > @@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int
> cpu, struct hlist_node *node)
> >  		return 0;
> >
> >  	acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
> > -	if (IS_ERR_OR_NULL(acomp_ctx)) {
> > +	if (!acomp_ctx) {
> >  		pr_err("Could not initialize acomp_ctx\n");
> >  		return -ENOMEM;
> >  	}
> >  	acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
> > -	if (IS_ERR_OR_NULL(acomp)) {
> > +	if (!acomp) {
> 
> This should be IS_ERR(acomp).  Please preserve the error code.
> 
> >  		pr_err("could not alloc crypto acomp %s : %ld\n",
> >  				pool->tfm_name, PTR_ERR(acomp));
> > -		return -ENOMEM;
> > +		goto free_acomp_ctx;
> >  	}
> >  	acomp_ctx->acomp = acomp;
> >
> >  	req = acomp_request_alloc(acomp_ctx->acomp);
> > -	if (IS_ERR_OR_NULL(req)) {
> > +	if (!req) {
> >  		pr_err("could not alloc crypto acomp %s : %ld\n",
> >  		       pool->tfm_name, PTR_ERR(acomp));
> > -		return -ENOMEM;
> > +		goto free_acomp;
> >  	}
> >  	acomp_ctx->req = req;
> >
> > @@ -462,6 +462,12 @@ static int zswap_cpu_comp_prepare(unsigned int
> cpu, struct hlist_node *node)
> >  	*per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
> >
> >  	return 0;
> > +
> > +free_acomp:
> > +	kfree(acomp);
> 
> The kfree() isn't correct.  It needs to be:
> 
> 	crypto_free_acomp(acomp);
> 
> > +free_acomp_ctx:
> > +	kfree(acomp_ctx);
> > +	return -ENOMEM;
> 
> regards,
> dan carpenter

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

* Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check
  2020-06-22 19:55   ` Song Bao Hua (Barry Song)
@ 2020-06-23 11:12     ` Colin Ian King
  2020-06-23 14:02       ` Vitaly Wool
  0 siblings, 1 reply; 5+ messages in thread
From: Colin Ian King @ 2020-06-23 11:12 UTC (permalink / raw)
  To: Song Bao Hua (Barry Song), Dan Carpenter
  Cc: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Stephen Rothwell, linux-mm, kernel-janitors, linux-kernel

On 22/06/2020 20:55, Song Bao Hua (Barry Song) wrote:
> 
> 
>> -----Original Message-----
>> From: Dan Carpenter [mailto:dan.carpenter@oracle.com]
>> Sent: Tuesday, June 23, 2020 6:28 AM
>> To: Colin King <colin.king@canonical.com>
>> Cc: Seth Jennings <sjenning@redhat.com>; Dan Streetman
>> <ddstreet@ieee.org>; Vitaly Wool <vitaly.wool@konsulko.com>; Andrew
>> Morton <akpm@linux-foundation.org>; Song Bao Hua (Barry Song)
>> <song.bao.hua@hisilicon.com>; Stephen Rothwell <sfr@canb.auug.org.au>;
>> linux-mm@kvack.org; kernel-janitors@vger.kernel.org;
>> linux-kernel@vger.kernel.org
>> Subject: Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and
>> rework kzalloc failure check
>>
>> On Mon, Jun 22, 2020 at 04:35:46PM +0100, Colin King wrote:
>>> From: Colin Ian King <colin.king@canonical.com>
>>>
>>> kzalloc failures return NULL on out of memory errors, so replace the
>>> IS_ERR_OR_NULL check with the usual null pointer check.  Fix two memory
>>> leaks with on acomp and acomp_ctx by ensuring these objects are free'd
>>> on the error return path.
>>>
>>> Addresses-Coverity: ("Resource leak")
>>> Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for
>> hardware acceleration")
>>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> 
> 
> Colin, thanks for your patch. I am sorry I did the same thing with you here:
> https://lkml.org/lkml/2020/6/22/347

Thanks for fixing this correctly, I ran out of time yesterday to re-do
the fix.

Colin

> 
> 
>>> ---
>>>  mm/zswap.c | 16 +++++++++++-----
>>>  1 file changed, 11 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/mm/zswap.c b/mm/zswap.c
>>> index 0d914ba6b4a0..14839cbac7ff 100644
>>> --- a/mm/zswap.c
>>> +++ b/mm/zswap.c
>>> @@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int
>> cpu, struct hlist_node *node)
>>>  		return 0;
>>>
>>>  	acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
>>> -	if (IS_ERR_OR_NULL(acomp_ctx)) {
>>> +	if (!acomp_ctx) {
>>>  		pr_err("Could not initialize acomp_ctx\n");
>>>  		return -ENOMEM;
>>>  	}
>>>  	acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
>>> -	if (IS_ERR_OR_NULL(acomp)) {
>>> +	if (!acomp) {
>>
>> This should be IS_ERR(acomp).  Please preserve the error code.
>>
>>>  		pr_err("could not alloc crypto acomp %s : %ld\n",
>>>  				pool->tfm_name, PTR_ERR(acomp));
>>> -		return -ENOMEM;
>>> +		goto free_acomp_ctx;
>>>  	}
>>>  	acomp_ctx->acomp = acomp;
>>>
>>>  	req = acomp_request_alloc(acomp_ctx->acomp);
>>> -	if (IS_ERR_OR_NULL(req)) {
>>> +	if (!req) {
>>>  		pr_err("could not alloc crypto acomp %s : %ld\n",
>>>  		       pool->tfm_name, PTR_ERR(acomp));
>>> -		return -ENOMEM;
>>> +		goto free_acomp;
>>>  	}
>>>  	acomp_ctx->req = req;
>>>
>>> @@ -462,6 +462,12 @@ static int zswap_cpu_comp_prepare(unsigned int
>> cpu, struct hlist_node *node)
>>>  	*per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
>>>
>>>  	return 0;
>>> +
>>> +free_acomp:
>>> +	kfree(acomp);
>>
>> The kfree() isn't correct.  It needs to be:
>>
>> 	crypto_free_acomp(acomp);
>>
>>> +free_acomp_ctx:
>>> +	kfree(acomp_ctx);
>>> +	return -ENOMEM;
>>
>> regards,
>> dan carpenter
> 

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

* Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check
  2020-06-23 11:12     ` Colin Ian King
@ 2020-06-23 14:02       ` Vitaly Wool
  0 siblings, 0 replies; 5+ messages in thread
From: Vitaly Wool @ 2020-06-23 14:02 UTC (permalink / raw)
  To: Colin Ian King
  Cc: Song Bao Hua (Barry Song),
	Dan Carpenter, Seth Jennings, Dan Streetman, Andrew Morton,
	Stephen Rothwell, linux-mm, kernel-janitors, linux-kernel

On Tue, Jun 23, 2020, 1:12 PM Colin Ian King <colin.king@canonical.com> wrote:
>
> On 22/06/2020 20:55, Song Bao Hua (Barry Song) wrote:
> >
> >
> >> -----Original Message-----
> >> From: Dan Carpenter [mailto:dan.carpenter@oracle.com]
> >> Sent: Tuesday, June 23, 2020 6:28 AM
> >> To: Colin King <colin.king@canonical.com>
> >> Cc: Seth Jennings <sjenning@redhat.com>; Dan Streetman
> >> <ddstreet@ieee.org>; Vitaly Wool <vitaly.wool@konsulko.com>; Andrew
> >> Morton <akpm@linux-foundation.org>; Song Bao Hua (Barry Song)
> >> <song.bao.hua@hisilicon.com>; Stephen Rothwell <sfr@canb.auug.org.au>;
> >> linux-mm@kvack.org; kernel-janitors@vger.kernel.org;
> >> linux-kernel@vger.kernel.org
> >> Subject: Re: [PATCH][next] mm/zswap: fix a couple of memory leaks and
> >> rework kzalloc failure check
> >>
> >> On Mon, Jun 22, 2020 at 04:35:46PM +0100, Colin King wrote:
> >>> From: Colin Ian King <colin.king@canonical.com>
> >>>
> >>> kzalloc failures return NULL on out of memory errors, so replace the
> >>> IS_ERR_OR_NULL check with the usual null pointer check.  Fix two memory
> >>> leaks with on acomp and acomp_ctx by ensuring these objects are free'd
> >>> on the error return path.
> >>>
> >>> Addresses-Coverity: ("Resource leak")
> >>> Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for
> >> hardware acceleration")
> >>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> >
> >
> > Colin, thanks for your patch. I am sorry I did the same thing with you here:
> > https://lkml.org/lkml/2020/6/22/347
>
> Thanks for fixing this correctly, I ran out of time yesterday to re-do
> the fix.
>
> Colin

I think this has gotten out of hand. Barry, could you please come up
with a replacement for the initial patch rather than doing it
incrementally?

Thanks,
   Vitaly

>
> >
> >
> >>> ---
> >>>  mm/zswap.c | 16 +++++++++++-----
> >>>  1 file changed, 11 insertions(+), 5 deletions(-)
> >>>
> >>> diff --git a/mm/zswap.c b/mm/zswap.c
> >>> index 0d914ba6b4a0..14839cbac7ff 100644
> >>> --- a/mm/zswap.c
> >>> +++ b/mm/zswap.c
> >>> @@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int
> >> cpu, struct hlist_node *node)
> >>>             return 0;
> >>>
> >>>     acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
> >>> -   if (IS_ERR_OR_NULL(acomp_ctx)) {
> >>> +   if (!acomp_ctx) {
> >>>             pr_err("Could not initialize acomp_ctx\n");
> >>>             return -ENOMEM;
> >>>     }
> >>>     acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
> >>> -   if (IS_ERR_OR_NULL(acomp)) {
> >>> +   if (!acomp) {
> >>
> >> This should be IS_ERR(acomp).  Please preserve the error code.
> >>
> >>>             pr_err("could not alloc crypto acomp %s : %ld\n",
> >>>                             pool->tfm_name, PTR_ERR(acomp));
> >>> -           return -ENOMEM;
> >>> +           goto free_acomp_ctx;
> >>>     }
> >>>     acomp_ctx->acomp = acomp;
> >>>
> >>>     req = acomp_request_alloc(acomp_ctx->acomp);
> >>> -   if (IS_ERR_OR_NULL(req)) {
> >>> +   if (!req) {
> >>>             pr_err("could not alloc crypto acomp %s : %ld\n",
> >>>                    pool->tfm_name, PTR_ERR(acomp));
> >>> -           return -ENOMEM;
> >>> +           goto free_acomp;
> >>>     }
> >>>     acomp_ctx->req = req;
> >>>
> >>> @@ -462,6 +462,12 @@ static int zswap_cpu_comp_prepare(unsigned int
> >> cpu, struct hlist_node *node)
> >>>     *per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
> >>>
> >>>     return 0;
> >>> +
> >>> +free_acomp:
> >>> +   kfree(acomp);
> >>
> >> The kfree() isn't correct.  It needs to be:
> >>
> >>      crypto_free_acomp(acomp);
> >>
> >>> +free_acomp_ctx:
> >>> +   kfree(acomp_ctx);
> >>> +   return -ENOMEM;
> >>
> >> regards,
> >> dan carpenter
> >
>

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

end of thread, other threads:[~2020-06-23 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-22 15:35 [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check Colin King
2020-06-22 18:28 ` Dan Carpenter
2020-06-22 19:55   ` Song Bao Hua (Barry Song)
2020-06-23 11:12     ` Colin Ian King
2020-06-23 14:02       ` Vitaly Wool

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