linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] iio: adc: stm32-adc: fix sleep in atomic context
@ 2020-03-09 10:02 Olivier Moysan
  2020-03-09 10:39 ` Fabrice Gasnier
  0 siblings, 1 reply; 3+ messages in thread
From: Olivier Moysan @ 2020-03-09 10:02 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, alexandre.torgue, fabrice.gasnier,
	benjamin.gaignard, olivier.moysan
  Cc: linux-iio, linux-stm32, linux-arm-kernel, linux-kernel

This commit fixes the following error:
"BUG: sleeping function called from invalid context at kernel/irq/chip.c"

In DMA mode suppress the trigger irq handler, and make the buffer
transfers directly in DMA callback, instead.

Fixes: 2763ea0585c9 ("iio: adc: stm32: add optional dma support")

Signed-off-by: Olivier Moysan <olivier.moysan@st.com>
---
Changes in v2:
- Add "Fixes" tag in commit message

This solution has been already discussed in the thread
https://lkml.org/lkml/2019/3/30/171, and applied in STM32 DFSDM driver:
e19ac9d9a978 ("iio: adc: stm32-dfsdm: fix sleep in atomic context")
---
 drivers/iio/adc/stm32-adc.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index 80c3f963527b..ae622ee6d08c 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -1418,8 +1418,30 @@ static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
 static void stm32_adc_dma_buffer_done(void *data)
 {
 	struct iio_dev *indio_dev = data;
+	struct stm32_adc *adc = iio_priv(indio_dev);
+	int residue = stm32_adc_dma_residue(adc);
+
+	/*
+	 * In DMA mode the trigger services of IIO are not used
+	 * (e.g. no call to iio_trigger_poll).
+	 * Calling irq handler associated to the hardware trigger is not
+	 * relevant as the conversions have already been done. Data
+	 * transfers are performed directly in DMA callback instead.
+	 * This implementation avoids to call trigger irq handler that
+	 * may sleep, in an atomic context (DMA irq handler context).
+	 */
+	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
 
-	iio_trigger_poll_chained(indio_dev->trig);
+	while (residue >= indio_dev->scan_bytes) {
+		u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
+
+		iio_push_to_buffers(indio_dev, buffer);
+
+		residue -= indio_dev->scan_bytes;
+		adc->bufi += indio_dev->scan_bytes;
+		if (adc->bufi >= adc->rx_buf_sz)
+			adc->bufi = 0;
+	}
 }
 
 static int stm32_adc_dma_start(struct iio_dev *indio_dev)
@@ -1845,6 +1867,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
 {
 	struct iio_dev *indio_dev;
 	struct device *dev = &pdev->dev;
+	irqreturn_t (*handler)(int irq, void *p) = NULL;
 	struct stm32_adc *adc;
 	int ret;
 
@@ -1911,9 +1934,11 @@ static int stm32_adc_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
+	if (!adc->dma_chan)
+		handler = &stm32_adc_trigger_handler;
+
 	ret = iio_triggered_buffer_setup(indio_dev,
-					 &iio_pollfunc_store_time,
-					 &stm32_adc_trigger_handler,
+					 &iio_pollfunc_store_time, handler,
 					 &stm32_adc_buffer_setup_ops);
 	if (ret) {
 		dev_err(&pdev->dev, "buffer setup failed\n");
-- 
2.17.1


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

* Re: [PATCH v2] iio: adc: stm32-adc: fix sleep in atomic context
  2020-03-09 10:02 [PATCH v2] iio: adc: stm32-adc: fix sleep in atomic context Olivier Moysan
@ 2020-03-09 10:39 ` Fabrice Gasnier
  2020-03-15  9:35   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Fabrice Gasnier @ 2020-03-09 10:39 UTC (permalink / raw)
  To: Olivier Moysan, jic23, knaack.h, lars, pmeerw, alexandre.torgue,
	benjamin.gaignard
  Cc: linux-iio, linux-stm32, linux-arm-kernel, linux-kernel

On 3/9/20 11:02 AM, Olivier Moysan wrote:
> This commit fixes the following error:
> "BUG: sleeping function called from invalid context at kernel/irq/chip.c"
> 
> In DMA mode suppress the trigger irq handler, and make the buffer
> transfers directly in DMA callback, instead.
> 
> Fixes: 2763ea0585c9 ("iio: adc: stm32: add optional dma support")
> 
> Signed-off-by: Olivier Moysan <olivier.moysan@st.com>

Hi Olivier,

Acked-by: Fabrice Gasnier <fabrice.gasnier@st.com>

Thanks,
Fabrice

> ---
> Changes in v2:
> - Add "Fixes" tag in commit message
> 
> This solution has been already discussed in the thread
> https://lkml.org/lkml/2019/3/30/171, and applied in STM32 DFSDM driver:
> e19ac9d9a978 ("iio: adc: stm32-dfsdm: fix sleep in atomic context")
> ---
>  drivers/iio/adc/stm32-adc.c | 31 ++++++++++++++++++++++++++++---
>  1 file changed, 28 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> index 80c3f963527b..ae622ee6d08c 100644
> --- a/drivers/iio/adc/stm32-adc.c
> +++ b/drivers/iio/adc/stm32-adc.c
> @@ -1418,8 +1418,30 @@ static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
>  static void stm32_adc_dma_buffer_done(void *data)
>  {
>  	struct iio_dev *indio_dev = data;
> +	struct stm32_adc *adc = iio_priv(indio_dev);
> +	int residue = stm32_adc_dma_residue(adc);
> +
> +	/*
> +	 * In DMA mode the trigger services of IIO are not used
> +	 * (e.g. no call to iio_trigger_poll).
> +	 * Calling irq handler associated to the hardware trigger is not
> +	 * relevant as the conversions have already been done. Data
> +	 * transfers are performed directly in DMA callback instead.
> +	 * This implementation avoids to call trigger irq handler that
> +	 * may sleep, in an atomic context (DMA irq handler context).
> +	 */
> +	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
>  
> -	iio_trigger_poll_chained(indio_dev->trig);
> +	while (residue >= indio_dev->scan_bytes) {
> +		u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
> +
> +		iio_push_to_buffers(indio_dev, buffer);
> +
> +		residue -= indio_dev->scan_bytes;
> +		adc->bufi += indio_dev->scan_bytes;
> +		if (adc->bufi >= adc->rx_buf_sz)
> +			adc->bufi = 0;
> +	}
>  }
>  
>  static int stm32_adc_dma_start(struct iio_dev *indio_dev)
> @@ -1845,6 +1867,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
>  {
>  	struct iio_dev *indio_dev;
>  	struct device *dev = &pdev->dev;
> +	irqreturn_t (*handler)(int irq, void *p) = NULL;
>  	struct stm32_adc *adc;
>  	int ret;
>  
> @@ -1911,9 +1934,11 @@ static int stm32_adc_probe(struct platform_device *pdev)
>  	if (ret < 0)
>  		return ret;
>  
> +	if (!adc->dma_chan)
> +		handler = &stm32_adc_trigger_handler;
> +
>  	ret = iio_triggered_buffer_setup(indio_dev,
> -					 &iio_pollfunc_store_time,
> -					 &stm32_adc_trigger_handler,
> +					 &iio_pollfunc_store_time, handler,
>  					 &stm32_adc_buffer_setup_ops);
>  	if (ret) {
>  		dev_err(&pdev->dev, "buffer setup failed\n");
> 

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

* Re: [PATCH v2] iio: adc: stm32-adc: fix sleep in atomic context
  2020-03-09 10:39 ` Fabrice Gasnier
@ 2020-03-15  9:35   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2020-03-15  9:35 UTC (permalink / raw)
  To: Fabrice Gasnier
  Cc: Olivier Moysan, knaack.h, lars, pmeerw, alexandre.torgue,
	benjamin.gaignard, linux-iio, linux-stm32, linux-arm-kernel,
	linux-kernel

On Mon, 9 Mar 2020 11:39:17 +0100
Fabrice Gasnier <fabrice.gasnier@st.com> wrote:

> On 3/9/20 11:02 AM, Olivier Moysan wrote:
> > This commit fixes the following error:
> > "BUG: sleeping function called from invalid context at kernel/irq/chip.c"
> > 
> > In DMA mode suppress the trigger irq handler, and make the buffer
> > transfers directly in DMA callback, instead.
> > 
> > Fixes: 2763ea0585c9 ("iio: adc: stm32: add optional dma support")
> > 
> > Signed-off-by: Olivier Moysan <olivier.moysan@st.com>  
> 
> Hi Olivier,
> 
> Acked-by: Fabrice Gasnier <fabrice.gasnier@st.com>
Thanks.  Queued up locally but not pushed out just yet as I have a pull
request out to Greg.  Also marked for stable.

thanks,

Jonathan

> 
> Thanks,
> Fabrice
> 
> > ---
> > Changes in v2:
> > - Add "Fixes" tag in commit message
> > 
> > This solution has been already discussed in the thread
> > https://lkml.org/lkml/2019/3/30/171, and applied in STM32 DFSDM driver:
> > e19ac9d9a978 ("iio: adc: stm32-dfsdm: fix sleep in atomic context")
> > ---
> >  drivers/iio/adc/stm32-adc.c | 31 ++++++++++++++++++++++++++++---
> >  1 file changed, 28 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> > index 80c3f963527b..ae622ee6d08c 100644
> > --- a/drivers/iio/adc/stm32-adc.c
> > +++ b/drivers/iio/adc/stm32-adc.c
> > @@ -1418,8 +1418,30 @@ static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
> >  static void stm32_adc_dma_buffer_done(void *data)
> >  {
> >  	struct iio_dev *indio_dev = data;
> > +	struct stm32_adc *adc = iio_priv(indio_dev);
> > +	int residue = stm32_adc_dma_residue(adc);
> > +
> > +	/*
> > +	 * In DMA mode the trigger services of IIO are not used
> > +	 * (e.g. no call to iio_trigger_poll).
> > +	 * Calling irq handler associated to the hardware trigger is not
> > +	 * relevant as the conversions have already been done. Data
> > +	 * transfers are performed directly in DMA callback instead.
> > +	 * This implementation avoids to call trigger irq handler that
> > +	 * may sleep, in an atomic context (DMA irq handler context).
> > +	 */
> > +	dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
> >  
> > -	iio_trigger_poll_chained(indio_dev->trig);
> > +	while (residue >= indio_dev->scan_bytes) {
> > +		u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
> > +
> > +		iio_push_to_buffers(indio_dev, buffer);
> > +
> > +		residue -= indio_dev->scan_bytes;
> > +		adc->bufi += indio_dev->scan_bytes;
> > +		if (adc->bufi >= adc->rx_buf_sz)
> > +			adc->bufi = 0;
> > +	}
> >  }
> >  
> >  static int stm32_adc_dma_start(struct iio_dev *indio_dev)
> > @@ -1845,6 +1867,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
> >  {
> >  	struct iio_dev *indio_dev;
> >  	struct device *dev = &pdev->dev;
> > +	irqreturn_t (*handler)(int irq, void *p) = NULL;
> >  	struct stm32_adc *adc;
> >  	int ret;
> >  
> > @@ -1911,9 +1934,11 @@ static int stm32_adc_probe(struct platform_device *pdev)
> >  	if (ret < 0)
> >  		return ret;
> >  
> > +	if (!adc->dma_chan)
> > +		handler = &stm32_adc_trigger_handler;
> > +
> >  	ret = iio_triggered_buffer_setup(indio_dev,
> > -					 &iio_pollfunc_store_time,
> > -					 &stm32_adc_trigger_handler,
> > +					 &iio_pollfunc_store_time, handler,
> >  					 &stm32_adc_buffer_setup_ops);
> >  	if (ret) {
> >  		dev_err(&pdev->dev, "buffer setup failed\n");
> >   


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

end of thread, other threads:[~2020-03-15  9:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09 10:02 [PATCH v2] iio: adc: stm32-adc: fix sleep in atomic context Olivier Moysan
2020-03-09 10:39 ` Fabrice Gasnier
2020-03-15  9:35   ` Jonathan Cameron

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).