All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
@ 2020-11-12  9:10 Alexandru Ardelean
  2020-11-12  9:54 ` Lars-Peter Clausen
  2020-11-13  9:40 ` [PATCH v2] " Alexandru Ardelean
  0 siblings, 2 replies; 13+ messages in thread
From: Alexandru Ardelean @ 2020-11-12  9:10 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: alexandru.ardelean, lars, jic23

From: Lars-Peter Clausen <lars@metafoo.de>

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 adds 4 bytes at the end of the current DMA buffer, which will
be used by the trigger handler.
This is required because the first 4 bytes are reserved for register data.

Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/adc/ad_sigma_delta.c       | 4 ++--
 include/linux/iio/adc/ad_sigma_delta.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index 86039e9ecaca..33297f26508a 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -395,11 +395,11 @@ 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[4];
 	unsigned int reg_size;
 	unsigned int data_reg;
-	uint8_t data[16];
 
-	memset(data, 0x00, 16);
+	memset(data, 0x00, 4);
 
 	reg_size = indio_dev->channels[0].scan_type.realbits +
 			indio_dev->channels[0].scan_type.shift;
diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
index a3a838dcf8e4..ac4ac4752c62 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[8] ____cacheline_aligned;
 };
 
 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-12  9:10 [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack Alexandru Ardelean
@ 2020-11-12  9:54 ` Lars-Peter Clausen
  2020-11-12 10:14   ` Alexandru Ardelean
  2020-11-13  9:40 ` [PATCH v2] " Alexandru Ardelean
  1 sibling, 1 reply; 13+ messages in thread
From: Lars-Peter Clausen @ 2020-11-12  9:54 UTC (permalink / raw)
  To: Alexandru Ardelean, linux-iio, linux-kernel; +Cc: jic23

On 11/12/20 10:10 AM, Alexandru Ardelean wrote:
> From: Lars-Peter Clausen <lars@metafoo.de>
>
> 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 adds 4 bytes at the end of the current DMA buffer, which will
> be used by the trigger handler.
> This is required because the first 4 bytes are reserved for register data.
>
> Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---
>   drivers/iio/adc/ad_sigma_delta.c       | 4 ++--
>   include/linux/iio/adc/ad_sigma_delta.h | 2 +-
>   2 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> index 86039e9ecaca..33297f26508a 100644
> --- a/drivers/iio/adc/ad_sigma_delta.c
> +++ b/drivers/iio/adc/ad_sigma_delta.c
> @@ -395,11 +395,11 @@ 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[4];
>   	unsigned int reg_size;
>   	unsigned int data_reg;
> -	uint8_t data[16];
>   
> -	memset(data, 0x00, 16);
> +	memset(data, 0x00, 4);

Younger me didn't know what he was doing, this is wrong. We need the 
extra space for the padding and timestamp.

We also can't put the beginning of the buffer at an 4 byte offset since 
it needs to be 8 byte aligned for the timestamp.

>   
>   	reg_size = indio_dev->channels[0].scan_type.realbits +
>   			indio_dev->channels[0].scan_type.shift;
> diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
> index a3a838dcf8e4..ac4ac4752c62 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[8] ____cacheline_aligned;
>   };
>   
>   static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-12  9:54 ` Lars-Peter Clausen
@ 2020-11-12 10:14   ` Alexandru Ardelean
  2020-11-12 10:52     ` Lars-Peter Clausen
  0 siblings, 1 reply; 13+ messages in thread
From: Alexandru Ardelean @ 2020-11-12 10:14 UTC (permalink / raw)
  To: Lars-Peter Clausen; +Cc: Alexandru Ardelean, linux-iio, LKML, Jonathan Cameron

On Thu, Nov 12, 2020 at 11:55 AM Lars-Peter Clausen <lars@metafoo.de> wrote:
>
> On 11/12/20 10:10 AM, Alexandru Ardelean wrote:
> > From: Lars-Peter Clausen <lars@metafoo.de>
> >
> > 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 adds 4 bytes at the end of the current DMA buffer, which will
> > be used by the trigger handler.
> > This is required because the first 4 bytes are reserved for register data.
> >
> > Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
> > Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> > ---
> >   drivers/iio/adc/ad_sigma_delta.c       | 4 ++--
> >   include/linux/iio/adc/ad_sigma_delta.h | 2 +-
> >   2 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> > index 86039e9ecaca..33297f26508a 100644
> > --- a/drivers/iio/adc/ad_sigma_delta.c
> > +++ b/drivers/iio/adc/ad_sigma_delta.c
> > @@ -395,11 +395,11 @@ 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[4];
> >       unsigned int reg_size;
> >       unsigned int data_reg;
> > -     uint8_t data[16];
> >
> > -     memset(data, 0x00, 16);
> > +     memset(data, 0x00, 4);
>
> Younger me didn't know what he was doing, this is wrong. We need the
> extra space for the padding and timestamp.
>
> We also can't put the beginning of the buffer at an 4 byte offset since
> it needs to be 8 byte aligned for the timestamp.

I'll correct this.
I was re-spinning this out of some old patches and discussions on this
that I have.
So, then this becomes 24 bytes? Or 16?

Something like:
uint8_t                         data[24] ____cacheline_aligned;

uint8_t *data = &sigma_delta->data[8];


>
> >
> >       reg_size = indio_dev->channels[0].scan_type.realbits +
> >                       indio_dev->channels[0].scan_type.shift;
> > diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
> > index a3a838dcf8e4..ac4ac4752c62 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[8] ____cacheline_aligned;
> >   };
> >
> >   static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-12 10:14   ` Alexandru Ardelean
@ 2020-11-12 10:52     ` Lars-Peter Clausen
  0 siblings, 0 replies; 13+ messages in thread
From: Lars-Peter Clausen @ 2020-11-12 10:52 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: Alexandru Ardelean, linux-iio, LKML, Jonathan Cameron

On 11/12/20 11:14 AM, Alexandru Ardelean wrote:
> On Thu, Nov 12, 2020 at 11:55 AM Lars-Peter Clausen <lars@metafoo.de> wrote:
>> On 11/12/20 10:10 AM, Alexandru Ardelean wrote:
>>> From: Lars-Peter Clausen <lars@metafoo.de>
>>>
>>> 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 adds 4 bytes at the end of the current DMA buffer, which will
>>> be used by the trigger handler.
>>> This is required because the first 4 bytes are reserved for register data.
>>>
>>> Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
>>> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
>>> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
>>> ---
>>>    drivers/iio/adc/ad_sigma_delta.c       | 4 ++--
>>>    include/linux/iio/adc/ad_sigma_delta.h | 2 +-
>>>    2 files changed, 3 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
>>> index 86039e9ecaca..33297f26508a 100644
>>> --- a/drivers/iio/adc/ad_sigma_delta.c
>>> +++ b/drivers/iio/adc/ad_sigma_delta.c
>>> @@ -395,11 +395,11 @@ 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[4];
>>>        unsigned int reg_size;
>>>        unsigned int data_reg;
>>> -     uint8_t data[16];
>>>
>>> -     memset(data, 0x00, 16);
>>> +     memset(data, 0x00, 4);
>> Younger me didn't know what he was doing, this is wrong. We need the
>> extra space for the padding and timestamp.
>>
>> We also can't put the beginning of the buffer at an 4 byte offset since
>> it needs to be 8 byte aligned for the timestamp.
> I'll correct this.
> I was re-spinning this out of some old patches and discussions on this
> that I have.
> So, then this becomes 24 bytes? Or 16?
>
> Something like:
> uint8_t                         data[24] ____cacheline_aligned;
>
> uint8_t *data = &sigma_delta->data[8];

Yes.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-12  9:10 [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack Alexandru Ardelean
  2020-11-12  9:54 ` Lars-Peter Clausen
@ 2020-11-13  9:40 ` Alexandru Ardelean
  2020-11-14 16:20   ` Jonathan Cameron
  2020-11-24 12:38   ` [PATCH v3] " Alexandru Ardelean
  1 sibling, 2 replies; 13+ messages in thread
From: Alexandru Ardelean @ 2020-11-13  9:40 UTC (permalink / raw)
  To: linux-iio, linux-kernel
  Cc: alexandru.tachici, jic23, lars, Alexandru Ardelean

From: Lars-Peter Clausen <lars@metafoo.de>

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@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---

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


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-13  9:40 ` [PATCH v2] " Alexandru Ardelean
@ 2020-11-14 16:20   ` Jonathan Cameron
  2020-11-19  8:30     ` Alexandru Ardelean
  2020-11-24 12:38   ` [PATCH v3] " Alexandru Ardelean
  1 sibling, 1 reply; 13+ messages in thread
From: Jonathan Cameron @ 2020-11-14 16:20 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, linux-kernel, alexandru.tachici, lars

On Fri, 13 Nov 2020 11:40:59 +0200
Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:

> From: Lars-Peter Clausen <lars@metafoo.de>
> 
> 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@metafoo.de>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>

There are neater options for solving this problem - see inline.

In particular I don't think you have a problem with setting the
rx and tx buffers to use the same memory.

> ---
> 
> 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.
>  	 */

If you do end up with something like this, it needs a clear explanation of 'why'
the size is 24 bytes.  No good just having it in the patch description.

> -	uint8_t				data[4] ____cacheline_aligned;
> +	uint8_t				data[24] ____cacheline_aligned;

This is downright confusing.  I'd just split the buffer into tx and rx
parts.   The first (doesn't matter which) needs to be marked __cacheline_aligned.
If the rx is second mark it __aligned(8) to force that to be appropriate for
the timestamp.

Or... (I haven't checked thoroughly for this from point of view of how it is used
in the drivers) use the same buffer for tx and rx.  That is supposed to be safe for
SPI drivers though wonderfully there is a ? after the statement of that in
include/linux/spi.h.  I think that is just pointing out that microwire doesn't
support duplex rather than saying it's invalid in general...



>  };
>  
>  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-14 16:20   ` Jonathan Cameron
@ 2020-11-19  8:30     ` Alexandru Ardelean
  2020-11-21 16:41       ` Jonathan Cameron
  0 siblings, 1 reply; 13+ messages in thread
From: Alexandru Ardelean @ 2020-11-19  8:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Alexandru Ardelean, linux-iio, LKML, alexandru.tachici,
	Lars-Peter Clausen

On Sat, Nov 14, 2020 at 6:20 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Fri, 13 Nov 2020 11:40:59 +0200
> Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:
>
> > From: Lars-Peter Clausen <lars@metafoo.de>
> >
> > 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@metafoo.de>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
>
> There are neater options for solving this problem - see inline.
>
> In particular I don't think you have a problem with setting the
> rx and tx buffers to use the same memory.
>
> > ---
> >
> > 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.
> >        */
>
> If you do end up with something like this, it needs a clear explanation of 'why'
> the size is 24 bytes.  No good just having it in the patch description.
>
> > -     uint8_t                         data[4] ____cacheline_aligned;
> > +     uint8_t                         data[24] ____cacheline_aligned;
>
> This is downright confusing.  I'd just split the buffer into tx and rx
> parts.   The first (doesn't matter which) needs to be marked __cacheline_aligned.
> If the rx is second mark it __aligned(8) to force that to be appropriate for
> the timestamp.
>
> Or... (I haven't checked thoroughly for this from point of view of how it is used
> in the drivers) use the same buffer for tx and rx.  That is supposed to be safe for
> SPI drivers though wonderfully there is a ? after the statement of that in
> include/linux/spi.h.  I think that is just pointing out that microwire doesn't
> support duplex rather than saying it's invalid in general...

I'm a bit paranoid to use the same buffer for RX & TX [in general].
It sounds like this could hide some bugs in some weird DMA implementations.
The DMA implementations could be fine on their own, but they wouldn't
expect that TX & RX buffers point to the same place.

>
>
>
> >  };
> >
> >  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-19  8:30     ` Alexandru Ardelean
@ 2020-11-21 16:41       ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2020-11-21 16:41 UTC (permalink / raw)
  To: Alexandru Ardelean
  Cc: Alexandru Ardelean, linux-iio, LKML, alexandru.tachici,
	Lars-Peter Clausen

On Thu, 19 Nov 2020 10:30:02 +0200
Alexandru Ardelean <ardeleanalex@gmail.com> wrote:

> On Sat, Nov 14, 2020 at 6:20 PM Jonathan Cameron <jic23@kernel.org> wrote:
> >
> > On Fri, 13 Nov 2020 11:40:59 +0200
> > Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:
> >  
> > > From: Lars-Peter Clausen <lars@metafoo.de>
> > >
> > > 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@metafoo.de>
> > > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>  
> >
> > There are neater options for solving this problem - see inline.
> >
> > In particular I don't think you have a problem with setting the
> > rx and tx buffers to use the same memory.
> >  
> > > ---
> > >
> > > 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.
> > >        */  
> >
> > If you do end up with something like this, it needs a clear explanation of 'why'
> > the size is 24 bytes.  No good just having it in the patch description.
> >  
> > > -     uint8_t                         data[4] ____cacheline_aligned;
> > > +     uint8_t                         data[24] ____cacheline_aligned;  
> >
> > This is downright confusing.  I'd just split the buffer into tx and rx
> > parts.   The first (doesn't matter which) needs to be marked __cacheline_aligned.
> > If the rx is second mark it __aligned(8) to force that to be appropriate for
> > the timestamp.
> >
> > Or... (I haven't checked thoroughly for this from point of view of how it is used
> > in the drivers) use the same buffer for tx and rx.  That is supposed to be safe for
> > SPI drivers though wonderfully there is a ? after the statement of that in
> > include/linux/spi.h.  I think that is just pointing out that microwire doesn't
> > support duplex rather than saying it's invalid in general...  
> 
> I'm a bit paranoid to use the same buffer for RX & TX [in general].
> It sounds like this could hide some bugs in some weird DMA implementations.
> The DMA implementations could be fine on their own, but they wouldn't
> expect that TX & RX buffers point to the same place.

Whilst in theory it should either be fine, or the hardware should use
a bounce buffer if it's not, I can understand your paranoia so
I'm fine with separate buffers.

Jonathan

> 
> >
> >
> >  
> > >  };
> > >
> > >  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,  
> >  


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-13  9:40 ` [PATCH v2] " Alexandru Ardelean
  2020-11-14 16:20   ` Jonathan Cameron
@ 2020-11-24 12:38   ` Alexandru Ardelean
  2020-11-28 13:34     ` Jonathan Cameron
  1 sibling, 1 reply; 13+ messages in thread
From: Alexandru Ardelean @ 2020-11-24 12:38 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: lars, alexandru.tachici, Alexandru Ardelean

From: Lars-Peter Clausen <lars@metafoo.de>

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.

The 'data' buffer is split into 'tx_buf' and 'rx_buf', to make a clearer
separation of which part of the buffer should be used for TX & RX.

Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/adc/ad_sigma_delta.c       | 18 ++++++++----------
 include/linux/iio/adc/ad_sigma_delta.h |  6 +++++-
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index 86039e9ecaca..3a6f239d4acc 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -57,7 +57,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm);
 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
 	unsigned int size, unsigned int val)
 {
-	uint8_t *data = sigma_delta->data;
+	uint8_t *data = sigma_delta->tx_buf;
 	struct spi_transfer t = {
 		.tx_buf		= data,
 		.len		= size + 1,
@@ -99,7 +99,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg);
 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
 	unsigned int reg, unsigned int size, uint8_t *val)
 {
-	uint8_t *data = sigma_delta->data;
+	uint8_t *data = sigma_delta->tx_buf;
 	int ret;
 	struct spi_transfer t[] = {
 		{
@@ -146,22 +146,22 @@ int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
 {
 	int ret;
 
-	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
+	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
 	if (ret < 0)
 		goto out;
 
 	switch (size) {
 	case 4:
-		*val = get_unaligned_be32(sigma_delta->data);
+		*val = get_unaligned_be32(sigma_delta->rx_buf);
 		break;
 	case 3:
-		*val = get_unaligned_be24(&sigma_delta->data[0]);
+		*val = get_unaligned_be24(sigma_delta->rx_buf);
 		break;
 	case 2:
-		*val = get_unaligned_be16(sigma_delta->data);
+		*val = get_unaligned_be16(sigma_delta->rx_buf);
 		break;
 	case 1:
-		*val = sigma_delta->data[0];
+		*val = sigma_delta->rx_buf[0];
 		break;
 	default:
 		ret = -EINVAL;
@@ -395,11 +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->rx_buf;
 	unsigned int reg_size;
 	unsigned int data_reg;
-	uint8_t data[16];
-
-	memset(data, 0x00, 16);
 
 	reg_size = indio_dev->channels[0].scan_type.realbits +
 			indio_dev->channels[0].scan_type.shift;
diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
index a3a838dcf8e4..efc2413abaf4 100644
--- a/include/linux/iio/adc/ad_sigma_delta.h
+++ b/include/linux/iio/adc/ad_sigma_delta.h
@@ -79,8 +79,12 @@ struct ad_sigma_delta {
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
+	 * 'tx_buf' is up to 32 bits.
+	 * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp,
+	 * rounded to 16 bytes to take into account padding.
 	 */
-	uint8_t				data[4] ____cacheline_aligned;
+	uint8_t				tx_buf[4] ____cacheline_aligned;
+	uint8_t				rx_buf[16] __aligned(8);
 };
 
 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2020-11-24 12:38   ` [PATCH v3] " Alexandru Ardelean
@ 2020-11-28 13:34     ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2020-11-28 13:34 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, linux-kernel, lars, alexandru.tachici

On Tue, 24 Nov 2020 14:38:07 +0200
Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:

> From: Lars-Peter Clausen <lars@metafoo.de>
> 
> 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.
> 
> The 'data' buffer is split into 'tx_buf' and 'rx_buf', to make a clearer
> separation of which part of the buffer should be used for TX & RX.
> 
> Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Given where we are in the cycle and the fact this has been there a long time
I've queued it up for the next merge window.  Also marked it for stable so
it will get backported in the medium term.

Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to do their usual magic.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/ad_sigma_delta.c       | 18 ++++++++----------
>  include/linux/iio/adc/ad_sigma_delta.h |  6 +++++-
>  2 files changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> index 86039e9ecaca..3a6f239d4acc 100644
> --- a/drivers/iio/adc/ad_sigma_delta.c
> +++ b/drivers/iio/adc/ad_sigma_delta.c
> @@ -57,7 +57,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm);
>  int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
>  	unsigned int size, unsigned int val)
>  {
> -	uint8_t *data = sigma_delta->data;
> +	uint8_t *data = sigma_delta->tx_buf;
>  	struct spi_transfer t = {
>  		.tx_buf		= data,
>  		.len		= size + 1,
> @@ -99,7 +99,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg);
>  static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
>  	unsigned int reg, unsigned int size, uint8_t *val)
>  {
> -	uint8_t *data = sigma_delta->data;
> +	uint8_t *data = sigma_delta->tx_buf;
>  	int ret;
>  	struct spi_transfer t[] = {
>  		{
> @@ -146,22 +146,22 @@ int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
>  {
>  	int ret;
>  
> -	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
> +	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
>  	if (ret < 0)
>  		goto out;
>  
>  	switch (size) {
>  	case 4:
> -		*val = get_unaligned_be32(sigma_delta->data);
> +		*val = get_unaligned_be32(sigma_delta->rx_buf);
>  		break;
>  	case 3:
> -		*val = get_unaligned_be24(&sigma_delta->data[0]);
> +		*val = get_unaligned_be24(sigma_delta->rx_buf);
>  		break;
>  	case 2:
> -		*val = get_unaligned_be16(sigma_delta->data);
> +		*val = get_unaligned_be16(sigma_delta->rx_buf);
>  		break;
>  	case 1:
> -		*val = sigma_delta->data[0];
> +		*val = sigma_delta->rx_buf[0];
>  		break;
>  	default:
>  		ret = -EINVAL;
> @@ -395,11 +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->rx_buf;
>  	unsigned int reg_size;
>  	unsigned int data_reg;
> -	uint8_t data[16];
> -
> -	memset(data, 0x00, 16);
>  
>  	reg_size = indio_dev->channels[0].scan_type.realbits +
>  			indio_dev->channels[0].scan_type.shift;
> diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
> index a3a838dcf8e4..efc2413abaf4 100644
> --- a/include/linux/iio/adc/ad_sigma_delta.h
> +++ b/include/linux/iio/adc/ad_sigma_delta.h
> @@ -79,8 +79,12 @@ struct ad_sigma_delta {
>  	/*
>  	 * DMA (thus cache coherency maintenance) requires the
>  	 * transfer buffers to live in their own cache lines.
> +	 * 'tx_buf' is up to 32 bits.
> +	 * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp,
> +	 * rounded to 16 bytes to take into account padding.
>  	 */
> -	uint8_t				data[4] ____cacheline_aligned;
> +	uint8_t				tx_buf[4] ____cacheline_aligned;
> +	uint8_t				rx_buf[16] __aligned(8);
>  };
>  
>  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2019-04-22  8:56   ` Jonathan Cameron
@ 2019-04-22 10:28     ` Ardelean, Alexandru
  0 siblings, 0 replies; 13+ messages in thread
From: Ardelean, Alexandru @ 2019-04-22 10:28 UTC (permalink / raw)
  To: jic23; +Cc: lars, linux-iio

On Mon, 2019-04-22 at 09:56 +0100, Jonathan Cameron wrote:
> [External]
> 
> 
> On Tue, 16 Apr 2019 11:19:16 +0300
> Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:
> 
> > From: Lars-Peter Clausen <lars@metafoo.de>
> > 
> > Use a heap allocated memory for the SPI transfer buffer. Using stack
> > memory
> > will not work on some architectures when using DMA.
> > 
> > This also avoids potential stack corruption, as it makes sure that the
> > buffer is in it's own cacheline and (obviously) not on the stack.
> > 
> > Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta
> > devices")
> > Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> 
> Hi Alex,
> 
> Thanks for the update.
> 
> I'm unclear why we can't just use one large buffer for both cases?
> i.e put the reg_data usecases in the buf_data buffer (renamed).
> 

That's a good point.
I didn't think much of it.


Looking through the code, the only argument for doing it like this
(separate buffer) is that the change is less complicated to look at, since
it makes no assumptions about offset in the buffer.

At a later point in time, this `buf_data` will be replaced with a kzalloc-
ed buffer [when adding sequencer support for SigmaDelta].

It may only make sense to have this change [whether 2 buffers or a single
buffer] for stable, to fix the cacheline issue.

I'll take a closer look at this.
It should work well to just increase the existing buffer and re-use that.


> Jonathan
> > ---
> > Changelog v1 -> v2:
> > * added ____cacheline_aligned to `buf_data`
> > * extended comment about cacheline
> > * added fixes tag
> > 
> >  drivers/iio/adc/ad_sigma_delta.c       | 22 +++++++++++-----------
> >  include/linux/iio/adc/ad_sigma_delta.h |  3 ++-
> >  2 files changed, 13 insertions(+), 12 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/ad_sigma_delta.c
> > b/drivers/iio/adc/ad_sigma_delta.c
> > index af6cbc683214..02d9049b9218 100644
> > --- a/drivers/iio/adc/ad_sigma_delta.c
> > +++ b/drivers/iio/adc/ad_sigma_delta.c
> > @@ -58,7 +58,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm);
> >  int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int
> > reg,
> >       unsigned int size, unsigned int val)
> >  {
> > -     uint8_t *data = sigma_delta->data;
> > +     uint8_t *data = sigma_delta->reg_data;
> >       struct spi_transfer t = {
> >               .tx_buf         = data,
> >               .len            = size + 1,
> > @@ -102,7 +102,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg);
> >  static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
> >       unsigned int reg, unsigned int size, uint8_t *val)
> >  {
> > -     uint8_t *data = sigma_delta->data;
> > +     uint8_t *data = sigma_delta->reg_data;
> >       int ret;
> >       struct spi_transfer t[] = {
> >               {
> > @@ -148,24 +148,24 @@ int ad_sd_read_reg(struct ad_sigma_delta
> > *sigma_delta,
> >  {
> >       int ret;
> > 
> > -     ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta-
> > >data);
> > +     ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta-
> > >reg_data);
> >       if (ret < 0)
> >               goto out;
> > 
> >       switch (size) {
> >       case 4:
> > -             *val = get_unaligned_be32(sigma_delta->data);
> > +             *val = get_unaligned_be32(sigma_delta->reg_data);
> >               break;
> >       case 3:
> > -             *val = (sigma_delta->data[0] << 16) |
> > -                     (sigma_delta->data[1] << 8) |
> > -                     sigma_delta->data[2];
> > +             *val = (sigma_delta->reg_data[0] << 16) |
> > +                     (sigma_delta->reg_data[1] << 8) |
> > +                     sigma_delta->reg_data[2];
> >               break;
> >       case 2:
> > -             *val = get_unaligned_be16(sigma_delta->data);
> > +             *val = get_unaligned_be16(sigma_delta->reg_data);
> >               break;
> >       case 1:
> > -             *val = sigma_delta->data[0];
> > +             *val = sigma_delta->reg_data[0];
> >               break;
> >       default:
> >               ret = -EINVAL;
> > @@ -403,12 +403,12 @@ 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->buf_data;
> >       unsigned int reg_size;
> >       unsigned int data_reg;
> > -     uint8_t data[16];
> >       int ret;
> > 
> > -     memset(data, 0x00, 16);
> > +     memset(sigma_delta->buf_data, 0x00, sizeof(sigma_delta-
> > >buf_data));
> > 
> >       reg_size = indio_dev->channels[0].scan_type.realbits +
> >                       indio_dev->channels[0].scan_type.shift;
> > diff --git a/include/linux/iio/adc/ad_sigma_delta.h
> > b/include/linux/iio/adc/ad_sigma_delta.h
> > index 6e9fb1932dde..d95216ce8a94 100644
> > --- a/include/linux/iio/adc/ad_sigma_delta.h
> > +++ b/include/linux/iio/adc/ad_sigma_delta.h
> > @@ -79,7 +79,8 @@ 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                         reg_data[4]
> > ____cacheline_aligned;
> > +     uint8_t                         buf_data[16]
> > ____cacheline_aligned;
> 
> The usual assumption is that a device is safe against itself, so we can't
> get cacheline corruption in a single action from the device to itself.
> This does make me wonder why we have two buffers like this at all though!
> >  };
> > 
> >  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta
> > *sd,
> 
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2019-04-16  8:19 ` [PATCH V2] " Alexandru Ardelean
@ 2019-04-22  8:56   ` Jonathan Cameron
  2019-04-22 10:28     ` Ardelean, Alexandru
  0 siblings, 1 reply; 13+ messages in thread
From: Jonathan Cameron @ 2019-04-22  8:56 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, Lars-Peter Clausen

On Tue, 16 Apr 2019 11:19:16 +0300
Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:

> From: Lars-Peter Clausen <lars@metafoo.de>
> 
> Use a heap allocated memory for the SPI transfer buffer. Using stack memory
> will not work on some architectures when using DMA.
> 
> This also avoids potential stack corruption, as it makes sure that the
> buffer is in it's own cacheline and (obviously) not on the stack.
> 
> Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Hi Alex,

Thanks for the update.

I'm unclear why we can't just use one large buffer for both cases?
i.e put the reg_data usecases in the buf_data buffer (renamed).

Jonathan
> ---
> Changelog v1 -> v2:
> * added ____cacheline_aligned to `buf_data`
> * extended comment about cacheline
> * added fixes tag
> 
>  drivers/iio/adc/ad_sigma_delta.c       | 22 +++++++++++-----------
>  include/linux/iio/adc/ad_sigma_delta.h |  3 ++-
>  2 files changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> index af6cbc683214..02d9049b9218 100644
> --- a/drivers/iio/adc/ad_sigma_delta.c
> +++ b/drivers/iio/adc/ad_sigma_delta.c
> @@ -58,7 +58,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm);
>  int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
>  	unsigned int size, unsigned int val)
>  {
> -	uint8_t *data = sigma_delta->data;
> +	uint8_t *data = sigma_delta->reg_data;
>  	struct spi_transfer t = {
>  		.tx_buf		= data,
>  		.len		= size + 1,
> @@ -102,7 +102,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg);
>  static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
>  	unsigned int reg, unsigned int size, uint8_t *val)
>  {
> -	uint8_t *data = sigma_delta->data;
> +	uint8_t *data = sigma_delta->reg_data;
>  	int ret;
>  	struct spi_transfer t[] = {
>  		{
> @@ -148,24 +148,24 @@ int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
>  {
>  	int ret;
>  
> -	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
> +	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->reg_data);
>  	if (ret < 0)
>  		goto out;
>  
>  	switch (size) {
>  	case 4:
> -		*val = get_unaligned_be32(sigma_delta->data);
> +		*val = get_unaligned_be32(sigma_delta->reg_data);
>  		break;
>  	case 3:
> -		*val = (sigma_delta->data[0] << 16) |
> -			(sigma_delta->data[1] << 8) |
> -			sigma_delta->data[2];
> +		*val = (sigma_delta->reg_data[0] << 16) |
> +			(sigma_delta->reg_data[1] << 8) |
> +			sigma_delta->reg_data[2];
>  		break;
>  	case 2:
> -		*val = get_unaligned_be16(sigma_delta->data);
> +		*val = get_unaligned_be16(sigma_delta->reg_data);
>  		break;
>  	case 1:
> -		*val = sigma_delta->data[0];
> +		*val = sigma_delta->reg_data[0];
>  		break;
>  	default:
>  		ret = -EINVAL;
> @@ -403,12 +403,12 @@ 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->buf_data;
>  	unsigned int reg_size;
>  	unsigned int data_reg;
> -	uint8_t data[16];
>  	int ret;
>  
> -	memset(data, 0x00, 16);
> +	memset(sigma_delta->buf_data, 0x00, sizeof(sigma_delta->buf_data));
>  
>  	reg_size = indio_dev->channels[0].scan_type.realbits +
>  			indio_dev->channels[0].scan_type.shift;
> diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
> index 6e9fb1932dde..d95216ce8a94 100644
> --- a/include/linux/iio/adc/ad_sigma_delta.h
> +++ b/include/linux/iio/adc/ad_sigma_delta.h
> @@ -79,7 +79,8 @@ 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				reg_data[4] ____cacheline_aligned;
> +	uint8_t				buf_data[16] ____cacheline_aligned;
The usual assumption is that a device is safe against itself, so we can't
get cacheline corruption in a single action from the device to itself.
This does make me wonder why we have two buffers like this at all though!

>  };
>  
>  static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH V2] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
  2019-04-09  7:58 [PATCH] " Alexandru Ardelean
@ 2019-04-16  8:19 ` Alexandru Ardelean
  2019-04-22  8:56   ` Jonathan Cameron
  0 siblings, 1 reply; 13+ messages in thread
From: Alexandru Ardelean @ 2019-04-16  8:19 UTC (permalink / raw)
  To: linux-iio; +Cc: Lars-Peter Clausen, Alexandru Ardelean

From: Lars-Peter Clausen <lars@metafoo.de>

Use a heap allocated memory for the SPI transfer buffer. Using stack memory
will not work on some architectures when using DMA.

This also avoids potential stack corruption, as it makes sure that the
buffer is in it's own cacheline and (obviously) not on the stack.

Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices")
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
Changelog v1 -> v2:
* added ____cacheline_aligned to `buf_data`
* extended comment about cacheline
* added fixes tag

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

diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index af6cbc683214..02d9049b9218 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -58,7 +58,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm);
 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
 	unsigned int size, unsigned int val)
 {
-	uint8_t *data = sigma_delta->data;
+	uint8_t *data = sigma_delta->reg_data;
 	struct spi_transfer t = {
 		.tx_buf		= data,
 		.len		= size + 1,
@@ -102,7 +102,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg);
 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
 	unsigned int reg, unsigned int size, uint8_t *val)
 {
-	uint8_t *data = sigma_delta->data;
+	uint8_t *data = sigma_delta->reg_data;
 	int ret;
 	struct spi_transfer t[] = {
 		{
@@ -148,24 +148,24 @@ int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
 {
 	int ret;
 
-	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
+	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->reg_data);
 	if (ret < 0)
 		goto out;
 
 	switch (size) {
 	case 4:
-		*val = get_unaligned_be32(sigma_delta->data);
+		*val = get_unaligned_be32(sigma_delta->reg_data);
 		break;
 	case 3:
-		*val = (sigma_delta->data[0] << 16) |
-			(sigma_delta->data[1] << 8) |
-			sigma_delta->data[2];
+		*val = (sigma_delta->reg_data[0] << 16) |
+			(sigma_delta->reg_data[1] << 8) |
+			sigma_delta->reg_data[2];
 		break;
 	case 2:
-		*val = get_unaligned_be16(sigma_delta->data);
+		*val = get_unaligned_be16(sigma_delta->reg_data);
 		break;
 	case 1:
-		*val = sigma_delta->data[0];
+		*val = sigma_delta->reg_data[0];
 		break;
 	default:
 		ret = -EINVAL;
@@ -403,12 +403,12 @@ 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->buf_data;
 	unsigned int reg_size;
 	unsigned int data_reg;
-	uint8_t data[16];
 	int ret;
 
-	memset(data, 0x00, 16);
+	memset(sigma_delta->buf_data, 0x00, sizeof(sigma_delta->buf_data));
 
 	reg_size = indio_dev->channels[0].scan_type.realbits +
 			indio_dev->channels[0].scan_type.shift;
diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
index 6e9fb1932dde..d95216ce8a94 100644
--- a/include/linux/iio/adc/ad_sigma_delta.h
+++ b/include/linux/iio/adc/ad_sigma_delta.h
@@ -79,7 +79,8 @@ 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				reg_data[4] ____cacheline_aligned;
+	uint8_t				buf_data[16] ____cacheline_aligned;
 };
 
 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-11-28 21:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12  9:10 [PATCH] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack Alexandru Ardelean
2020-11-12  9:54 ` Lars-Peter Clausen
2020-11-12 10:14   ` Alexandru Ardelean
2020-11-12 10:52     ` Lars-Peter Clausen
2020-11-13  9:40 ` [PATCH v2] " Alexandru Ardelean
2020-11-14 16:20   ` Jonathan Cameron
2020-11-19  8:30     ` Alexandru Ardelean
2020-11-21 16:41       ` Jonathan Cameron
2020-11-24 12:38   ` [PATCH v3] " Alexandru Ardelean
2020-11-28 13:34     ` Jonathan Cameron
  -- strict thread matches above, loose matches on Subject: below --
2019-04-09  7:58 [PATCH] " Alexandru Ardelean
2019-04-16  8:19 ` [PATCH V2] " Alexandru Ardelean
2019-04-22  8:56   ` Jonathan Cameron
2019-04-22 10:28     ` Ardelean, Alexandru

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.