mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Rodrigo Alencar via B4 Relay
	<devnull+rodrigo.alencar.analog.com@kernel.org>
Cc: rodrigo.alencar@analog.com, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	David Lechner <dlechner@baylibre.com>,
	Andy Shevchenko <andy@kernel.org>
Subject: Re: [PATCH RFC 2/5] iio: core: add struct iio_chan_attr and split attribute creation
Date: Sat, 21 Nov 2026 01:25:42 +0000	[thread overview]
Message-ID: <20261121012542.3beee3c9@jic23-hlaptop> (raw)
In-Reply-To: <20261120-iio-core-attr-review-v1-2-05b41f8133c2@analog.com>

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

> From: Rodrigo Alencar <rodrigo.alencar@analog.com>
> 
> 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@analog.com>
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;
> +}




  reply	other threads:[~2026-11-21  1:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 18:03 [PATCH RFC 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
2026-11-20 18:03 ` [PATCH RFC 1/5] iio: buffer: add iio_attr_to_buffer() helper Rodrigo Alencar via B4 Relay
2026-11-20 18:03 ` [PATCH RFC 2/5] iio: core: add struct iio_chan_attr and split attribute creation Rodrigo Alencar via B4 Relay
2026-11-21  1:25   ` Jonathan Cameron [this message]
2026-11-20 18:03 ` [PATCH RFC 3/5] iio: event: add struct iio_event_attr Rodrigo Alencar via B4 Relay
2026-11-20 18:04 ` [PATCH RFC 4/5] iio: buffer: add struct iio_buffer_attr Rodrigo Alencar via B4 Relay
2026-11-21  1:52   ` Jonathan Cameron
2026-11-20 18:04 ` [PATCH RFC 5/5] iio: sysfs: drop core-only fields from struct iio_dev_attr Rodrigo Alencar via B4 Relay
2026-11-21  1:36   ` Jonathan Cameron
2026-11-21  1:39 ` [PATCH RFC 0/5] iio: split struct iio_dev_attr into typed core attributes Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121012542.3beee3c9@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=devnull+rodrigo.alencar.analog.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rodrigo.alencar@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®