Re: [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id

From: Ardelean, Alexandru
Date: Sat Apr 18 2020 - 12:28:45 EST


On Sat, 2020-04-18 at 16:25 +0100, Jonathan Cameron wrote:
> [External]
>
> On Thu, 16 Apr 2020 15:33:31 +0300
> Alexandru Ardelean <alexandru.ardelean@xxxxxxxxxx> wrote:
>
> > This change moves the 'ida_simple_get()' call to be the first one in
> > iio_device_alloc(). It cleans up the error path a bit as we don't need to
> > call any kfree(dev) anymore. We allocate an IIO device only if we have
> > managed to obtain a device ID.
>
> We just threw away an ID if the kzalloc then fails (or am I missing
> something?)
> With that fixed I can't see this as being much of an improvement.
> Either way one allocation needs to be tidied up.

oops,
the ida thingi eluded me when i wrote this;
apologies for the noise


>
> Jonathan
>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@xxxxxxxxxx>
> > ---
> > drivers/iio/industrialio-core.c | 19 ++++++++++---------
> > 1 file changed, 10 insertions(+), 9 deletions(-)
> >
> > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-
> > core.c
> > index f4daf19f2a3b..7c1d8a3ab2f3 100644
> > --- a/drivers/iio/industrialio-core.c
> > +++ b/drivers/iio/industrialio-core.c
> > @@ -1494,6 +1494,14 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
> > {
> > struct iio_dev *dev;
> > size_t alloc_size;
> > + int id;
> > +
> > + id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
> > + if (id < 0) {
> > + /* cannot use a dev_err as the name isn't available */
> > + pr_err("failed to get device id\n");
> > + return NULL;
> > + }
> >
> > alloc_size = sizeof(struct iio_dev);
> > if (sizeof_priv) {
> > @@ -1506,6 +1514,8 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
> > dev = kzalloc(alloc_size, GFP_KERNEL);
> >
> > if (dev) {
> > + dev->id = id;
> > + dev_set_name(&dev->dev, "iio:device%d", dev->id);
> > dev->dev.groups = dev->groups;
> > dev->dev.type = &iio_device_type;
> > dev->dev.bus = &iio_bus_type;
> > @@ -1514,15 +1524,6 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
> > mutex_init(&dev->mlock);
> > mutex_init(&dev->info_exist_lock);
> > INIT_LIST_HEAD(&dev->channel_attr_list);
> > -
> > - dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
> > - if (dev->id < 0) {
> > - /* cannot use a dev_err as the name isn't available */
> > - pr_err("failed to get device id\n");
> > - kfree(dev);
> > - return NULL;
> > - }
> > - dev_set_name(&dev->dev, "iio:device%d", dev->id);
> > INIT_LIST_HEAD(&dev->buffer_list);
> > }
> >