diff mbox series

[2/6] nvmem: fix memory leak in error path

Message ID 20200217195435.9309-3-brgl@bgdev.pl
State New
Headers show
Series nvmem/gpio: fix resource management | expand

Commit Message

Bartosz Golaszewski Feb. 17, 2020, 7:54 p.m. UTC
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

We need to remove the ida mapping when returning from nvmem_register()
with an error.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/nvmem/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Srinivas Kandagatla Feb. 18, 2020, 9:31 a.m. UTC | #1
On 17/02/2020 19:54, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> 
> We need to remove the ida mapping when returning from nvmem_register()
> with an error.
> 
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---

Applied thanks,
--srini
Srinivas Kandagatla Feb. 18, 2020, 9:42 a.m. UTC | #2
On 17/02/2020 19:54, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> 
> We need to remove the ida mapping when returning from nvmem_register()
> with an error.
> 
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Was too quick in my last reply..

> ---
>   drivers/nvmem/core.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index ef326f243f36..b0be03d5f240 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -353,7 +353,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>   		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
>   						    GPIOD_OUT_HIGH);
>   	if (IS_ERR(nvmem->wp_gpio))
> -		return ERR_CAST(nvmem->wp_gpio);
> +		goto err_ida_remove;

Looks like this is adding  nvmem leak here.
May be something like this should help:


if (IS_ERR(nvmem->wp_gpio)) {
	rval =  ERR_CAST(nvmem->wp_gpio);
	ida_simple_remove(&nvmem_ida, nvmem->id);
	kfree(nvmem);
	return rval;

}

--srini
>   
>   
>   	kref_init(&nvmem->refcnt);
> @@ -430,6 +430,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>   	device_del(&nvmem->dev);
>   err_put_device:
>   	put_device(&nvmem->dev);
> +err_ida_remove:
> +	ida_simple_remove(&nvmem_ida, nvmem->id);
>   
>   	return ERR_PTR(rval);
>   }
>
Bartosz Golaszewski Feb. 18, 2020, 9:44 a.m. UTC | #3
wt., 18 lut 2020 o 10:42 Srinivas Kandagatla
<srinivas.kandagatla@linaro.org> napisaƂ(a):
>
>
>
> On 17/02/2020 19:54, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> >
> > We need to remove the ida mapping when returning from nvmem_register()
> > with an error.
> >
> > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> Was too quick in my last reply..
>
> > ---
> >   drivers/nvmem/core.c | 4 +++-
> >   1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> > index ef326f243f36..b0be03d5f240 100644
> > --- a/drivers/nvmem/core.c
> > +++ b/drivers/nvmem/core.c
> > @@ -353,7 +353,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> >               nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
> >                                                   GPIOD_OUT_HIGH);
> >       if (IS_ERR(nvmem->wp_gpio))
> > -             return ERR_CAST(nvmem->wp_gpio);
> > +             goto err_ida_remove;
>
> Looks like this is adding  nvmem leak here.
> May be something like this should help:
>
>
> if (IS_ERR(nvmem->wp_gpio)) {
>         rval =  ERR_CAST(nvmem->wp_gpio);
>         ida_simple_remove(&nvmem_ida, nvmem->id);
>         kfree(nvmem);
>         return rval;
>
> }
>

Srinivas,

I just sent a v2 of this series that addresses it as well. Please
don't apply v1 yet.

Bartosz
diff mbox series

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index ef326f243f36..b0be03d5f240 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -353,7 +353,7 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
 						    GPIOD_OUT_HIGH);
 	if (IS_ERR(nvmem->wp_gpio))
-		return ERR_CAST(nvmem->wp_gpio);
+		goto err_ida_remove;
 
 
 	kref_init(&nvmem->refcnt);
@@ -430,6 +430,8 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	device_del(&nvmem->dev);
 err_put_device:
 	put_device(&nvmem->dev);
+err_ida_remove:
+	ida_simple_remove(&nvmem_ida, nvmem->id);
 
 	return ERR_PTR(rval);
 }