[PATCH v2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack

From: Alexandru Ardelean
Date: Fri Nov 13 2020 - 04:41:25 EST


From: Lars-Peter Clausen <lars@xxxxxxxxxx>

Use a heap allocated memory for the SPI transfer buffer. Using stack memory
can corrupt stack memory when using DMA on some systems.

This change moves the buffer from the stack of the trigger handler call to
the heap of the buffer of the state struct. The size increases takes into
account the alignment for the timestamp, which is 8 bytes.
So the buffer is put at an offset of 8 bytes.

Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
Signed-off-by: Lars-Peter Clausen <lars@xxxxxxxxxx>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@xxxxxxxxxx>
---

Changelog v1 -> v2:
* bumped the buffer on state struct to 24 bytes
* increased the offset to 8 bytes to account for the timestamp alignment

drivers/iio/adc/ad_sigma_delta.c | 2 +-
include/linux/iio/adc/ad_sigma_delta.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index 86039e9ecaca..9f730c9d6aaa 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -395,9 +395,9 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
+ uint8_t *data = &sigma_delta->data[8];
unsigned int reg_size;
unsigned int data_reg;
- uint8_t data[16];

memset(data, 0x00, 16);

diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
index a3a838dcf8e4..8fb74755f873 100644
--- a/include/linux/iio/adc/ad_sigma_delta.h
+++ b/include/linux/iio/adc/ad_sigma_delta.h
@@ -80,7 +80,7 @@ struct ad_sigma_delta {
* DMA (thus cache coherency maintenance) requires the
* transfer buffers to live in their own cache lines.
*/
- uint8_t data[4] ____cacheline_aligned;
+ uint8_t data[24] ____cacheline_aligned;
};

static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
--
2.27.0