[PATCH v5 1/3] iio: imu: inv_icm42600: move structure DMA buffers at the end

Jean-Baptiste Maneyrol via B4 Relay posted 3 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH v5 1/3] iio: imu: inv_icm42600: move structure DMA buffers at the end
Posted by Jean-Baptiste Maneyrol via B4 Relay 3 months, 2 weeks ago
From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>

Move DMA buffers at the end of the structure to avoid overflow
bugs with unexpected effect.

struct inv_icm42600_fifo has a DMA buffer at the end.

Signed-off-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
---
 drivers/iio/imu/inv_icm42600/inv_icm42600.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600.h b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
index 55ed1ddaa8cb5dd410d17db3866fa0f22f18e9d2..9b2cce172670c5513f18d5979a5ff563e9af4cb3 100644
--- a/drivers/iio/imu/inv_icm42600/inv_icm42600.h
+++ b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
@@ -148,9 +148,9 @@ struct inv_icm42600_suspended {
  *  @suspended:		suspended sensors configuration.
  *  @indio_gyro:	gyroscope IIO device.
  *  @indio_accel:	accelerometer IIO device.
- *  @buffer:		data transfer buffer aligned for DMA.
- *  @fifo:		FIFO management structure.
  *  @timestamp:		interrupt timestamps.
+ *  @fifo:		FIFO management structure.
+ *  @buffer:		data transfer buffer aligned for DMA.
  */
 struct inv_icm42600_state {
 	struct mutex lock;
@@ -164,12 +164,12 @@ struct inv_icm42600_state {
 	struct inv_icm42600_suspended suspended;
 	struct iio_dev *indio_gyro;
 	struct iio_dev *indio_accel;
-	u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
-	struct inv_icm42600_fifo fifo;
 	struct {
 		s64 gyro;
 		s64 accel;
 	} timestamp;
+	struct inv_icm42600_fifo fifo;
+	u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
 };
 
 

-- 
2.49.0
Re: [PATCH v5 1/3] iio: imu: inv_icm42600: move structure DMA buffers at the end
Posted by David Lechner 3 months, 2 weeks ago
On Mon, Jun 23, 2025 at 6:56 AM Jean-Baptiste Maneyrol via B4 Relay
<devnull+jean-baptiste.maneyrol.tdk.com@kernel.org> wrote:
>
> From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
>
> Move DMA buffers at the end of the structure to avoid overflow
> bugs with unexpected effect.

If there is an overflow bug, we should fix that rather than hiding it.

If I misunderstood the problem and timestamp and fifo should not be in
the DMA aligned area and there is a problem with DMA cache writing
over them, then I think we should reword the commit message.

>
> struct inv_icm42600_fifo has a DMA buffer at the end.
>
> Signed-off-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
> ---
>  drivers/iio/imu/inv_icm42600/inv_icm42600.h | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600.h b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> index 55ed1ddaa8cb5dd410d17db3866fa0f22f18e9d2..9b2cce172670c5513f18d5979a5ff563e9af4cb3 100644
> --- a/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> @@ -148,9 +148,9 @@ struct inv_icm42600_suspended {
>   *  @suspended:                suspended sensors configuration.
>   *  @indio_gyro:       gyroscope IIO device.
>   *  @indio_accel:      accelerometer IIO device.
> - *  @buffer:           data transfer buffer aligned for DMA.
> - *  @fifo:             FIFO management structure.
>   *  @timestamp:                interrupt timestamps.
> + *  @fifo:             FIFO management structure.
> + *  @buffer:           data transfer buffer aligned for DMA.
>   */
>  struct inv_icm42600_state {
>         struct mutex lock;
> @@ -164,12 +164,12 @@ struct inv_icm42600_state {
>         struct inv_icm42600_suspended suspended;
>         struct iio_dev *indio_gyro;
>         struct iio_dev *indio_accel;
> -       u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
> -       struct inv_icm42600_fifo fifo;
>         struct {
>                 s64 gyro;
>                 s64 accel;
>         } timestamp;
> +       struct inv_icm42600_fifo fifo;

I didn't look at how the drivers use timestamp and fifo, but if they
are passed as a buffer to SPI, then they need to stay in the DMA
aligned area of the struct.

> +       u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
>  };
>
>
>
> --
> 2.49.0
>
>