linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcache: Fix potential memory leak in register_bcache()
@ 2020-11-09  9:28 Tiezhu Yang
  2020-11-11  8:24 ` Coly Li
  0 siblings, 1 reply; 2+ messages in thread
From: Tiezhu Yang @ 2020-11-09  9:28 UTC (permalink / raw)
  To: Coly Li, Kent Overstreet; +Cc: linux-bcache, linux-kernel, Xuefeng Li

Call kfree() in the error path to free the memory allocated by kzalloc().

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 drivers/md/bcache/super.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 46a0013..af51574 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2593,8 +2593,10 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
 		ret = register_bdev(sb, sb_disk, bdev, dc);
 		mutex_unlock(&bch_register_lock);
 		/* blkdev_put() will be called in cached_dev_free() */
-		if (ret < 0)
+		if (ret < 0) {
+			kfree(dc);
 			goto out_free_sb;
+		}
 	} else {
 		struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
 
@@ -2602,8 +2604,10 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
 			goto out_put_sb_page;
 
 		/* blkdev_put() will be called in bch_cache_release() */
-		if (register_cache(sb, sb_disk, bdev, ca) != 0)
+		if (register_cache(sb, sb_disk, bdev, ca) != 0) {
+			kfree(ca);
 			goto out_free_sb;
+		}
 	}
 
 done:
-- 
2.1.0


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

* Re: [PATCH] bcache: Fix potential memory leak in register_bcache()
  2020-11-09  9:28 [PATCH] bcache: Fix potential memory leak in register_bcache() Tiezhu Yang
@ 2020-11-11  8:24 ` Coly Li
  0 siblings, 0 replies; 2+ messages in thread
From: Coly Li @ 2020-11-11  8:24 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: Kent Overstreet, linux-bcache, linux-kernel, Xuefeng Li

On 2020/11/9 17:28, Tiezhu Yang wrote:
> Call kfree() in the error path to free the memory allocated by kzalloc().
> 
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Hi Tiezhu,

NACK, dc is freed in bch_cached_dev_release() and ca is freed in
bch_cache_release().

Indeed you are not the first or second who tried to fix here. The error
handling code path to release the memory objects are implicit.

Thanks.

Coly Li


> ---
>  drivers/md/bcache/super.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
> index 46a0013..af51574 100644
> --- a/drivers/md/bcache/super.c
> +++ b/drivers/md/bcache/super.c
> @@ -2593,8 +2593,10 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
>  		ret = register_bdev(sb, sb_disk, bdev, dc);
>  		mutex_unlock(&bch_register_lock);
>  		/* blkdev_put() will be called in cached_dev_free() */
> -		if (ret < 0)
> +		if (ret < 0) {
> +			kfree(dc);
>  			goto out_free_sb;
> +		}
>  	} else {
>  		struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
>  
> @@ -2602,8 +2604,10 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
>  			goto out_put_sb_page;
>  
>  		/* blkdev_put() will be called in bch_cache_release() */
> -		if (register_cache(sb, sb_disk, bdev, ca) != 0)
> +		if (register_cache(sb, sb_disk, bdev, ca) != 0) {
> +			kfree(ca);
>  			goto out_free_sb;
> +		}
>  	}
>  
>  done:
> 


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

end of thread, other threads:[~2020-11-11  8:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-09  9:28 [PATCH] bcache: Fix potential memory leak in register_bcache() Tiezhu Yang
2020-11-11  8:24 ` Coly Li

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