linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id
@ 2020-04-16 12:33 Alexandru Ardelean
  2020-04-18 15:25 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandru Ardelean @ 2020-04-16 12:33 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: jic23, Alexandru Ardelean

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.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 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);
 	}
 
-- 
2.17.1


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

* Re: [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id
  2020-04-16 12:33 [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id Alexandru Ardelean
@ 2020-04-18 15:25 ` Jonathan Cameron
  2020-04-18 16:28   ` Ardelean, Alexandru
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2020-04-18 15:25 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, linux-kernel

On Thu, 16 Apr 2020 15:33:31 +0300
Alexandru Ardelean <alexandru.ardelean@analog.com> 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.

Jonathan

> 
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---
>  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);
>  	}
>  


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

* Re: [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id
  2020-04-18 15:25 ` Jonathan Cameron
@ 2020-04-18 16:28   ` Ardelean, Alexandru
  0 siblings, 0 replies; 3+ messages in thread
From: Ardelean, Alexandru @ 2020-04-18 16:28 UTC (permalink / raw)
  To: jic23; +Cc: linux-kernel, linux-iio

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@analog.com> 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@analog.com>
> > ---
> >  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);
> >  	}
> >  

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

end of thread, other threads:[~2020-04-18 16:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16 12:33 [PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id Alexandru Ardelean
2020-04-18 15:25 ` Jonathan Cameron
2020-04-18 16:28   ` Ardelean, Alexandru

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