Re: [PATCH RFC 2/5] iio: core: add struct iio_chan_attr and split attribute creation

From: Jonathan Cameron

Date: Fri Nov 20 2026 - 20:25:57 EST


On Fri, 20 Nov 2026 18:03:58 +0000
Rodrigo Alencar via B4 Relay <devnull+rodrigo.alencar.analog.com@xxxxxxxxxx> wrote:

> From: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
>
> Channel, event and buffer attributes are all created through
> __iio_add_chan_devattr(), which allocates a struct iio_dev_attr and its
> name, and only then walks the attribute list looking for a duplicate.
> For shared attributes (info_mask_shared_by_*, shared ext_info and shared
> event info) every channel after the first one allocates the attribute
> and its name just to find the duplicate and free both again. The
> function also takes a buffer pointer that only buffer attributes use.
>
> Introduce struct iio_chan_attr for the attributes created by the IIO
> core. It embeds struct iio_dev_attr and adds the channel spec and the
> list node, so that the core-only state no longer needs to live in the
> structure drivers use for their own attributes. Other attribute types
> can extend it further.
>
> Replace __iio_add_chan_devattr() with two steps:
>
> - iio_chan_attr_prepare() builds the full attribute name on the stack
> and checks it against the attribute list.
> - iio_chan_attr_add() initializes an attribute allocated by the caller
> and adds it to the list.
>
> The caller allocates its own attribute type in between, so the core
> needs no size or context pointer, and duplicates are detected before
> anything is allocated.
>
> Also add iio_attr_show_t and iio_attr_store_t, so the show and store
> callback prototypes are not spelled out in every helper.
>
> Event and buffer attributes are converted to struct iio_chan_attr, but
> otherwise keep their current behavior.
>
> Signed-off-by: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
A couple of minor comments inline.

Thanks,

Jonathan


> int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
> index 2d7759325eb3..48fdaed6d703 100644
> --- a/drivers/iio/industrialio-buffer.c
> +++ b/drivers/iio/industrialio-buffer.c

> @@ -1521,23 +1526,25 @@ static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
> struct attribute *attr)
> {
> struct device_attribute *dattr = to_dev_attr(attr);
> + struct iio_chan_attr *chan_attr;
> struct iio_dev_attr *iio_attr;
>
> - iio_attr = kzalloc_obj(*iio_attr);
> - if (!iio_attr)
> + chan_attr = kzalloc_obj(*chan_attr);

Seems to be suited to
struct iio_chan_attr *chan_attr __free(kfree) =
kzalloc_obj(*chan_attr);

> + if (!chan_attr)
> return NULL;
>
> + iio_attr = &chan_attr->iio_attr;
> iio_attr->buffer = buffer;
> memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
> iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
> if (!iio_attr->dev_attr.attr.name) {
> - kfree(iio_attr);
> + kfree(chan_attr);

No need for this.

> return NULL;
> }
>
> sysfs_attr_init(&iio_attr->dev_attr.attr);
>
> - list_add(&iio_attr->l, &buffer->buffer_attr_list);
> + list_add(&chan_attr->l, &buffer->buffer_attr_list);

then
list_add(&no_free_ptr(chan_attr)->l, &buffer->buffer_attr_list);

>
> return &iio_attr->dev_attr.attr;
> }

> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index ed7aa7fd9391..d1f56e7f1666 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
>
> -static
> -int __iio_device_attr_init(struct device *dev,
> - struct device_attribute *dev_attr,
> - const char *postfix,
> - struct iio_chan_spec const *chan,
> - ssize_t (*readfunc)(struct device *dev,
> - struct device_attribute *attr,
> - char *buf),
> - ssize_t (*writefunc)(struct device *dev,
> - struct device_attribute *attr,
> - const char *buf,
> - size_t len),
> - enum iio_shared_by shared_by)
> +static ssize_t iio_chan_attr_name_emit(struct device *dev,
> + const struct iio_chan_spec *chan,
> + enum iio_shared_by shared_by,
> + const char *postfix,
> + char *buf, size_t len)
> {
> - char prefix[NAME_MAX + 1];
> + struct seq_buf s;
> ssize_t ret;
>
> + ret = __iio_chan_prefix_emit(dev, chan, shared_by, buf, len);

Is it worth the pain of just passing the seq_buf in there?


> + if (ret < 0)
> + return ret;
> +
> + seq_buf_init(&s, buf + ret, len - ret);
> + seq_buf_printf(&s, ret ? "_%s" : "%s", postfix);
> + if (seq_buf_has_overflowed(&s))
> + return -EOVERFLOW;
> +
> + return ret + s.len;
> +}