diff mbox series

[02/19] iio: accel: bma220: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

Message ID 20210501170121.512209-3-jic23@kernel.org (mailing list archive)
State New, archived
Headers show
Series IIO: Alignment fixes part 2 - struct used to ensure alignment | expand

Commit Message

Jonathan Cameron May 1, 2021, 5:01 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

To make code more readable, use a structure to express the channel
layout and ensure the timestamp is 8 byte aligned.

Found during an audit of all calls of this function.

Fixes: 194dc4c71413 ("iio: accel: Add triggered buffer support for BMA220")
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/iio/accel/bma220_spi.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Jonathan Cameron May 13, 2021, 5:26 p.m. UTC | #1
On Sat,  1 May 2021 18:01:04 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> To make code more readable, use a structure to express the channel
> layout and ensure the timestamp is 8 byte aligned.
> 
> Found during an audit of all calls of this function.
~Gah. Clearly wasn't on my best form when I originally wrote these.
Number of chans in this one is wrong as well.  Fixed to 3 as per the
comment that got dropped because it should have been obvious.

As you might imagine, the error in patch 1 made me look closer at
the rest of these whilst applying.
> 
> Fixes: 194dc4c71413 ("iio: accel: Add triggered buffer support for BMA220")
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Applied to the togreg branch of iio.git and pushed out as testing for the
autobuilders to poke at.

Thanks,

Jonathan

> ---
>  drivers/iio/accel/bma220_spi.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/accel/bma220_spi.c b/drivers/iio/accel/bma220_spi.c
> index 36fc9876dbca..2802ce1852ef 100644
> --- a/drivers/iio/accel/bma220_spi.c
> +++ b/drivers/iio/accel/bma220_spi.c
> @@ -63,7 +63,11 @@ static const int bma220_scale_table[][2] = {
>  struct bma220_data {
>  	struct spi_device *spi_device;
>  	struct mutex lock;
> -	s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 8x8 timestamp */
> +	struct {
> +		s8 chans[2];
> +		/* Ensure timestamp is naturally aligned. */
> +		s64 timestamp __aligned(8);
> +	} scan;
>  	u8 tx_buf[2] ____cacheline_aligned;
>  };
>  
> @@ -94,12 +98,12 @@ static irqreturn_t bma220_trigger_handler(int irq, void *p)
>  
>  	mutex_lock(&data->lock);
>  	data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
> -	ret = spi_write_then_read(spi, data->tx_buf, 1, data->buffer,
> +	ret = spi_write_then_read(spi, data->tx_buf, 1, &data->scan.chans,
>  				  ARRAY_SIZE(bma220_channels) - 1);
>  	if (ret < 0)
>  		goto err;
>  
> -	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
> +	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
>  					   pf->timestamp);
>  err:
>  	mutex_unlock(&data->lock);
diff mbox series

Patch

diff --git a/drivers/iio/accel/bma220_spi.c b/drivers/iio/accel/bma220_spi.c
index 36fc9876dbca..2802ce1852ef 100644
--- a/drivers/iio/accel/bma220_spi.c
+++ b/drivers/iio/accel/bma220_spi.c
@@ -63,7 +63,11 @@  static const int bma220_scale_table[][2] = {
 struct bma220_data {
 	struct spi_device *spi_device;
 	struct mutex lock;
-	s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 8x8 timestamp */
+	struct {
+		s8 chans[2];
+		/* Ensure timestamp is naturally aligned. */
+		s64 timestamp __aligned(8);
+	} scan;
 	u8 tx_buf[2] ____cacheline_aligned;
 };
 
@@ -94,12 +98,12 @@  static irqreturn_t bma220_trigger_handler(int irq, void *p)
 
 	mutex_lock(&data->lock);
 	data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
-	ret = spi_write_then_read(spi, data->tx_buf, 1, data->buffer,
+	ret = spi_write_then_read(spi, data->tx_buf, 1, &data->scan.chans,
 				  ARRAY_SIZE(bma220_channels) - 1);
 	if (ret < 0)
 		goto err;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
 					   pf->timestamp);
 err:
 	mutex_unlock(&data->lock);