Skip to contentSkip to conversation

iio: adc: ad4000: Avoid potential double data word read

PATCH Applied Marcelo Schmitt 5 participants 8 messages Apr 15, 2025 – May 5, 2025

Subsystems (3)
lore
Share & tools
lore

Marcelo Schmitt

author maintainer
#0 lore raw

PATCH 1/1 iio: adc: ad4000: Avoid potential double data word read

Currently, SPI-Engine offload module always sends 32-bit data elements to DMA engine. Appropriately, when set for SPI offloading, the IIO driver uses 32 storagebits for IIO ADC channel buffer elements. However, setting SPI transfer length according to storagebits (32-bits in case of offload) can lead to unnecessarily long transfers for ADCs that are 16-bit or less precision. Adjust AD4000 single-shot read to run transfers of 2 bytes when that is enough to get all ADC data bits.

Fixes d0dba3df842f ("iio: adc: ad4000: Add support for SPI offload")

---
After enough sleep and some time not looking at this driver I finally realize the potential issue David was talking about. Although I didn't see any clearly wrong reading when testing it last week, I'm adding the fixes tag since it's probably easier to drop the tag than to go fetch the commit log. Also adding a suggested-by tag.

Thanks,
Marcelo

 drivers/iio/adc/ad4000.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad4000.c +1 -1
index e69a9d2a3e8c..5813db28510d 100644
@@ -941,7 +941,7 @@ static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st,
 	xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
 
 	xfers[1].rx_buf = &st->scan.data;
-	xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
+	xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
 
 	/*
 	 * If the device is set up for SPI offloading, IIO channel scan_type is

base-commit: 1c2409fe38d5c19015d69851d15ba543d1911932

Signature · 1 line
2.47.2
#1 lore raw
Quoted 8 lines · Marcelo Schmitt

Currently, SPI-Engine offload module always sends 32-bit data elements to DMA engine. Appropriately, when set for SPI offloading, the IIO driver uses 32 storagebits for IIO ADC channel buffer elements. However, setting SPI transfer length according to storagebits (32-bits in case of offload) can lead to unnecessarily long transfers for ADCs that are 16-bit or less precision. Adjust AD4000 single-shot read to run transfers of 2 bytes when that is enough to get all ADC data bits.

...

drivers/iio/adc/ad4000.c static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st, · line 944

Quoted 3 lines
        xfers[1].rx_buf = &st->scan.data;
drivers/iio/adc/ad4000.c +1 -1
-       xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
+       xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;

But wouldn't be logical to have

       xfers[1].len = BITS_TO_BYTES(chan->scan_type.realbits);

?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#2 lore raw
Quoted 17 lines · Andy Shevchenko
Quoted 8 lines · Marcelo Schmitt

Currently, SPI-Engine offload module always sends 32-bit data elements to DMA engine. Appropriately, when set for SPI offloading, the IIO driver uses 32 storagebits for IIO ADC channel buffer elements. However, setting SPI transfer length according to storagebits (32-bits in case of offload) can lead to unnecessarily long transfers for ADCs that are 16-bit or less precision. Adjust AD4000 single-shot read to run transfers of 2 bytes when that is enough to get all ADC data bits.

...

drivers/iio/adc/ad4000.c static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st, · line 944

Quoted 3 lines
        xfers[1].rx_buf = &st->scan.data;
drivers/iio/adc/ad4000.c +1 -1
-       xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
+       xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
Quoted 6 lines

But wouldn't be logical to have

       xfers[1].len = BITS_TO_BYTES(chan->scan_type.realbits);

?

No, SPI expects 1, 2 or 4 bytes, never 3. If realbits is 18, we need len = 4.

It would have to be:

	xfers[1].len = roundup_pow_of_two(BITS_TO_BYTES(chan->scan_type.realbits));

But that gets too long for 1 line, so I prefer what Marcelo wrote.

Maybe an idea for another day:

#define SPI_LEN_FOR_BITS(bits) roundup_pow_of_two(BITS_TO_BYTES(bits))

There are a couple of places in spi/ that could use this and several iio drivers.

#3 lore raw
Quoted 3 lines · David Lechner
Quoted 2 lines · Andy Shevchenko

On Tue, Apr 15, 2025 at 3:21 PM Marcelo Schmitt
<[email protected]> wrote:

...

Quoted 10 lines
Quoted 9 lines

drivers/iio/adc/ad4000.c static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st, · line 944

Quoted 3 lines
        xfers[1].rx_buf = &st->scan.data;
drivers/iio/adc/ad4000.c +1 -1
-       xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
+       xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;

But wouldn't be logical to have

       xfers[1].len = BITS_TO_BYTES(chan->scan_type.realbits);

?

Quoted 8 lines

No, SPI expects 1, 2 or 4 bytes, never 3. If realbits is 18, we need len = 4.

It would have to be:

        xfers[1].len = roundup_pow_of_two(BITS_TO_BYTES(chan->scan_type.realbits));

But that gets too long for 1 line

Actually there are a handful of drivers including SPI core that need that helper already, I would prefer to have a helper defined in spi.h.

, so I prefer what Marcelo wrote.

Quoted 4 lines

Maybe an idea for another day:

#define SPI_LEN_FOR_BITS(bits) roundup_pow_of_two(BITS_TO_BYTES(bits))

Right, but as static inline to have stricter types.

Quoted 2 lines

There are a couple of places in spi/ that could use this and several iio drivers.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · David Lechner
Quoted 2 lines · Andy Shevchenko

On Tue, Apr 15, 2025 at 3:21 PM Marcelo Schmitt
<[email protected]> wrote:

...

Quoted 22 lines
Quoted 10 lines
Quoted 9 lines

drivers/iio/adc/ad4000.c static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st, · line 944

Quoted 3 lines
        xfers[1].rx_buf = &st->scan.data;
drivers/iio/adc/ad4000.c +1 -1
-       xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
+       xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;

But wouldn't be logical to have

       xfers[1].len = BITS_TO_BYTES(chan->scan_type.realbits);

?

Quoted 8 lines

No, SPI expects 1, 2 or 4 bytes, never 3. If realbits is 18, we need len = 4.

It would have to be:

        xfers[1].len = roundup_pow_of_two(BITS_TO_BYTES(chan->scan_type.realbits));

But that gets too long for 1 line

Actually there are a handful of drivers including SPI core that need that helper already, I would prefer to have a helper defined in spi.h.

Quoted 10 lines

, so I prefer what Marcelo wrote.

Quoted 4 lines

Maybe an idea for another day:

#define SPI_LEN_FOR_BITS(bits) roundup_pow_of_two(BITS_TO_BYTES(bits))

Right, but as static inline to have stricter types.

Quoted 2 lines

There are a couple of places in spi/ that could use this and several iio drivers.

Or even wider, in bitops.h / bit*.h somewhere. Let me craft a patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · David Lechner

On 4/15/25 1:02 PM, Andy Shevchenko wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines

It would have to be:

        xfers[1].len = roundup_pow_of_two(BITS_TO_BYTES(chan->scan_type.realbits));

But that gets too long for 1 line

Actually there are a handful of drivers including SPI core that need that helper already, I would prefer to have a helper defined in spi.h.

, so I prefer what Marcelo wrote.

Quoted 3 lines

Maybe an idea for another day:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

#define SPI_LEN_FOR_BITS(bits) roundup_pow_of_two(BITS_TO_BYTES(bits))

Right, but as static inline to have stricter types.

Quoted 2 lines

There are a couple of places in spi/ that could use this and several iio drivers.

Or even wider, in bitops.h / bit*.h somewhere. Let me craft a patch.

Just sent a mini-series, you are in Cc there.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#6 lore raw
Quoted 23 lines · On Wed, 16 Apr 2025 09:47:03 +0300 Andy Shevchenko <[email protected]> wrote:
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · David Lechner

On 4/15/25 1:02 PM, Andy Shevchenko wrote:

...

Quoted 15 lines
Quoted 15 lines
Quoted 5 lines

It would have to be:

        xfers[1].len = roundup_pow_of_two(BITS_TO_BYTES(chan->scan_type.realbits));

But that gets too long for 1 line

Actually there are a handful of drivers including SPI core that need that helper already, I would prefer to have a helper defined in spi.h.

, so I prefer what Marcelo wrote.

Quoted 4 lines

Maybe an idea for another day:

#define SPI_LEN_FOR_BITS(bits) roundup_pow_of_two(BITS_TO_BYTES(bits))

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Right, but as static inline to have stricter types.

Quoted 2 lines

There are a couple of places in spi/ that could use this and several iio drivers.

Or even wider, in bitops.h / bit*.h somewhere. Let me craft a patch.

Just sent a mini-series, you are in Cc there.

I've picked this one up for now. Assuming we'll cycle round to use the new helper once it's available in my upstream.

Thanks,

Jonathan

reviewer Reviewed-by on v1 1/1
#7 lore raw
In reply to Marcelo Schmitt
Quoted 8 lines · Marcelo Schmitt

Currently, SPI-Engine offload module always sends 32-bit data elements to DMA engine. Appropriately, when set for SPI offloading, the IIO driver uses 32 storagebits for IIO ADC channel buffer elements. However, setting SPI transfer length according to storagebits (32-bits in case of offload) can lead to unnecessarily long transfers for ADCs that are 16-bit or less precision. Adjust AD4000 single-shot read to run transfers of 2 bytes when that is enough to get all ADC data bits.

Quoted 4 lines
Fixes d0dba3df842f ("iio: adc: ad4000: Add support for SPI offload")

---