Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: David Lechner <dlechner@baylibre.com>
Cc: linux-iio@vger.kernel.org, "Nuno Sá" <noname.nuno@gmail.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: Re: [PATCH 05/16] iio: adc: Use iio_push_to_buffers_with_ts() to provide length for runtime checks.
Date: Sun, 6 Apr 2025 18:02:46 +0100	[thread overview]
Message-ID: <20250406180246.108a58a5@jic23-huawei> (raw)
In-Reply-To: <b282990a-de51-4120-abde-9b628847d9f9@baylibre.com>

On Mon, 10 Mar 2025 16:49:18 -0500
David Lechner <dlechner@baylibre.com> wrote:

> On 3/9/25 1:20 PM, Jonathan Cameron wrote:
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > 
> > This new function allows us to perform debug checks in the helper to ensure
> > that the overrun does not occur.  Use it in all the simple cases where
> > either a static buffer or a structure is used in the drivers.
> > 
> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > ---  
> 
> ...
> 
> > diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c
> > index 9a020680885d..77c5dd414d36 100644
> > --- a/drivers/iio/adc/ad4030.c
> > +++ b/drivers/iio/adc/ad4030.c
> > @@ -706,8 +706,9 @@ static irqreturn_t ad4030_trigger_handler(int irq, void *p)
> >  	if (ret)
> >  		goto out;
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_data.raw,
> > -					   pf->timestamp);
> > +	iio_push_to_buffers_with_ts(indio_dev, st->rx_data.raw,  
> 
> Would &st->rx_data be more correct? I guess it doesn't really matter in this case.
Feels slightly more logical to use rx_data so changed.

> 
> > +				    sizeof(st->rx_data.raw),
> > +				    pf->timestamp);
> >  
> >  out:
> >  	iio_trigger_notify_done(indio_dev->trig);  
> 
> ...
> 
> > diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
> > index 18559757f908..4217d3963455 100644
> > --- a/drivers/iio/adc/ad7266.c
> > +++ b/drivers/iio/adc/ad7266.c
> > @@ -87,8 +87,9 @@ static irqreturn_t ad7266_trigger_handler(int irq, void *p)
> >  
> >  	ret = spi_read(st->spi, st->data.sample, 4);
> >  	if (ret == 0) {
> > -		iio_push_to_buffers_with_timestamp(indio_dev, &st->data,
> > -			    pf->timestamp);
> > +		iio_push_to_buffers_with_ts(indio_dev, &st->data,
> > +					    sizeof(st->data),  
> 
> This one has `s64 timestamp;` which should be aligned_s64.
Ah. That's an old bug!  Guess no one rand this on x86_32 :)

I'll add a precursor fix.
Note I didn't include some other drivers in this series because they are
also broken, but I missed this one. Good spot.
> 
> > +					    pf->timestamp);
> >  	}
> >  
> >  	iio_trigger_notify_done(indio_dev->trig);
> > diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
> > index 28b88092b4aa..def5f91dc343 100644
> > --- a/drivers/iio/adc/ad7298.c
> > +++ b/drivers/iio/adc/ad7298.c
> > @@ -155,8 +155,9 @@ static irqreturn_t ad7298_trigger_handler(int irq, void *p)
> >  	if (b_sent)
> >  		goto done;
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
> > -		iio_get_time_ns(indio_dev));
> > +	iio_push_to_buffers_with_ts(indio_dev, st->rx_buf,  
> 
> Does this one actually have enough room in rx_buf for 9 16-bit channels and a
> an aligned timestamp?

Only 8 channels have scan_index != -1 so this is fine I think.
-1 is magic for don't allow this one to be used in buffered mode.

> 
> __be16	rx_buf[12] __aligned(IIO_DMA_MINALIGN);
> 
> 
> > +				    sizeof(st->rx_buf),
> > +				    iio_get_time_ns(indio_dev));
> >  
> >  done:
> >  	iio_trigger_notify_done(indio_dev->trig);  
> 
> ...
> 
> > diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c
> > index 631e83717167..3ffa11b2e060 100644
> > --- a/drivers/iio/adc/ad7606.c
> > +++ b/drivers/iio/adc/ad7606.c
> > @@ -679,8 +679,8 @@ static irqreturn_t ad7606_trigger_handler(int irq, void *p)
> >  	if (ret)
> >  		goto error_ret;
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, &st->data,
> > -					   iio_get_time_ns(indio_dev));
> > +	iio_push_to_buffers_with_ts(indio_dev, &st->data, sizeof(st->data),  
> 
> This one might need ALIGNED in the buffer declarations. I'm working on this
> driver though, so can fix it up.
Corner case.  It's actually fine because IIO_DMA_MINALIGN is >= 8 but
that's not obvious and may not remain true for all future architectures so
indeed good to tidy that up at somepoint with the additional markings.

> 
> > +				    iio_get_time_ns(indio_dev));
> >  error_ret:
> >  	iio_trigger_notify_done(indio_dev->trig);
> >  	/* The rising edge of the CONVST signal starts a new conversion. */
> > diff --git a/drivers/iio/adc/ad7768-1.c b/drivers/iio/adc/ad7768-1.c
> > index 5a863005aca6..4eb586d473ce 100644
> > --- a/drivers/iio/adc/ad7768-1.c
> > +++ b/drivers/iio/adc/ad7768-1.c
> > @@ -474,8 +474,9 @@ static irqreturn_t ad7768_trigger_handler(int irq, void *p)
> >  	if (ret < 0)
> >  		goto out;
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, &st->data.scan,
> > -					   iio_get_time_ns(indio_dev));
> > +	iio_push_to_buffers_with_ts(indio_dev, &st->data.scan,
> > +				    sizeof(st->data.scan),  
> 
> This one could use aligned_s64 on the timestamp.
Definitely. Another bug I'll put a separate fix in for this.

> 
> > +				    iio_get_time_ns(indio_dev));
> >  
> >  out:
> >  	iio_trigger_notify_done(indio_dev->trig);  
> 
> ...
> 
> > diff --git a/drivers/iio/adc/ad7923.c b/drivers/iio/adc/ad7923.c
> > index 87945efb940b..0369151c7db1 100644
> > --- a/drivers/iio/adc/ad7923.c
> > +++ b/drivers/iio/adc/ad7923.c
> > @@ -207,8 +207,8 @@ static irqreturn_t ad7923_trigger_handler(int irq, void *p)
> >  	if (b_sent)
> >  		goto done;
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
> > -					   iio_get_time_ns(indio_dev));
> > +	iio_push_to_buffers_with_ts(indio_dev, st->rx_buf, sizeof(st->rx_buf),  
> 
> This one is similar to ad7298 but doesn't have a temperature channel, so
> probably OK. Still could use ALIGNED in the buffer declaration to make it more
> obvious that we have a timestamp.

Also relies on IIO_DMA_MINALIGN >= 8
It's a bit messy to force alignment twice so I'll leave this for now.

> 
> > +				    iio_get_time_ns(indio_dev));
> >  
> >  done:
> >  	iio_trigger_notify_done(indio_dev->trig);
> > diff --git a/drivers/iio/adc/dln2-adc.c b/drivers/iio/adc/dln2-adc.c
> > index a1e48a756a7b..5ffb4b5f5c99 100644
> > --- a/drivers/iio/adc/dln2-adc.c
> > +++ b/drivers/iio/adc/dln2-adc.c
> > @@ -488,8 +488,8 @@ static irqreturn_t dln2_adc_trigger_h(int irq, void *p)
> >  		       (void *)dev_data.values + t->from, t->length);
> >  	}
> >  
> > -	iio_push_to_buffers_with_timestamp(indio_dev, &data,
> > -					   iio_get_time_ns(indio_dev));
> > +	iio_push_to_buffers_with_ts(indio_dev, &data, sizeof(data),  
> 
> Could use aligned_s64 on timestamp_space.

This one is more subtle as the structure is big enough but the overall
structure might be misaligned. I'll add a patch fixing it but not sure
it is suitable for a backport.

> 
> > +				    iio_get_time_ns(indio_dev));
> >  
> >  done:
> >  	iio_trigger_notify_done(indio_dev->trig);  
> 
> ...
> 
> > diff --git a/drivers/iio/adc/mxs-lradc-adc.c b/drivers/iio/adc/mxs-lradc-adc.c
> > index 8f1e6acea53b..92baf3f5f560 100644
> > --- a/drivers/iio/adc/mxs-lradc-adc.c
> > +++ b/drivers/iio/adc/mxs-lradc-adc.c
> > @@ -425,7 +425,8 @@ static irqreturn_t mxs_lradc_adc_trigger_handler(int irq, void *p)
> >  		j++;
> >  	}
> >  
> > -	iio_push_to_buffers_with_timestamp(iio, adc->buffer, pf->timestamp);
> > +	iio_push_to_buffers_with_ts(iio, adc->buffer, sizeof(adc->buffer),  
> 
> u32			buffer[10] __aligned(8);
> 
> Technically OK, but could use ALIGN. There are some other drivers like this
> but I won't call all of them out since they should be OK.

You mean to calculate the number of entrees in the array?

Jonathan

> 
> > +				    pf->timestamp);
> >  
> >  	iio_trigger_notify_done(iio->trig);
> >    
> 
> ...
> 
> > diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> > index 5dbf5f136768..32e7938b3892 100644
> > --- a/drivers/iio/adc/stm32-adc.c
> > +++ b/drivers/iio/adc/stm32-adc.c
> > @@ -1858,8 +1858,8 @@ static irqreturn_t stm32_adc_trigger_handler(int irq, void *p)
> >  
> >  	/* reset buffer index */
> >  	adc->bufi = 0;
> > -	iio_push_to_buffers_with_timestamp(indio_dev, adc->buffer,
> > -					   pf->timestamp);
> > +	iio_push_to_buffers_with_ts(indio_dev, adc->buffer, sizeof(adc->buffer),  
> 
> u16			buffer[STM32_ADC_MAX_SQ + 4] __aligned(8);
> 
> Could use ALIGN for timestamp instead of assuming STM32_ADC_MAX_SQ * 2 is the
> correct alignment. There are also a few more drivers like this that depend on
> a macro being even or multiple of 4 to get the correct alignment, which seems
> like it could be fragile, e.g. if a temperature channel was added.
> 
> > +				    pf->timestamp);
> >  	iio_trigger_notify_done(indio_dev->trig);
> >  
> >  	/* re-enable eoc irq */  
> 
> ...
> 
> 


  reply	other threads:[~2025-04-06 17:02 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-09 18:20 [PATCH 00/16] IIO: Introduce iio_push_to_buffers_with_ts() taking an input buffer length argument Jonathan Cameron
2025-03-09 18:20 ` [PATCH 01/16] iio: introduced iio_push_to_buffers_with_ts() that takes a total_len argument Jonathan Cameron
2025-03-10  8:34   ` Nuno Sá
2025-03-10 20:04     ` Jonathan Cameron
2025-03-10 22:16       ` Nuno Sá
2025-03-09 18:20 ` [PATCH 02/16] iio: dummy: Use a fixed structure to build up scan to push to buffers Jonathan Cameron
2025-03-10 21:10   ` David Lechner
2025-03-09 18:20 ` [PATCH 03/16] iio: dummy: Switch to iio_push_to_buffers_with_ts() and provide size of storage Jonathan Cameron
2025-03-09 18:20 ` [PATCH 04/16] iio: adc: ti-ads131e08: Use new iio_push_to_buffers_with_ts() to provide length sanity check Jonathan Cameron
2025-03-10 22:07   ` David Lechner
2025-03-09 18:20 ` [PATCH 05/16] iio: adc: Use iio_push_to_buffers_with_ts() to provide length for runtime checks Jonathan Cameron
2025-03-10 21:49   ` David Lechner
2025-04-06 17:02     ` Jonathan Cameron [this message]
2025-04-06 17:15       ` David Lechner
2025-03-09 18:20 ` [PATCH 06/16] iio: accel: " Jonathan Cameron
2025-03-10 22:20   ` David Lechner
2025-04-06 17:07     ` Jonathan Cameron
2025-03-09 18:20 ` [PATCH 07/16] iio: accel: hid: " Jonathan Cameron
2025-03-11 18:30   ` David Lechner
2025-03-09 18:20 ` [PATCH 08/16] iio: chemical: " Jonathan Cameron
2025-03-09 18:20 ` [PATCH 09/16] iio: temp: maxim-thermocouple: Fix potential lack of DMA safe buffer Jonathan Cameron
2025-03-09 18:20 ` [PATCH 10/16] iio: temperature: Use iio_push_to_buffers_with_ts() to provide length for runtime checks Jonathan Cameron
2025-03-09 18:20 ` [PATCH 11/16] iio: resolver: " Jonathan Cameron
2025-03-09 18:20 ` [PATCH 12/16] iio: proximity: irsd200: Use a struct for scan and iio_push_to_buffers_with_ts() Jonathan Cameron
2025-03-09 18:20 ` [PATCH 13/16] iio: proximity: Use iio_push_to_buffers_with_ts() to provide length for runtime checks Jonathan Cameron
2025-03-09 18:20 ` [PATCH 14/16] iio: pressure: zpa2326: Use aligned_s64 for the timestamp Jonathan Cameron
2025-03-09 18:20 ` [PATCH 15/16] iio: pressure: Use iio_push_to_buffers_with_ts() to provide length for runtime checks Jonathan Cameron
2025-03-09 18:21 ` [PATCH 16/16] iio: magnetometer: " 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=20250406180246.108a58a5@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=noname.nuno@gmail.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