* [PATCH] stm class: fix device leak in open error path
@ 2016-11-01 10:44 Johan Hovold
2016-11-17 14:38 ` Johan Hovold
2016-11-18 11:55 ` Alexander Shishkin
0 siblings, 2 replies; 4+ messages in thread
From: Johan Hovold @ 2016-11-01 10:44 UTC (permalink / raw)
To: Alexander Shishkin; +Cc: linux-kernel, Johan Hovold
Make sure to drop the reference taken by class_find_device() also on
allocation errors in open().
Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for...")
Signed-off-by: Johan Hovold <johan@kernel.org>
---
drivers/hwtracing/stm/core.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 51f81d64ca37..47811f5c9bc9 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -368,8 +368,10 @@ static int stm_char_open(struct inode *inode, struct file *file)
return -ENODEV;
stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
- if (!stmf)
- return -ENOMEM;
+ if (!stmf) {
+ err = -ENOMEM;
+ goto err_put_device;
+ }
stm_output_init(&stmf->output);
stmf->stm = to_stm_device(dev);
@@ -382,9 +384,10 @@ static int stm_char_open(struct inode *inode, struct file *file)
return nonseekable_open(inode, file);
err_free:
+ kfree(stmf);
+err_put_device:
/* matches class_find_device() above */
put_device(dev);
- kfree(stmf);
return err;
}
--
2.7.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] stm class: fix device leak in open error path
2016-11-01 10:44 [PATCH] stm class: fix device leak in open error path Johan Hovold
@ 2016-11-17 14:38 ` Johan Hovold
2016-11-18 11:55 ` Alexander Shishkin
1 sibling, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2016-11-17 14:38 UTC (permalink / raw)
To: Alexander Shishkin; +Cc: linux-kernel, Johan Hovold
On Tue, Nov 01, 2016 at 11:44:54AM +0100, Johan Hovold wrote:
> Make sure to drop the reference taken by class_find_device() also on
> allocation errors in open().
>
> Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for...")
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---
> drivers/hwtracing/stm/core.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
> index 51f81d64ca37..47811f5c9bc9 100644
> --- a/drivers/hwtracing/stm/core.c
> +++ b/drivers/hwtracing/stm/core.c
> @@ -368,8 +368,10 @@ static int stm_char_open(struct inode *inode, struct file *file)
> return -ENODEV;
>
> stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
> - if (!stmf)
> - return -ENOMEM;
> + if (!stmf) {
> + err = -ENOMEM;
> + goto err_put_device;
> + }
>
> stm_output_init(&stmf->output);
> stmf->stm = to_stm_device(dev);
> @@ -382,9 +384,10 @@ static int stm_char_open(struct inode *inode, struct file *file)
> return nonseekable_open(inode, file);
>
> err_free:
> + kfree(stmf);
> +err_put_device:
> /* matches class_find_device() above */
> put_device(dev);
> - kfree(stmf);
>
> return err;
> }
Any comments to this one, Alexander?
Thanks,
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] stm class: fix device leak in open error path
2016-11-01 10:44 [PATCH] stm class: fix device leak in open error path Johan Hovold
2016-11-17 14:38 ` Johan Hovold
@ 2016-11-18 11:55 ` Alexander Shishkin
2016-11-18 13:01 ` Johan Hovold
1 sibling, 1 reply; 4+ messages in thread
From: Alexander Shishkin @ 2016-11-18 11:55 UTC (permalink / raw)
To: Johan Hovold; +Cc: linux-kernel, Johan Hovold
Johan Hovold <johan@kernel.org> writes:
> Make sure to drop the reference taken by class_find_device() also on
> allocation errors in open().
>
> Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for...")
> Signed-off-by: Johan Hovold <johan@kernel.org>
Good catch, thanks! I'm going to change it a bit to look like this
if you don't mind:
diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 51f81d64ca..cf6c9ea14c 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -361,7 +361,7 @@ static int stm_char_open(struct inode *inode, struct file *file)
struct stm_file *stmf;
struct device *dev;
unsigned int major = imajor(inode);
- int err = -ENODEV;
+ int err = -ENOMEM;
dev = class_find_device(&stm_class, NULL, &major, major_match);
if (!dev)
@@ -369,8 +369,9 @@ static int stm_char_open(struct inode *inode, struct file *file)
stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
if (!stmf)
- return -ENOMEM;
+ goto err_put_device;
+ err = -ENODEV;
stm_output_init(&stmf->output);
stmf->stm = to_stm_device(dev);
@@ -382,9 +383,10 @@ static int stm_char_open(struct inode *inode, struct file *file)
return nonseekable_open(inode, file);
err_free:
+ kfree(stmf);
+err_put_device:
/* matches class_find_device() above */
put_device(dev);
- kfree(stmf);
return err;
}
Regards,
--
Alex
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] stm class: fix device leak in open error path
2016-11-18 11:55 ` Alexander Shishkin
@ 2016-11-18 13:01 ` Johan Hovold
0 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2016-11-18 13:01 UTC (permalink / raw)
To: Alexander Shishkin; +Cc: Johan Hovold, linux-kernel
On Fri, Nov 18, 2016 at 01:55:58PM +0200, Alexander Shishkin wrote:
> Johan Hovold <johan@kernel.org> writes:
>
> > Make sure to drop the reference taken by class_find_device() also on
> > allocation errors in open().
> >
> > Fixes: 7bd1d4093c2f ("stm class: Introduce an abstraction for...")
> > Signed-off-by: Johan Hovold <johan@kernel.org>
>
> Good catch, thanks! I'm going to change it a bit to look like this
> if you don't mind:
>
> diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
> index 51f81d64ca..cf6c9ea14c 100644
> --- a/drivers/hwtracing/stm/core.c
> +++ b/drivers/hwtracing/stm/core.c
> @@ -361,7 +361,7 @@ static int stm_char_open(struct inode *inode, struct file *file)
> struct stm_file *stmf;
> struct device *dev;
> unsigned int major = imajor(inode);
> - int err = -ENODEV;
> + int err = -ENOMEM;
>
> dev = class_find_device(&stm_class, NULL, &major, major_match);
> if (!dev)
> @@ -369,8 +369,9 @@ static int stm_char_open(struct inode *inode, struct file *file)
>
> stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
> if (!stmf)
> - return -ENOMEM;
> + goto err_put_device;
>
> + err = -ENODEV;
> stm_output_init(&stmf->output);
> stmf->stm = to_stm_device(dev);
Sure. I find it more readable to set the errno in the error path, but I
don't mind.
Thanks,
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-11-18 13:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-01 10:44 [PATCH] stm class: fix device leak in open error path Johan Hovold
2016-11-17 14:38 ` Johan Hovold
2016-11-18 11:55 ` Alexander Shishkin
2016-11-18 13:01 ` Johan Hovold
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.