Skip to contentSkip to conversation

dmaengine: xilinx_dma: Add missing check for empty list

PATCH Applied Sebastian von Ohr 3 participants 11 messages Mar 3, 2020 – Apr 15, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: xilinx_dma: Add missing check for empty list

The DMA transfer might finish just after checking the state with dma_cookie_status, but before the lock is acquired. Not checking for an empty list in xilinx_dma_tx_status may result in reading random data or data corruption when desc is written to. This can be reliably triggered by using dma_sync_wait to wait for DMA completion.

---
 drivers/dma/xilinx/xilinx_dma.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/dma/xilinx/xilinx_dma.c +10 -10
index a9c5d5cc9f2b..5d5f1d0ce16c 100644
@@ -1229,16 +1229,16 @@ static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
 		return ret;
 
 	spin_lock_irqsave(&chan->lock, flags);
-
-	desc = list_last_entry(&chan->active_list,
-			       struct xilinx_dma_tx_descriptor, node);
-	/*
-	 * VDMA and simple mode do not support residue reporting, so the
-	 * residue field will always be 0.
-	 */
-	if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
-		residue = xilinx_dma_get_residue(chan, desc);
-
+	if (!list_empty(&chan->active_list)) {
+		desc = list_last_entry(&chan->active_list,
+				       struct xilinx_dma_tx_descriptor, node);
+		/*
+		 * VDMA and simple mode do not support residue reporting, so the
+		 * residue field will always be 0.
+		 */
+		if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
+			residue = xilinx_dma_get_residue(chan, desc);
+	}
 	spin_unlock_irqrestore(&chan->lock, flags);
 
 	dma_set_residue(txstate, residue);
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 6 lines · Sebastian von Ohr

The DMA transfer might finish just after checking the state with dma_cookie_status, but before the lock is acquired. Not checking for an empty list in xilinx_dma_tx_status may result in reading random data or data corruption when desc is written to. This can be reliably triggered by using dma_sync_wait to wait for DMA completion.

Appana, Radhey can you please test this..?

Quoted 39 lines
---
 drivers/dma/xilinx/xilinx_dma.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/dma/xilinx/xilinx_dma.c +10 -10
index a9c5d5cc9f2b..5d5f1d0ce16c 100644
@@ -1229,16 +1229,16 @@ static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
 		return ret;
 
 	spin_lock_irqsave(&chan->lock, flags);
-
-	desc = list_last_entry(&chan->active_list,
-			       struct xilinx_dma_tx_descriptor, node);
-	/*
-	 * VDMA and simple mode do not support residue reporting, so the
-	 * residue field will always be 0.
-	 */
-	if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
-		residue = xilinx_dma_get_residue(chan, desc);
-
+	if (!list_empty(&chan->active_list)) {
+		desc = list_last_entry(&chan->active_list,
+				       struct xilinx_dma_tx_descriptor, node);
+		/*
+		 * VDMA and simple mode do not support residue reporting, so the
+		 * residue field will always be 0.
+		 */
+		if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
+			residue = xilinx_dma_get_residue(chan, desc);
+	}
 	spin_unlock_irqrestore(&chan->lock, flags);
 
 	dma_set_residue(txstate, residue);
Signature · 1 line
2.17.1
Signature · 1 line
~Vinod
#2 lore raw
Quoted 8 lines

-----Original Message-----
From: Vinod Koul <[email protected]>
Sent: Friday, March 6, 2020 7:04 PM
To: Sebastian von Ohr <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Radhey Shyam Pandey <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Minor nit - Better to also add <...> "in device_tx_status callback "

Quoted 9 lines · Sebastian von Ohr
Quoted 5 lines

The DMA transfer might finish just after checking the state with dma_cookie_status, but before the lock is acquired. Not checking for an empty list in xilinx_dma_tx_status may result in reading random data or data corruption when desc is written to. This can be reliably triggered by using dma_sync_wait to wait for DMA completion.

Appana, Radhey can you please test this..?

Sure, we will test it. Changes look fine. Though had a question in mind, for a generic fix to this problem, should we make locking mandatory for all cookie helper functions? Or is there any limitation?

The framework say for dma_cookie_status says locking is not required. This scenario is a race condition when the driver calls dma_cookie_status and it sees it's not completed, but then since there is no locking and dma completion comes and it changes cookie state and removes the element from active list to done list. When driver access it in tx_status it results in data corruption/crash.

Quoted 48 lines

drivers/dma/xilinx/xilinx_dma.c static enum dma_status

Quoted 13 lines
---
 drivers/dma/xilinx/xilinx_dma.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/dma/xilinx/xilinx_dma.c index a9c5d5cc9f2b..5d5f1d0ce16c 100644

drivers/dma/xilinx/xilinx_dma.c +0 -0
@@ -1229,16 +1229,16 @@ static enum dma_status

xilinx_dma_tx_status(struct dma_chan *dchan,

drivers/dma/xilinx/xilinx_dma.c static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,

Quoted 11 lines
drivers/dma/xilinx/xilinx_dma.c +0 -8
 		return ret;
 
 	spin_lock_irqsave(&chan->lock, flags);
-
-	desc = list_last_entry(&chan->active_list,
-			       struct xilinx_dma_tx_descriptor, node);
-	/*
-	 * VDMA and simple mode do not support residue reporting, so the
-	 * residue field will always be 0.
-	 */
-	if (chan->has_sg && chan->xdev->dma_config->dmatype !=

XDMA_TYPE_VDMA)

drivers/dma/xilinx/xilinx_dma.c static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,

Quoted 7 lines
drivers/dma/xilinx/xilinx_dma.c +5 -2
-		residue = xilinx_dma_get_residue(chan, desc);
-
+	if (!list_empty(&chan->active_list)) {
+		desc = list_last_entry(&chan->active_list,
+				       struct xilinx_dma_tx_descriptor, node);
+		/*
+		 * VDMA and simple mode do not support residue reporting,

so the

drivers/dma/xilinx/xilinx_dma.c static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,

Quoted 3 lines
drivers/dma/xilinx/xilinx_dma.c +3 -0
+		 * residue field will always be 0.
+		 */
+		if (chan->has_sg && chan->xdev->dma_config->dmatype !=

XDMA_TYPE_VDMA)

Quoted 7 lines
drivers/dma/xilinx/xilinx_dma.c +2 -1
+			residue = xilinx_dma_get_residue(chan, desc);
+	}
 	spin_unlock_irqrestore(&chan->lock, flags);
 
 	dma_set_residue(txstate, residue);
--

2.17.1

--
~Vinod

maintainer
#3 lore raw
Quoted 24 lines · Radhey Shyam Pandey
Quoted 8 lines

-----Original Message-----
From: Vinod Koul <[email protected]>
Sent: Friday, March 6, 2020 7:04 PM
To: Sebastian von Ohr <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Radhey Shyam Pandey <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Minor nit - Better to also add <...> "in device_tx_status callback "

Quoted 9 lines · Sebastian von Ohr
Quoted 5 lines

The DMA transfer might finish just after checking the state with dma_cookie_status, but before the lock is acquired. Not checking for an empty list in xilinx_dma_tx_status may result in reading random data or data corruption when desc is written to. This can be reliably triggered by using dma_sync_wait to wait for DMA completion.

Appana, Radhey can you please test this..?

Sure, we will test it. Changes look fine. Though had a question in mind, for a generic fix to this problem, should we make locking mandatory for all cookie helper functions? Or is there any limitation?

Quoted 6 lines

The framework say for dma_cookie_status says locking is not required. This scenario is a race condition when the driver calls dma_cookie_status and it sees it's not completed, but then since there is no locking and dma completion comes and it changes cookie state and removes the element from active list to done list. When driver access it in tx_status it results in data corruption/crash.

The expectation is that you would lock while looking at list and then return.. So you should not have issues..

Signature · 1 line
~Vinod
#4 lore raw
In reply to Radhey Shyam Pandey
Quoted 10 lines

-----Original Message-----
From: Radhey Shyam Pandey [mailto:[email protected]]
Sent: Friday, March 6, 2020 2:57 PM
To: Vinod Koul <[email protected]>; Sebastian von Ohr <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Quoted 3 lines

Sure, we will test it. Changes look fine. Though had a question in mind, for a generic fix to this problem, should we make locking mandatory for all cookie helper functions? Or is there any limitation?

Any progress on the testing? If you need help reproducing the issue please let me know.

#5 lore raw
Quoted 20 lines

-----Original Message-----
From: Sebastian von Ohr <[email protected]>
Sent: Tuesday, April 7, 2020 1:22 PM
To: Radhey Shyam Pandey <[email protected]>; Vinod Koul <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Quoted 10 lines

-----Original Message-----
From: Radhey Shyam Pandey [mailto:[email protected]]
Sent: Friday, March 6, 2020 2:57 PM
To: Vinod Koul <[email protected]>; Sebastian von Ohr <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Quoted 6 lines
Quoted 3 lines

Sure, we will test it. Changes look fine. Though had a question in mind, for a generic fix to this problem, should we make locking mandatory for all cookie helper functions? Or is there any limitation?

Any progress on the testing? If you need help reproducing the issue please let me know.

Thanks for reminding me. Somehow I missed it. You mentioned in one of earlier thread that this bug is introduced it using dma_sync_wait to wait for DMA completion. So to reproduce the issue in xilinx axidma test client I have to replace issue_pending with sync_wait API?

#6 lore raw
Quoted 10 lines

-----Original Message-----
From: Radhey Shyam Pandey [mailto:[email protected]]
Sent: Tuesday, April 7, 2020 6:04 PM
To: Sebastian von Ohr <[email protected]>; Vinod Koul <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Quoted 4 lines

Thanks for reminding me. Somehow I missed it. You mentioned in one of earlier thread that this bug is introduced it using dma_sync_wait to wait for DMA completion. So to reproduce the issue in xilinx axidma test client I have to replace issue_pending with sync_wait API?

Yes, dma_sync_wait triggered the bug for me almost every transfer. In the xilinx axidmatest this is probably best achieved by adding dma_sync_wait before the wait_for_completion_timeout. I encountered the bug with your xilinx-v2019.2.01 tag. On this tag it actually crashes the kernel with an invalid memory access (because the residue is written to desc). With the current driver version it probably seems to work fine. You might have to add some debug print to verify that the active_list can indeed be empty in xilinx_dma_tx_status.

#7 lore raw
Quoted 26 lines

-----Original Message-----
From: Sebastian von Ohr <[email protected]>
Sent: Wednesday, April 8, 2020 12:42 PM
To: Radhey Shyam Pandey <[email protected]>; Vinod Koul <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

Quoted 8 lines

-----Original Message-----
From: Radhey Shyam Pandey [mailto:[email protected]]
Sent: Tuesday, April 7, 2020 6:04 PM
To: Sebastian von Ohr <[email protected]>; Vinod Koul <[email protected]>; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for

empty

Quoted 6 lines

list

Thanks for reminding me. Somehow I missed it. You mentioned in one of earlier thread that this bug is introduced it using dma_sync_wait to wait for DMA completion. So to reproduce the issue in xilinx axidma test client I have to replace issue_pending with sync_wait API?

Quoted 8 lines

Yes, dma_sync_wait triggered the bug for me almost every transfer. In the xilinx axidmatest this is probably best achieved by adding dma_sync_wait before the wait_for_completion_timeout. I encountered the bug with your xilinx-v2019.2.01 tag. On this tag it actually crashes the kernel with an invalid memory access (because the residue is written to desc). With the current driver version it probably seems to work fine. You might have to add some debug print to verify that the active_list can indeed be empty in xilinx_dma_tx_status.

I tried with xilinx-v2019.2.01 tag and added dma_sync_wait before wait_for _completion_timeout still the bug is not reproduced. I guess it's difficult to reproduce as it is dependent on actual timing on the events i.e tx_status checks for cookie status and it is not complete. Then soon interrupt handler is triggered for transfer complete and it updates the empty list and when it is again accessed in tx_status it results in data corruption.

But just to ensure that I am using the same sequence is it possible to share the patch for axidmatest client?

#8 lore raw

I've attached a patch below. When using this patch with the xilinx-v2019.2.01 tag I get a kernel panic immediately when loading the module. Maybe the exact components on the FPGA are also important. I have one AXI DMA component with scatter/gather enabled, read/write widths set to 64bit and a max burst size of 16.

drivers/dma/xilinx/axidmatest.c +2 -0
index 3d88982c9f7e..757bab152e0a 100644
@@ -407,6 +407,7 @@ static int dmatest_slave_func(void *data)
 		dma_async_issue_pending(tx_chan);
 		dma_async_issue_pending(rx_chan);
 
+		dma_sync_wait(tx_chan, tx_cookie);
 		tx_tmo = wait_for_completion_timeout(&tx_cmp, tx_tmo);
 
 		status = dma_async_is_tx_complete(tx_chan, tx_cookie,
@@ -428,6 +429,7 @@ static int dmatest_slave_func(void *data)
 			continue;
 		}
 
+		dma_sync_wait(rx_chan, rx_cookie);
 		rx_tmo = wait_for_completion_timeout(&rx_cmp, rx_tmo);
 		status = dma_async_is_tx_complete(rx_chan, rx_cookie,
 							NULL, NULL);
#9 lore raw
Quoted 34 lines

-----Original Message-----
From: Sebastian von Ohr <[email protected]>
Sent: Wednesday, April 8, 2020 8:49 PM
To: Radhey Shyam Pandey <[email protected]>; [email protected]; Appana Durga Kedareswara Rao <[email protected]>; Michal Simek <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] dmaengine: xilinx_dma: Add missing check for empty list

I've attached a patch below. When using this patch with the xilinx- v2019.2.01
tag I get a kernel panic immediately when loading the module. Maybe the exact
components on the FPGA are also important. I have one AXI DMA component with
scatter/gather enabled, read/write widths set to 64bit and a max burst size of 16.

drivers/dma/xilinx/axidmatest.c +1 -0
index 3d88982c9f7e..757bab152e0a 100644
@@ -407,6 +407,7 @@ static int dmatest_slave_func(void *data)
 		dma_async_issue_pending(tx_chan);
 		dma_async_issue_pending(rx_chan);
 
+		dma_sync_wait(tx_chan, tx_cookie);
 		tx_tmo = wait_for_completion_timeout(&tx_cmp, tx_tmo);
 
 		status = dma_async_is_tx_complete(tx_chan, tx_cookie,

drivers/dma/xilinx/axidmatest.c static int dmatest_slave_func(void *data) · line 435

Quoted 7 lines
drivers/dma/xilinx/axidmatest.c +1 -0
@@ -428,6 +429,7 @@ static int dmatest_slave_func(void *data)
 			continue;
 		}
 
+		dma_sync_wait(rx_chan, rx_cookie);
 		rx_tmo = wait_for_completion_timeout(&rx_cmp, rx_tmo);
 		status = dma_async_is_tx_complete(rx_chan, rx_cookie,
 							NULL, NULL);

Still, the issue is not reproduced, but as you also mentioned it might depend on design, exact timing on events, etc. The patch checks active list empty state before accessing it so that looks correct. In my test, it doesn't break any of the existing functionality so adding a tested-by tag.

maintainer Applied
#10 lore raw
In reply to Sebastian von Ohr
Quoted 6 lines · Sebastian von Ohr

The DMA transfer might finish just after checking the state with dma_cookie_status, but before the lock is acquired. Not checking for an empty list in xilinx_dma_tx_status may result in reading random data or data corruption when desc is written to. This can be reliably triggered by using dma_sync_wait to wait for DMA completion.

Applied, thanks

Signature · 1 line
~Vinod