block; release bip in a right way in error path
diff mbox series

Message ID 20200623140653.4226-1-cgxu519@mykernel.net
State New
Headers show
Series
  • block; release bip in a right way in error path
Related show

Commit Message

Chengguang Xu June 23, 2020, 2:06 p.m. UTC
Release bip using kfree() in error path when that was allocated
by kmalloc().

Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
---
 block/bio-integrity.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Christoph Hellwig June 24, 2020, 6:50 a.m. UTC | #1
On Tue, Jun 23, 2020 at 10:06:53PM +0800, Chengguang Xu wrote:
> Release bip using kfree() in error path when that was allocated
> by kmalloc().
> 
> Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
> ---
>  block/bio-integrity.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/block/bio-integrity.c b/block/bio-integrity.c
> index 23632a33ed39..538c8dc8840a 100644
> --- a/block/bio-integrity.c
> +++ b/block/bio-integrity.c
> @@ -78,7 +78,11 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
>  
>  	return bip;
>  err:
> -	mempool_free(bip, &bs->bio_integrity_pool);
> +	if (bs && mempool_initialized(&bs->bio_integrity_pool))
> +		mempool_free(bip, &bs->bio_integrity_pool);
> +	else
> +		kfree(bip);
> +
>  	return ERR_PTR(-ENOMEM);

How about factoring out a __bio_integrity_free helper to not duplicate
this logic?
Chengguang Xu June 24, 2020, 8:01 a.m. UTC | #2
On 6/24/20 2:50 PM, Christoph Hellwig wrote:
> On Tue, Jun 23, 2020 at 10:06:53PM +0800, Chengguang Xu wrote:
>> Release bip using kfree() in error path when that was allocated
>> by kmalloc().
>>
>> Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
>> ---
>>   block/bio-integrity.c | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/block/bio-integrity.c b/block/bio-integrity.c
>> index 23632a33ed39..538c8dc8840a 100644
>> --- a/block/bio-integrity.c
>> +++ b/block/bio-integrity.c
>> @@ -78,7 +78,11 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
>>   
>>   	return bip;
>>   err:
>> -	mempool_free(bip, &bs->bio_integrity_pool);
>> +	if (bs && mempool_initialized(&bs->bio_integrity_pool))
>> +		mempool_free(bip, &bs->bio_integrity_pool);
>> +	else
>> +		kfree(bip);
>> +
>>   	return ERR_PTR(-ENOMEM);
> 
> How about factoring out a __bio_integrity_free helper to not duplicate
> this logic?
> 

Yeah, that's better, thanks for the suggestion.


cgxu

Patch
diff mbox series

diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index 23632a33ed39..538c8dc8840a 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -78,7 +78,11 @@  struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 
 	return bip;
 err:
-	mempool_free(bip, &bs->bio_integrity_pool);
+	if (bs && mempool_initialized(&bs->bio_integrity_pool))
+		mempool_free(bip, &bs->bio_integrity_pool);
+	else
+		kfree(bip);
+
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_integrity_alloc);