2023-07-20 21:15:00

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 0/8] iio: core: A few code cleanups and documentation fixes

Just set of almost unrelated to each other cleanups against IIO
core implementation.

The positive LoCs diffstat due to first patch that adds a lot of
documentation for the new added macro.

Andy Shevchenko (8):
iio: core: Add opaque_struct_size() helper and use it
iio: core: Use sysfs_match_string() helper
iio: core: Switch to krealloc_array()
iio: core: Use min() instead of min_t() to make code more robust
iio: core: Get rid of redundant 'else'
iio: core: Fix issues and style of the comments
iio: core: Move initcalls closer to the respective calls
iio: core: Improve indentation in a few places

drivers/iio/industrialio-core.c | 226 ++++++++++++++++----------------
1 file changed, 115 insertions(+), 111 deletions(-)

--
2.40.0.1.gaa8946217a0b



2023-07-20 21:16:59

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 8/8] iio: core: Improve indentation in a few places

Improve an indentation in a few places to increase readability.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 23 ++++++++++-------------
1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 202a1a67ba98..1918da2a70ad 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -206,9 +206,9 @@ bool iio_buffer_enabled(struct iio_dev *indio_dev)
{
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);

- return iio_dev_opaque->currentmode
- & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
- INDIO_BUFFER_SOFTWARE);
+ return iio_dev_opaque->currentmode &
+ (INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE |
+ INDIO_BUFFER_TRIGGERED);
}
EXPORT_SYMBOL_GPL(iio_buffer_enabled);

@@ -388,8 +388,8 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
}

iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
- sizeof(iio_dev_opaque->read_buf),
- "0x%X\n", val);
+ sizeof(iio_dev_opaque->read_buf),
+ "0x%X\n", val);

return simple_read_from_buffer(userbuf, count, ppos,
iio_dev_opaque->read_buf,
@@ -492,8 +492,7 @@ static ssize_t iio_read_channel_ext_info(struct device *dev,

static ssize_t iio_write_channel_ext_info(struct device *dev,
struct device_attribute *attr,
- const char *buf,
- size_t len)
+ const char *buf, size_t len)
{
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
@@ -585,9 +584,9 @@ static int iio_setup_mount_idmatrix(const struct device *dev,
ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
const struct iio_chan_spec *chan, char *buf)
{
- const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
- priv)(indio_dev, chan);
+ const struct iio_mount_matrix *mtx;

+ mtx = ((iio_get_mount_matrix_t *)priv)(indio_dev, chan);
if (IS_ERR(mtx))
return PTR_ERR(mtx);

@@ -1025,14 +1024,12 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
if (chan->modified && (shared_by == IIO_SEPARATE)) {
if (chan->extend_name)
full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
- iio_modifier_names[chan
- ->channel2],
+ iio_modifier_names[chan->channel2],
chan->extend_name,
postfix);
else
full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
- iio_modifier_names[chan
- ->channel2],
+ iio_modifier_names[chan->channel2],
postfix);
} else {
if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
--
2.40.0.1.gaa8946217a0b


2023-07-20 21:17:25

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

Let the krealloc_array() copy the original data and
check for a multiplication overflow.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 20 +++++++-------------
1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 90e59223b178..be154879983e 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1465,7 +1465,7 @@ int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
const struct attribute_group **new, **old = iio_dev_opaque->groups;
unsigned int cnt = iio_dev_opaque->groupcounter;

- new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
+ new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL);
if (!new)
return -ENOMEM;

@@ -1483,13 +1483,13 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
{
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
+ struct attribute **attrs, **attr, *clk = NULL;
struct iio_dev_attr *p;
- struct attribute **attr, *clk = NULL;

/* First count elements in any existing group */
- if (indio_dev->info->attrs) {
- attr = indio_dev->info->attrs->attrs;
- while (*attr++ != NULL)
+ attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL;
+ if (attrs) {
+ for (attr = attrs; *attr; attr++)
attrcount_orig++;
}
attrcount = attrcount_orig;
@@ -1521,20 +1521,14 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
if (clk)
attrcount++;

+ /* Copy across original attributes, and point to original binary attributes */
iio_dev_opaque->chan_attr_group.attrs =
- kcalloc(attrcount + 1,
- sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
- GFP_KERNEL);
+ krealloc_array(attrs, attrcount + 1, sizeof(*attrs), GFP_KERNEL);
if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
ret = -ENOMEM;
goto error_clear_attrs;
}
- /* Copy across original attributes, and point to original binary attributes */
if (indio_dev->info->attrs) {
- memcpy(iio_dev_opaque->chan_attr_group.attrs,
- indio_dev->info->attrs->attrs,
- sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
- *attrcount_orig);
iio_dev_opaque->chan_attr_group.is_visible =
indio_dev->info->attrs->is_visible;
iio_dev_opaque->chan_attr_group.bin_attrs =
--
2.40.0.1.gaa8946217a0b


2023-07-20 21:18:11

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/8] iio: core: Add opaque_struct_size() helper and use it

Introduce opaque_struct_size() helper, which may be moved
to overflow.h in the future, and use it in the IIO core.

Cc: Uwe Kleine-König <[email protected]>
Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index c117f50d0cf3..6cca86fd0ef9 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1617,6 +1617,28 @@ const struct device_type iio_device_type = {
.release = iio_dev_release,
};

+/**
+ * opaque_struct_size() - Calculate size of opaque structure with trailing aligned data.
+ * @p: Pointer to the opaque structure.
+ * @a: Alignment in bytes before trailing data.
+ * @s: Data size in bytes (can be 0).
+ *
+ * Calculates size of memory needed for structure of @p followed by the aligned data
+ * of size @s. When @s is 0, the alignment @a is not taken into account and the result
+ * is an equivalent to sizeof(*(@p)).
+ *
+ * Returns: Number of bytes needed or SIZE_MAX on overflow.
+ */
+#define opaque_struct_size(p, a, s) ({ \
+ size_t _psize = sizeof(*(p)); \
+ size_t _asize = ALIGN(_psize, (a)); \
+ size_t _ssize = s; \
+ _ssize ? size_add(_asize, _ssize) : _psize; \
+})
+
+#define opaque_struct_data(p, a) \
+ ((void *)(p) + ALIGN(sizeof(*(p)), (a)))
+
/**
* iio_device_alloc() - allocate an iio_dev from a driver
* @parent: Parent device.
@@ -1628,19 +1650,13 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
struct iio_dev *indio_dev;
size_t alloc_size;

- alloc_size = sizeof(struct iio_dev_opaque);
- if (sizeof_priv) {
- alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
- alloc_size += sizeof_priv;
- }
-
+ alloc_size = opaque_struct_size(iio_dev_opaque, IIO_DMA_MINALIGN, sizeof_priv);
iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
if (!iio_dev_opaque)
return NULL;

indio_dev = &iio_dev_opaque->indio_dev;
- indio_dev->priv = (char *)iio_dev_opaque +
- ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
+ indio_dev->priv = opaque_struct_data(iio_dev_opaque, IIO_DMA_MINALIGN);

indio_dev->dev.parent = parent;
indio_dev->dev.type = &iio_device_type;
--
2.40.0.1.gaa8946217a0b


2023-07-20 21:31:53

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 2/8] iio: core: Use sysfs_match_string() helper

Use sysfs_match_string() helper instead of open coded variant.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 81 +++++++++++++--------------------
1 file changed, 31 insertions(+), 50 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 6cca86fd0ef9..90e59223b178 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1400,50 +1400,32 @@ static ssize_t label_show(struct device *dev, struct device_attribute *attr,

static DEVICE_ATTR_RO(label);

+static const char * const clock_names[] = {
+ [CLOCK_REALTIME] = "realtime",
+ [CLOCK_MONOTONIC] = "monotonic",
+ [CLOCK_PROCESS_CPUTIME_ID] = "process_cputime_id",
+ [CLOCK_THREAD_CPUTIME_ID] = "thread_cputime_id",
+ [CLOCK_MONOTONIC_RAW] = "monotonic_raw",
+ [CLOCK_REALTIME_COARSE] = "realtime_coarse",
+ [CLOCK_MONOTONIC_COARSE] = "monotonic_coarse",
+ [CLOCK_BOOTTIME] = "boottime",
+ [CLOCK_REALTIME_ALARM] = "realtime_alarm",
+ [CLOCK_BOOTTIME_ALARM] = "boottime_alarm",
+ [CLOCK_SGI_CYCLE] = "sgi_cycle",
+ [CLOCK_TAI] = "tai",
+};
+
static ssize_t current_timestamp_clock_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
const clockid_t clk = iio_device_get_clock(indio_dev);
- const char *name;
- ssize_t sz;

- switch (clk) {
- case CLOCK_REALTIME:
- name = "realtime\n";
- sz = sizeof("realtime\n");
- break;
- case CLOCK_MONOTONIC:
- name = "monotonic\n";
- sz = sizeof("monotonic\n");
- break;
- case CLOCK_MONOTONIC_RAW:
- name = "monotonic_raw\n";
- sz = sizeof("monotonic_raw\n");
- break;
- case CLOCK_REALTIME_COARSE:
- name = "realtime_coarse\n";
- sz = sizeof("realtime_coarse\n");
- break;
- case CLOCK_MONOTONIC_COARSE:
- name = "monotonic_coarse\n";
- sz = sizeof("monotonic_coarse\n");
- break;
- case CLOCK_BOOTTIME:
- name = "boottime\n";
- sz = sizeof("boottime\n");
- break;
- case CLOCK_TAI:
- name = "tai\n";
- sz = sizeof("tai\n");
- break;
- default:
+ if (clk < 0 && clk >= ARRAY_SIZE(clock_names))
BUG();
- }

- memcpy(buf, name, sz);
- return sz;
+ return sprintf(buf, "%s\n", clock_names[clk]);
}

static ssize_t current_timestamp_clock_store(struct device *dev,
@@ -1453,22 +1435,21 @@ static ssize_t current_timestamp_clock_store(struct device *dev,
clockid_t clk;
int ret;

- if (sysfs_streq(buf, "realtime"))
- clk = CLOCK_REALTIME;
- else if (sysfs_streq(buf, "monotonic"))
- clk = CLOCK_MONOTONIC;
- else if (sysfs_streq(buf, "monotonic_raw"))
- clk = CLOCK_MONOTONIC_RAW;
- else if (sysfs_streq(buf, "realtime_coarse"))
- clk = CLOCK_REALTIME_COARSE;
- else if (sysfs_streq(buf, "monotonic_coarse"))
- clk = CLOCK_MONOTONIC_COARSE;
- else if (sysfs_streq(buf, "boottime"))
- clk = CLOCK_BOOTTIME;
- else if (sysfs_streq(buf, "tai"))
- clk = CLOCK_TAI;
- else
+ ret = sysfs_match_string(clock_names, buf);
+ if (ret < 0)
+ return ret;
+
+ switch (ret) {
+ case CLOCK_PROCESS_CPUTIME_ID:
+ case CLOCK_THREAD_CPUTIME_ID:
+ case CLOCK_REALTIME_ALARM:
+ case CLOCK_BOOTTIME_ALARM:
+ case CLOCK_SGI_CYCLE:
return -EINVAL;
+ default:
+ clk = ret;
+ break;
+ }

ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
if (ret)
--
2.40.0.1.gaa8946217a0b


2023-07-21 08:23:42

by Nuno Sá

[permalink] [raw]
Subject: Re: [PATCH v1 0/8] iio: core: A few code cleanups and documentation fixes

On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:
> Just set of almost unrelated to each other cleanups against IIO
> core implementation.
>
> The positive LoCs diffstat due to first patch that adds a lot of
> documentation for the new added macro.
>
> Andy Shevchenko (8):
>   iio: core: Add opaque_struct_size() helper and use it
>   iio: core: Use sysfs_match_string() helper
>   iio: core: Switch to krealloc_array()
>   iio: core: Use min() instead of min_t() to make code more robust
>   iio: core: Get rid of redundant 'else'
>   iio: core: Fix issues and style of the comments
>   iio: core: Move initcalls closer to the respective calls
>   iio: core: Improve indentation in a few places
>
>  drivers/iio/industrialio-core.c | 226 ++++++++++++++++----------------
>  1 file changed, 115 insertions(+), 111 deletions(-)
>

Neat series... Just a few comments on my side and on patch 3 is up to you to
take the comments or not.

With my comment addressed on patch 2:

Reviewed-by: Nuno Sa <[email protected]>


2023-07-21 08:23:47

by Nuno Sá

[permalink] [raw]
Subject: Re: [PATCH v1 2/8] iio: core: Use sysfs_match_string() helper

On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:
> Use sysfs_match_string() helper instead of open coded variant.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
>  drivers/iio/industrialio-core.c | 81 +++++++++++++--------------------
>  1 file changed, 31 insertions(+), 50 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 6cca86fd0ef9..90e59223b178 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1400,50 +1400,32 @@ static ssize_t label_show(struct device *dev, struct
> device_attribute *attr,
>  
>  static DEVICE_ATTR_RO(label);
>  
> +static const char * const clock_names[] = {
> +       [CLOCK_REALTIME]                = "realtime",
> +       [CLOCK_MONOTONIC]               = "monotonic",
> +       [CLOCK_PROCESS_CPUTIME_ID]      = "process_cputime_id",
> +       [CLOCK_THREAD_CPUTIME_ID]       = "thread_cputime_id",
> +       [CLOCK_MONOTONIC_RAW]           = "monotonic_raw",
> +       [CLOCK_REALTIME_COARSE]         = "realtime_coarse",
> +       [CLOCK_MONOTONIC_COARSE]        = "monotonic_coarse",
> +       [CLOCK_BOOTTIME]                = "boottime",
> +       [CLOCK_REALTIME_ALARM]          = "realtime_alarm",
> +       [CLOCK_BOOTTIME_ALARM]          = "boottime_alarm",
> +       [CLOCK_SGI_CYCLE]               = "sgi_cycle",
> +       [CLOCK_TAI]                     = "tai",
> +};
> +
>  static ssize_t current_timestamp_clock_show(struct device *dev,
>                                             struct device_attribute *attr,
>                                             char *buf)
>  {
>         const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>         const clockid_t clk = iio_device_get_clock(indio_dev);
> -       const char *name;
> -       ssize_t sz;
>  
> -       switch (clk) {
> -       case CLOCK_REALTIME:
> -               name = "realtime\n";
> -               sz = sizeof("realtime\n");
> -               break;
> -       case CLOCK_MONOTONIC:
> -               name = "monotonic\n";
> -               sz = sizeof("monotonic\n");
> -               break;
> -       case CLOCK_MONOTONIC_RAW:
> -               name = "monotonic_raw\n";
> -               sz = sizeof("monotonic_raw\n");
> -               break;
> -       case CLOCK_REALTIME_COARSE:
> -               name = "realtime_coarse\n";
> -               sz = sizeof("realtime_coarse\n");
> -               break;
> -       case CLOCK_MONOTONIC_COARSE:
> -               name = "monotonic_coarse\n";
> -               sz = sizeof("monotonic_coarse\n");
> -               break;
> -       case CLOCK_BOOTTIME:
> -               name = "boottime\n";
> -               sz = sizeof("boottime\n");
> -               break;
> -       case CLOCK_TAI:
> -               name = "tai\n";
> -               sz = sizeof("tai\n");
> -               break;
> -       default:
> +       if (clk < 0 && clk >= ARRAY_SIZE(clock_names))
>                 BUG();
> -       }
>  
> -       memcpy(buf, name, sz);
> -       return sz;
> +       return sprintf(buf, "%s\n", clock_names[clk]);

syfs_emit()...

- Nuno Sá

2023-07-21 08:28:34

by Nuno Sá

[permalink] [raw]
Subject: Re: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:
> Let the krealloc_array() copy the original data and
> check for a multiplication overflow.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
>  drivers/iio/industrialio-core.c | 20 +++++++-------------
>  1 file changed, 7 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 90e59223b178..be154879983e 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1465,7 +1465,7 @@ int iio_device_register_sysfs_group(struct iio_dev
> *indio_dev,
>         const struct attribute_group **new, **old = iio_dev_opaque->groups;
>         unsigned int cnt = iio_dev_opaque->groupcounter;
>  
> -       new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
> +       new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL);
>         if (!new)
>                 return -ENOMEM;
>  
> @@ -1483,13 +1483,13 @@ static int iio_device_register_sysfs(struct iio_dev
> *indio_dev)
>  {
>         struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
>         int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
> +       struct attribute **attrs, **attr, *clk = NULL;
>         struct iio_dev_attr *p;
> -       struct attribute **attr, *clk = NULL;
>  
>         /* First count elements in any existing group */
> -       if (indio_dev->info->attrs) {
> -               attr = indio_dev->info->attrs->attrs;
> -               while (*attr++ != NULL)
> +       attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL;
> +       if (attrs) {
> +               for (attr = attrs; *attr; attr++)
>                         attrcount_orig++;

not really related with the change... maybe just mention it in the commit?

>         }
>         attrcount = attrcount_orig;
> @@ -1521,20 +1521,14 @@ static int iio_device_register_sysfs(struct iio_dev
> *indio_dev)
>         if (clk)
>                 attrcount++;
>  
> +       /* Copy across original attributes, and point to original binary
> attributes */
>         iio_dev_opaque->chan_attr_group.attrs =
> -               kcalloc(attrcount + 1,
> -                       sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
> -                       GFP_KERNEL);
> +               krealloc_array(attrs, attrcount + 1, sizeof(*attrs),
> GFP_KERNEL);
>         if (iio_dev_opaque->chan_attr_group.attrs == NULL) {

since you're here and you also already did some style cleanups above, maybe
change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'?

- Nuno Sá

2023-07-21 10:18:11

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno S? wrote:
> On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:

...

> > +???????struct attribute **attrs, **attr, *clk = NULL;
> > ????????struct iio_dev_attr *p;
> > -???????struct attribute **attr, *clk = NULL;
> > ?
> > ????????/* First count elements in any existing group */
> > -???????if (indio_dev->info->attrs) {
> > -???????????????attr = indio_dev->info->attrs->attrs;
> > -???????????????while (*attr++ != NULL)
> > +???????attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL;
> > +???????if (attrs) {
> > +???????????????for (attr = attrs; *attr; attr++)
> > ????????????????????????attrcount_orig++;

> not really related with the change... maybe just mention it in the commit?

Hmm... It's related to make krealloc_array() to work as expected.

> > ????????}

...

> > ????????iio_dev_opaque->chan_attr_group.attrs =
> > -???????????????kcalloc(attrcount + 1,
> > -???????????????????????sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
> > -???????????????????????GFP_KERNEL);
> > +???????????????krealloc_array(attrs, attrcount + 1, sizeof(*attrs),
> > GFP_KERNEL);
> > ????????if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
>
> since you're here and you also already did some style cleanups above, maybe
> change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'?

I don't think it's related (but you can tell that this check related to
the allocator, and since we touch it, we may touch this), if Jonathan
wants this, I definitely do.

...

Thank you for the review!

--
With Best Regards,
Andy Shevchenko



2023-07-21 10:42:14

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 0/8] iio: core: A few code cleanups and documentation fixes

On Fri, Jul 21, 2023 at 10:06:05AM +0200, Nuno S? wrote:
> On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:
> > Just set of almost unrelated to each other cleanups against IIO
> > core implementation.
> >
> > The positive LoCs diffstat due to first patch that adds a lot of
> > documentation for the new added macro.
> >
> > Andy Shevchenko (8):
> > ? iio: core: Add opaque_struct_size() helper and use it
> > ? iio: core: Use sysfs_match_string() helper
> > ? iio: core: Switch to krealloc_array()
> > ? iio: core: Use min() instead of min_t() to make code more robust
> > ? iio: core: Get rid of redundant 'else'
> > ? iio: core: Fix issues and style of the comments
> > ? iio: core: Move initcalls closer to the respective calls
> > ? iio: core: Improve indentation in a few places
> >
> > ?drivers/iio/industrialio-core.c | 226 ++++++++++++++++----------------
> > ?1 file changed, 115 insertions(+), 111 deletions(-)
> >
>
> Neat series... Just a few comments on my side and on patch 3 is up to you to
> take the comments or not.
>
> With my comment addressed on patch 2:

Agree, I won't change patch 3 for now, I replied there why.

> Reviewed-by: Nuno Sa <[email protected]>

Thank you! I'll embed this into v2.

--
With Best Regards,
Andy Shevchenko



2023-07-21 11:14:23

by Nuno Sá

[permalink] [raw]
Subject: Re: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote:
> On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno Sá wrote:
> > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:
>
> ...
>
> > > +       struct attribute **attrs, **attr, *clk = NULL;
> > >         struct iio_dev_attr *p;
> > > -       struct attribute **attr, *clk = NULL;
> > >  
> > >         /* First count elements in any existing group */
> > > -       if (indio_dev->info->attrs) {
> > > -               attr = indio_dev->info->attrs->attrs;
> > > -               while (*attr++ != NULL)
> > > +       attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs :
> > > NULL;
> > > +       if (attrs) {
> > > +               for (attr = attrs; *attr; attr++)
> > >                         attrcount_orig++;
>
> > not really related with the change... maybe just mention it in the commit?
>
> Hmm... It's related to make krealloc_array() to work as expected.
>

Hmm, I think it's arguable :). while() -> for() it's not really needed unless
I'm missing something. You could even initialize 'attrs' to NULL at declaration
and keep the above diff minimum.

That said, I actually prefer this style (even though some people don't like much
the ternary operator).

> > >         }
>
> ...
>
> > >         iio_dev_opaque->chan_attr_group.attrs =
> > > -               kcalloc(attrcount + 1,
> > > -                       sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
> > > -                       GFP_KERNEL);
> > > +               krealloc_array(attrs, attrcount + 1, sizeof(*attrs),
> > > GFP_KERNEL);
> > >         if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
> >
> > since you're here and you also already did some style cleanups above, maybe
> > change it to 'if (!iio_dev_opaque->chan_attr_group.attrs)'?
>
> I don't think it's related (but you can tell that this check related to
> the allocator, and since we touch it, we may touch this), if Jonathan
> wants this, I definitely do.

Fair enough...


- Nuno Sá

2023-07-21 12:01:14

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

On Fri, Jul 21, 2023 at 02:28:36PM +0300, Andy Shevchenko wrote:
> On Fri, Jul 21, 2023 at 12:53:53PM +0200, Nuno S? wrote:
> > On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote:
> > > On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno S? wrote:
> > > > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:

...

> > > > > +???????struct attribute **attrs, **attr, *clk = NULL;
> > > > > ????????struct iio_dev_attr *p;
> > > > > -???????struct attribute **attr, *clk = NULL;
> > > > > ?
> > > > > ????????/* First count elements in any existing group */
> > > > > -???????if (indio_dev->info->attrs) {
> > > > > -???????????????attr = indio_dev->info->attrs->attrs;
> > > > > -???????????????while (*attr++ != NULL)
> > > > > +???????attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs :
> > > > > NULL;
> > > > > +???????if (attrs) {
> > > > > +???????????????for (attr = attrs; *attr; attr++)
> > > > > ????????????????????????attrcount_orig++;
> > >
> > > > not really related with the change... maybe just mention it in the commit?
> > >
> > > Hmm... It's related to make krealloc_array() to work as expected.
> > >
> >
> > Hmm, I think it's arguable :). while() -> for() it's not really needed unless
> > I'm missing something. You could even initialize 'attrs' to NULL at declaration
> > and keep the above diff minimum.
>
> I'm not a fan of the assignments in the declarations when it potentially can be
> disrupted by a chunk of code and reading the code itself may be harder due to
> an interruption for checking the initial value. Hence, having
>
> + attr = attrs;
> while (... != NULL)
>
> seems enough to be replaced with one liner for-loop.

Note that attrs is reused later, so the above assignment makes it cleaner that
some value is assigned to it. With the original code it's not so obvious.

> > That said, I actually prefer this style (even though some people don't like much
> > the ternary operator).
>
> Thanks!
>
> > > > > ????????}

--
With Best Regards,
Andy Shevchenko



2023-07-21 12:09:48

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 3/8] iio: core: Switch to krealloc_array()

On Fri, Jul 21, 2023 at 12:53:53PM +0200, Nuno S? wrote:
> On Fri, 2023-07-21 at 13:14 +0300, Andy Shevchenko wrote:
> > On Fri, Jul 21, 2023 at 09:59:37AM +0200, Nuno S? wrote:
> > > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:

...

> > > > +???????struct attribute **attrs, **attr, *clk = NULL;
> > > > ????????struct iio_dev_attr *p;
> > > > -???????struct attribute **attr, *clk = NULL;
> > > > ?
> > > > ????????/* First count elements in any existing group */
> > > > -???????if (indio_dev->info->attrs) {
> > > > -???????????????attr = indio_dev->info->attrs->attrs;
> > > > -???????????????while (*attr++ != NULL)
> > > > +???????attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs :
> > > > NULL;
> > > > +???????if (attrs) {
> > > > +???????????????for (attr = attrs; *attr; attr++)
> > > > ????????????????????????attrcount_orig++;
> >
> > > not really related with the change... maybe just mention it in the commit?
> >
> > Hmm... It's related to make krealloc_array() to work as expected.
> >
>
> Hmm, I think it's arguable :). while() -> for() it's not really needed unless
> I'm missing something. You could even initialize 'attrs' to NULL at declaration
> and keep the above diff minimum.

I'm not a fan of the assignments in the declarations when it potentially can be
disrupted by a chunk of code and reading the code itself may be harder due to
an interruption for checking the initial value. Hence, having

+ attr = attrs;
while (... != NULL)

seems enough to be replaced with one liner for-loop.

> That said, I actually prefer this style (even though some people don't like much
> the ternary operator).

Thanks!

> > > > ????????}

--
With Best Regards,
Andy Shevchenko



2023-07-21 17:13:34

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 0/8] iio: core: A few code cleanups and documentation fixes

On Fri, Jul 21, 2023 at 07:55:22PM +0300, Andy Shevchenko wrote:
> On Fri, Jul 21, 2023 at 10:06:05AM +0200, Nuno S? wrote:
> > On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:

...

> > Reviewed-by: Nuno Sa <[email protected]>
>
> Btw, is it deliberately you don't put accent on "a"?

So far in the upstream it had been started from f1caa90085ef ("iio: dac:
set variable max5522_channels storage-class-specifier to static").

--
With Best Regards,
Andy Shevchenko



2023-07-21 17:26:00

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 0/8] iio: core: A few code cleanups and documentation fixes

On Fri, Jul 21, 2023 at 10:06:05AM +0200, Nuno S? wrote:
> On Thu, 2023-07-20 at 23:53 +0300, Andy Shevchenko wrote:

...

> Reviewed-by: Nuno Sa <[email protected]>

Btw, is it deliberately you don't put accent on "a"?


--
With Best Regards,
Andy Shevchenko