Re: [PATCH] stm class: fix device leak in open error path

From: Johan Hovold
Date: Fri Nov 18 2016 - 08:01:24 EST


On Fri, Nov 18, 2016 at 01:55:58PM +0200, Alexander Shishkin wrote:
> Johan Hovold <johan@xxxxxxxxxx> 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@xxxxxxxxxx>
>
> 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