diff mbox series

[v14,12/12] dmaengine: imx-sdma: add terminated list for freed descriptor in worker

Message ID 1617809456-17693-13-git-send-email-yibin.gong@nxp.com (mailing list archive)
State New, archived
Headers show
Series add ecspi ERR009165 for i.mx6/7 soc family | expand

Commit Message

Robin Gong April 7, 2021, 3:30 p.m. UTC
Add terminated list for keeping descriptor so that it could be freed in
worker without any potential involving next descriptor raised up before
this descriptor freed, because vchan_get_all_descriptors get all
descriptors including the last terminated descriptor and the next
descriptor, hence, the next descriptor maybe freed unexpectly when it's
done in worker without this patch.
https://www.spinics.net/lists/dmaengine/msg23367.html

Signed-off-by: Robin Gong <yibin.gong@nxp.com>
Reported-by: Richard Leitner <richard.leitner@skidata.com>
---
 drivers/dma/imx-sdma.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

Comments

Vinod Koul April 12, 2021, 9:39 a.m. UTC | #1
On 07-04-21, 23:30, Robin Gong wrote:
> Add terminated list for keeping descriptor so that it could be freed in
> worker without any potential involving next descriptor raised up before
> this descriptor freed, because vchan_get_all_descriptors get all
> descriptors including the last terminated descriptor and the next
> descriptor, hence, the next descriptor maybe freed unexpectly when it's
> done in worker without this patch.
> https://www.spinics.net/lists/dmaengine/msg23367.html

Sound like you should implement .device_synchronize() and do the actual
work there..?

> 
> Signed-off-by: Robin Gong <yibin.gong@nxp.com>
> Reported-by: Richard Leitner <richard.leitner@skidata.com>
> ---
>  drivers/dma/imx-sdma.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c
> index 9519b41..4174580 100644
> --- a/drivers/dma/imx-sdma.c
> +++ b/drivers/dma/imx-sdma.c
> @@ -381,6 +381,7 @@ struct sdma_channel {
>  	enum dma_status			status;
>  	struct imx_dma_data		data;
>  	struct work_struct		terminate_worker;
> +	struct list_head                terminated;
>  	bool				is_ram_script;
>  };
>  
> @@ -1041,9 +1042,6 @@ static void sdma_channel_terminate_work(struct work_struct *work)
>  {
>  	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
>  						  terminate_worker);
> -	unsigned long flags;
> -	LIST_HEAD(head);
> -
>  	/*
>  	 * According to NXP R&D team a delay of one BD SDMA cost time
>  	 * (maximum is 1ms) should be added after disable of the channel
> @@ -1052,10 +1050,7 @@ static void sdma_channel_terminate_work(struct work_struct *work)
>  	 */
>  	usleep_range(1000, 2000);
>  
> -	spin_lock_irqsave(&sdmac->vc.lock, flags);
> -	vchan_get_all_descriptors(&sdmac->vc, &head);
> -	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
> -	vchan_dma_desc_free_list(&sdmac->vc, &head);
> +	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
>  }
>  
>  static int sdma_terminate_all(struct dma_chan *chan)
> @@ -1069,6 +1064,13 @@ static int sdma_terminate_all(struct dma_chan *chan)
>  
>  	if (sdmac->desc) {
>  		vchan_terminate_vdesc(&sdmac->desc->vd);
> +		/*
> +		 * move out current descriptor into terminated list so that
> +		 * it could be free in sdma_channel_terminate_work alone
> +		 * later without potential involving next descriptor raised
> +		 * up before the last descriptor terminated.
> +		 */
> +		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
>  		sdmac->desc = NULL;
>  		schedule_work(&sdmac->terminate_worker);
>  	}
> @@ -2075,6 +2077,7 @@ static int sdma_probe(struct platform_device *pdev)
>  
>  		sdmac->channel = i;
>  		sdmac->vc.desc_free = sdma_desc_free;
> +		INIT_LIST_HEAD(&sdmac->terminated);
>  		INIT_WORK(&sdmac->terminate_worker,
>  				sdma_channel_terminate_work);
>  		/*
> -- 
> 2.7.4
Robin Gong April 13, 2021, 5:05 a.m. UTC | #2
On 2021/04/12 17:39,  Vinod Koul <vkoul@kernel.org>  wrote: 
> On 07-04-21, 23:30, Robin Gong wrote:
> > Add terminated list for keeping descriptor so that it could be freed
> > in worker without any potential involving next descriptor raised up
> > before this descriptor freed, because vchan_get_all_descriptors get
> > all descriptors including the last terminated descriptor and the next
> > descriptor, hence, the next descriptor maybe freed unexpectly when
> > it's done in worker without this patch.
> > https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.
> >
> spinics.net%2Flists%2Fdmaengine%2Fmsg23367.html&amp;data=04%7C01%
> 7Cyib
> >
> in.gong%40nxp.com%7Cf255f329c8de459ffbaf08d8fd96d6c5%7C686ea1d3bc
> 2b4c6
> >
> fa92cd99c5c301635%7C0%7C0%7C637538171591949442%7CUnknown%7CT
> WFpbGZsb3d
> >
> 8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3
> D%7C
> >
> 1000&amp;sdata=3YFgzHFDNwRnogvxZpNcwVKOKpk4GHrgScdrbuMKjwE%3D
> &amp;rese
> > rved=0
> 
> Sound like you should implement .device_synchronize() and do the actual
> work there..?
Yes,  I believe no issue here if call dmaengine_terminate_sync() always since
flush_work(&sdmac->terminate_worker) has already been in  .device_synchronize() of
sdma driver. But unfortunately, have to use dmaengine_terminate_all() instead in some
non-atomic case like ALSA.
Vinod Koul June 15, 2021, 6:06 a.m. UTC | #3
On 07-04-21, 23:30, Robin Gong wrote:
> Add terminated list for keeping descriptor so that it could be freed in
> worker without any potential involving next descriptor raised up before
> this descriptor freed, because vchan_get_all_descriptors get all
> descriptors including the last terminated descriptor and the next
> descriptor, hence, the next descriptor maybe freed unexpectly when it's
> done in worker without this patch.
> https://www.spinics.net/lists/dmaengine/msg23367.html

Acked-By: Vinod Koul <vkoul@kernel.org>
Lucas Stach July 9, 2021, 9:55 a.m. UTC | #4
Am Mittwoch, dem 07.04.2021 um 23:30 +0800 schrieb Robin Gong:
> Add terminated list for keeping descriptor so that it could be freed in
> worker without any potential involving next descriptor raised up before
> this descriptor freed, because vchan_get_all_descriptors get all
> descriptors including the last terminated descriptor and the next
> descriptor, hence, the next descriptor maybe freed unexpectly when it's
> done in worker without this patch.
> https://www.spinics.net/lists/dmaengine/msg23367.html
> 
> Signed-off-by: Robin Gong <yibin.gong@nxp.com>
> Reported-by: Richard Leitner <richard.leitner@skidata.com>

Reviewed-by: Lucas Stach <l.stach@pengutronix.de>

> ---
>  drivers/dma/imx-sdma.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c
> index 9519b41..4174580 100644
> --- a/drivers/dma/imx-sdma.c
> +++ b/drivers/dma/imx-sdma.c
> @@ -381,6 +381,7 @@ struct sdma_channel {
>  	enum dma_status			status;
>  	struct imx_dma_data		data;
>  	struct work_struct		terminate_worker;
> +	struct list_head                terminated;
>  	bool				is_ram_script;
>  };
>  
> @@ -1041,9 +1042,6 @@ static void sdma_channel_terminate_work(struct work_struct *work)
>  {
>  	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
>  						  terminate_worker);
> -	unsigned long flags;
> -	LIST_HEAD(head);
> -
>  	/*
>  	 * According to NXP R&D team a delay of one BD SDMA cost time
>  	 * (maximum is 1ms) should be added after disable of the channel
> @@ -1052,10 +1050,7 @@ static void sdma_channel_terminate_work(struct work_struct *work)
>  	 */
>  	usleep_range(1000, 2000);
>  
> -	spin_lock_irqsave(&sdmac->vc.lock, flags);
> -	vchan_get_all_descriptors(&sdmac->vc, &head);
> -	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
> -	vchan_dma_desc_free_list(&sdmac->vc, &head);
> +	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
>  }
>  
>  static int sdma_terminate_all(struct dma_chan *chan)
> @@ -1069,6 +1064,13 @@ static int sdma_terminate_all(struct dma_chan *chan)
>  
>  	if (sdmac->desc) {
>  		vchan_terminate_vdesc(&sdmac->desc->vd);
> +		/*
> +		 * move out current descriptor into terminated list so that
> +		 * it could be free in sdma_channel_terminate_work alone
> +		 * later without potential involving next descriptor raised
> +		 * up before the last descriptor terminated.
> +		 */
> +		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
>  		sdmac->desc = NULL;
>  		schedule_work(&sdmac->terminate_worker);
>  	}
> @@ -2075,6 +2077,7 @@ static int sdma_probe(struct platform_device *pdev)
>  
>  		sdmac->channel = i;
>  		sdmac->vc.desc_free = sdma_desc_free;
> +		INIT_LIST_HEAD(&sdmac->terminated);
>  		INIT_WORK(&sdmac->terminate_worker,
>  				sdma_channel_terminate_work);
>  		/*
diff mbox series

Patch

diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c
index 9519b41..4174580 100644
--- a/drivers/dma/imx-sdma.c
+++ b/drivers/dma/imx-sdma.c
@@ -381,6 +381,7 @@  struct sdma_channel {
 	enum dma_status			status;
 	struct imx_dma_data		data;
 	struct work_struct		terminate_worker;
+	struct list_head                terminated;
 	bool				is_ram_script;
 };
 
@@ -1041,9 +1042,6 @@  static void sdma_channel_terminate_work(struct work_struct *work)
 {
 	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
 						  terminate_worker);
-	unsigned long flags;
-	LIST_HEAD(head);
-
 	/*
 	 * According to NXP R&D team a delay of one BD SDMA cost time
 	 * (maximum is 1ms) should be added after disable of the channel
@@ -1052,10 +1050,7 @@  static void sdma_channel_terminate_work(struct work_struct *work)
 	 */
 	usleep_range(1000, 2000);
 
-	spin_lock_irqsave(&sdmac->vc.lock, flags);
-	vchan_get_all_descriptors(&sdmac->vc, &head);
-	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
-	vchan_dma_desc_free_list(&sdmac->vc, &head);
+	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
 }
 
 static int sdma_terminate_all(struct dma_chan *chan)
@@ -1069,6 +1064,13 @@  static int sdma_terminate_all(struct dma_chan *chan)
 
 	if (sdmac->desc) {
 		vchan_terminate_vdesc(&sdmac->desc->vd);
+		/*
+		 * move out current descriptor into terminated list so that
+		 * it could be free in sdma_channel_terminate_work alone
+		 * later without potential involving next descriptor raised
+		 * up before the last descriptor terminated.
+		 */
+		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
 		sdmac->desc = NULL;
 		schedule_work(&sdmac->terminate_worker);
 	}
@@ -2075,6 +2077,7 @@  static int sdma_probe(struct platform_device *pdev)
 
 		sdmac->channel = i;
 		sdmac->vc.desc_free = sdma_desc_free;
+		INIT_LIST_HEAD(&sdmac->terminated);
 		INIT_WORK(&sdmac->terminate_worker,
 				sdma_channel_terminate_work);
 		/*