LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Miquel Raynal <miquel.raynal@bootlin.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	Nuno Sa <Nuno.Sa@analog.com>
Subject: Re: [PATCH v2 04/16] iio: adc: max1027: Avoid device managed allocators for DMA purposes
Date: Sun, 5 Sep 2021 16:26:12 +0100	[thread overview]
Message-ID: <20210905162612.571fc03a@jic23-huawei> (raw)
In-Reply-To: <20210902211437.503623-5-miquel.raynal@bootlin.com>

On Thu,  2 Sep 2021 23:14:25 +0200
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> My overall understanding [1] is that devm_ allocations will require an
> extra 16 bytes at the beginning of the allocated buffer to store
> information about the managing device, this shifts a little bit the
> buffer which may then not be fully aligned on cache lines, disqualifying
> them for being suitable for DMA.
> 
> Let's switch to a regular kmalloc based allocator to ensure st->buffer
> is DMA-able, as required by the IIO subsystem.
> 
> [1] https://linux-arm-kernel.infradead.narkive.com/vyJqy0RQ/question-devm-kmalloc-for-dma
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
As per the other thread (I was late to the discussion) I don't believe this is
necessary because there is no chance of the data in the cacheline being touched
by the CPU whilst DMA is going on.

https://linux-arm-kernel.infradead.narkive.com/vyJqy0RQ/question-devm-kmalloc-for-dma#post5

Writes before dma transfer are safe.  It's writes during it that can cause problem so as
long as a driver isn't doing devm_ calls during such writes (and I really hope this isn't)
then we are safe.  The device will only overwrite the data with what was already there.

Jonathan

> ---
>  drivers/iio/adc/max1027.c | 21 ++++++++++++---------
>  1 file changed, 12 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> index f27044324141..1167d50c1d67 100644
> --- a/drivers/iio/adc/max1027.c
> +++ b/drivers/iio/adc/max1027.c
> @@ -438,9 +438,7 @@ static int max1027_probe(struct spi_device *spi)
>  	indio_dev->num_channels = st->info->num_channels;
>  	indio_dev->available_scan_masks = st->info->available_scan_masks;
>  
> -	st->buffer = devm_kmalloc_array(&indio_dev->dev,
> -					indio_dev->num_channels, 2,
> -					GFP_KERNEL);
> +	st->buffer = kmalloc_array(indio_dev->num_channels, 2, GFP_KERNEL);
>  	if (!st->buffer)
>  		return -ENOMEM;
>  
> @@ -451,7 +449,7 @@ static int max1027_probe(struct spi_device *spi)
>  						      NULL);
>  		if (ret < 0) {
>  			dev_err(&indio_dev->dev, "Failed to setup buffer\n");
> -			return ret;
> +			goto free_buffer;
>  		}
>  
>  		st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger",
> @@ -460,7 +458,7 @@ static int max1027_probe(struct spi_device *spi)
>  			ret = -ENOMEM;
>  			dev_err(&indio_dev->dev,
>  				"Failed to allocate iio trigger\n");
> -			return ret;
> +			goto free_buffer;
>  		}
>  
>  		st->trig->ops = &max1027_trigger_ops;
> @@ -470,7 +468,7 @@ static int max1027_probe(struct spi_device *spi)
>  		if (ret < 0) {
>  			dev_err(&indio_dev->dev,
>  				"Failed to register iio trigger\n");
> -			return ret;
> +			goto free_buffer;
>  		}
>  
>  		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
> @@ -481,7 +479,7 @@ static int max1027_probe(struct spi_device *spi)
>  						st->trig);
>  		if (ret < 0) {
>  			dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
> -			return ret;
> +			goto free_buffer;
>  		}
>  	}
>  
> @@ -490,7 +488,7 @@ static int max1027_probe(struct spi_device *spi)
>  	ret = spi_write(st->spi, &st->reg, 1);
>  	if (ret < 0) {
>  		dev_err(&indio_dev->dev, "Failed to reset the ADC\n");
> -		return ret;
> +		goto free_buffer;
>  	}
>  
>  	/* Disable averaging */
> @@ -498,10 +496,15 @@ static int max1027_probe(struct spi_device *spi)
>  	ret = spi_write(st->spi, &st->reg, 1);
>  	if (ret < 0) {
>  		dev_err(&indio_dev->dev, "Failed to configure averaging register\n");
> -		return ret;
> +		goto free_buffer;
>  	}
>  
>  	return devm_iio_device_register(&spi->dev, indio_dev);
> +
> +free_buffer:
> +	kfree(st->buffer);
> +
> +	return ret;
>  }
>  
>  static struct spi_driver max1027_driver = {


  reply	other threads:[~2021-09-05 15:22 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02 21:14 [PATCH v2 00/16] Bring external triggers support to MAX1027-like ADCs Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 01/16] iio: adc: max1027: Fix style Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 02/16] iio: adc: max1027: Drop extra warning message Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 03/16] iio: adc: max1027: Drop useless debug messages Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 04/16] iio: adc: max1027: Avoid device managed allocators for DMA purposes Miquel Raynal
2021-09-05 15:26   ` Jonathan Cameron [this message]
2021-09-02 21:14 ` [PATCH v2 05/16] iio: adc: max1027: Minimize the number of converted channels Miquel Raynal
2021-09-05 15:34   ` Jonathan Cameron
2021-09-15  9:46     ` Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 06/16] iio: adc: max1027: Rename a helper Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 07/16] iio: adc: max1027: Create a helper to enable/disable the cnvst trigger Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 08/16] iio: adc: max1027: Simplify the _set_trigger_state() helper Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 09/16] iio: adc: max1027: Ensure a default cnvst trigger configuration Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 10/16] iio: adc: max1027: Create a helper to configure the channels to scan Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 11/16] iio: adc: max1027: Prevent single channel accesses during buffer reads Miquel Raynal
2021-09-05 15:38   ` Jonathan Cameron
2021-09-02 21:14 ` [PATCH v2 12/16] iio: adc: max1027: Separate the IRQ handler from the read logic Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 13/16] iio: adc: max1027: Introduce an end of conversion helper Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 14/16] iio: adc: max1027: Don't just sleep when the EOC interrupt is available Miquel Raynal
2021-09-06  9:38   ` Sa, Nuno
2021-09-15  9:55     ` Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 15/16] iio: adc: max1027: Add support for external triggers Miquel Raynal
2021-09-05 16:10   ` Jonathan Cameron
2021-09-06  9:53     ` Sa, Nuno
2021-09-15 15:45       ` Miquel Raynal
2021-09-18 17:39         ` Jonathan Cameron
2021-09-20 10:47           ` Miquel Raynal
2021-09-15 10:18     ` Miquel Raynal
2021-09-18 17:13       ` Jonathan Cameron
2021-09-20  8:37         ` Miquel Raynal
2021-09-20 17:43           ` Jonathan Cameron
2021-09-21  8:11             ` Miquel Raynal
2021-09-02 21:14 ` [PATCH v2 16/16] iio: adc: max1027: Don't reject external triggers when there is no IRQ Miquel Raynal

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=20210905162612.571fc03a@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Nuno.Sa@analog.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=thomas.petazzoni@bootlin.com \
    --subject='Re: [PATCH v2 04/16] iio: adc: max1027: Avoid device managed allocators for DMA purposes' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).