All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Alexandru Ardelean <alexandru.ardelean@analog.com>
Cc: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<lars@metafoo.de>, <alexandru.tachici@analog.com>
Subject: Re: [PATCH v3] iio: ad_sigma_delta: Don't put SPI transfer buffer on the stack
Date: Sat, 28 Nov 2020 13:34:08 +0000	[thread overview]
Message-ID: <20201128133408.3218c1fd@archlinux> (raw)
In-Reply-To: <20201124123807.19717-1-alexandru.ardelean@analog.com>

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,


      reply	other threads:[~2020-11-28 21:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201128133408.3218c1fd@archlinux \
    --to=jic23@kernel.org \
    --cc=alexandru.ardelean@analog.com \
    --cc=alexandru.tachici@analog.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.