Re: [PATCH v3 06/11] iio: core: merge buffer/ & scan_elements/ attributes

From: Alexandru Ardelean
Date: Thu Feb 04 2021 - 08:43:46 EST


On Wed, Feb 3, 2021 at 12:04 PM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
>
> On Mon, Feb 1, 2021 at 5:28 PM Alexandru Ardelean
> <alexandru.ardelean@xxxxxxxxxx> wrote:
> >
> > With this change, we create a new directory for the IIO device called
> > buffer0, under which both the old buffer/ and scan_elements/ are stored.
> >
> > This is done to simplify the addition of multiple IIO buffers per IIO
> > device. Otherwise we would need to add a bufferX/ and scan_elementsX/
> > directory for each IIO buffer.
> > With the current way of storing attribute groups, we can't have directories
> > stored under each other (i.e. scan_elements/ under buffer/), so the best
> > approach moving forward is to merge their attributes.
> >
> > The old/legacy buffer/ & scan_elements/ groups are not stored on the opaque
> > IIO device object. This way the IIO buffer can have just a single
> > attribute_group object, saving a bit of memory when adding multiple IIO
> > buffers.
>
> ...
>
> > +static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
> > + struct attribute **buffer_attrs,
> > + int buffer_attrcount,
> > + int scan_el_attrcount)
> > +{
> > + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
> > + struct attribute_group *group;
> > + int ret;
> > +
> > + group = &iio_dev_opaque->legacy_buffer_group;
>
> > + group->attrs = kcalloc(buffer_attrcount + 1,
> > + sizeof(struct attribute *), GFP_KERNEL);
> > + if (!group->attrs)
> > + return -ENOMEM;
> > +
> > + memcpy(group->attrs, buffer_attrs,
> > + buffer_attrcount * sizeof(struct attribute *));
>
> kmemdup() ?
> Perhaps introduce kmemdup_array().

doesn't add much benefit from what i can tell;
and it complicates things with the fact that we need to add the extra
null terminator element;
[1] if we kmemdup(buffer_attrcount + 1) , the copy an extra element we
don't need, which needs to be null-ed

>
> > + group->name = "buffer";
> > +
> > + ret = iio_device_register_sysfs_group(indio_dev, group);
> > + if (ret)
> > + goto error_free_buffer_attrs;
> > +
> > + group = &iio_dev_opaque->legacy_scan_el_group;
>
> > + group->attrs = kcalloc(scan_el_attrcount + 1,
> > + sizeof(struct attribute *), GFP_KERNEL);
> > + if (!group->attrs) {
> > + ret = -ENOMEM;
> > + goto error_free_buffer_attrs;
> > + }
> > +
> > + memcpy(group->attrs, &buffer_attrs[buffer_attrcount],
> > + scan_el_attrcount * sizeof(struct attribute *));
>
> Ditto.

continuing from [1]
here it may be worse, because kmemdup() would copy 1 element from
undefined memory;

>
> > + group->name = "scan_elements";
> > +
> > + ret = iio_device_register_sysfs_group(indio_dev, group);
> > + if (ret)
> > + goto error_free_scan_el_attrs;
> > +
> > + return 0;
> > +
> > +error_free_buffer_attrs:
> > + kfree(iio_dev_opaque->legacy_buffer_group.attrs);
> > +error_free_scan_el_attrs:
> > + kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
> > +
> > + return ret;
> > +}
>
> --
> With Best Regards,
> Andy Shevchenko