dmaengine: xilinx_dma: Add missing check for empty list
Subsystems (2)
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
@@ -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
Vinod Koul
maintainerQuoted 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 inxilinx_dma_tx_statusmay result in reading random data or data corruption when desc is written to. This can be reliably triggered by usingdma_sync_waitto wait for DMA completion.
Appana, Radhey can you please test this..?
Quoted 39 lines
Signed-off-by Sebastian von Ohr <[email protected]>--- drivers/dma/xilinx/xilinx_dma.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)drivers/dma/xilinx/xilinx_dma.c +10
-10@@ -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
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 inxilinx_dma_tx_statusmay result in reading random data or data corruption when desc is written to. This can be reliably triggered by usingdma_sync_waitto 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
Signed-off-by Sebastian von Ohr <[email protected]>--- drivers/dma/xilinx/xilinx_dma.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)(quoted diff) +0
-0
b/drivers/dma/xilinx/xilinx_dma.cindexa9c5d5cc9f2b..5d5f1d0ce16c100644drivers/dma/xilinx/xilinx_dma.c +0
-0@@ -1229,16 +1229,16 @@ static enum dma_statusxilinx_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
-8return 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
Vinod Koul
maintainerQuoted 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 listMinor nit - Better to also add <...> "in
device_tx_statuscallback "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 inxilinx_dma_tx_statusmay result in reading random data or data corruption when desc is written to. This can be reliably triggered by usingdma_sync_waitto 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_statussays locking is not required. This scenario is a race condition when the driver callsdma_cookie_statusand 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 intx_statusit 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
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.
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 listQuoted 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?
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_waitto wait for DMA completion. So to reproduce the issue in xilinx axidma test client I have to replaceissue_pendingwithsync_waitAPI?
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.
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 listQuoted 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 forempty
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_waitto wait for DMA completion. So to reproduce the issue in xilinx axidma test client I have to replaceissue_pendingwithsync_waitAPI?
Quoted 8 lines
Yes,
dma_sync_waittriggered the bug for me almost every transfer. In the xilinx axidmatest this is probably best achieved by addingdma_sync_waitbefore thewait_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 theactive_listcan indeed be empty inxilinx_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?
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
@@ -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 );
Radhey Shyam Pandey
Tested-byQuoted 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 listI'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@@ -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.
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 inxilinx_dma_tx_statusmay result in reading random data or data corruption when desc is written to. This can be reliably triggered by usingdma_sync_waitto wait for DMA completion.
Applied, thanks
Signature · 1 line
~Vinod