lkml.org 
[lkml]   [2020]   [Apr]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] iio: core: fail early in iio_device_alloc() if we can't get a device id
Date
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
\
 
 \ /
  Last update: 2020-04-16 14:33    [W:0.213 / U:1.136 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site