Skip to content

Commit b2cc5c4

Browse files
vvmitrofanovvinodkoul
authored andcommitted
dmaengine: sf-pdma: Add multithread support for a DMA channel
When we get a DMA channel and try to use it in multiple threads it will cause oops and hanging the system. % echo 64 > /sys/module/dmatest/parameters/threads_per_chan % echo 10000 > /sys/module/dmatest/parameters/iterations % echo 1 > /sys/module/dmatest/parameters/run [ 89.480664] Unable to handle kernel NULL pointer dereference at virtual address 00000000000000a0 [ 89.488725] Oops [#1] [ 89.494708] CPU: 2 PID: 1008 Comm: dma0chan0-copy0 Not tainted 5.17.0-rc5 [ 89.509385] epc : vchan_find_desc+0x32/0x46 [ 89.513553] ra : sf_pdma_tx_status+0xca/0xd6 This happens because of data race. Each thread rewrite channels's descriptor as soon as device_prep_dma_memcpy() is called. It leads to the situation when the driver thinks that it uses right descriptor that actually is freed or substituted for other one. With current fixes a descriptor changes its value only when it has been used. A new descriptor is acquired from vc->desc_issued queue that is already filled with descriptors that are ready to be sent. Threads have no direct access to DMA channel descriptor. Now it is just possible to queue a descriptor for further processing. Fixes: 6973886 ("dmaengine: sf-pdma: add platform DMA support for HiFive Unleashed A00") Signed-off-by: Viacheslav Mitrofanov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent 4ce653d commit b2cc5c4

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

drivers/dma/sf-pdma/sf-pdma.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ static inline struct sf_pdma_desc *to_sf_pdma_desc(struct virt_dma_desc *vd)
5252
static struct sf_pdma_desc *sf_pdma_alloc_desc(struct sf_pdma_chan *chan)
5353
{
5454
struct sf_pdma_desc *desc;
55-
unsigned long flags;
56-
57-
spin_lock_irqsave(&chan->lock, flags);
58-
59-
if (chan->desc && !chan->desc->in_use) {
60-
spin_unlock_irqrestore(&chan->lock, flags);
61-
return chan->desc;
62-
}
63-
64-
spin_unlock_irqrestore(&chan->lock, flags);
6555

6656
desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
6757
if (!desc)
@@ -111,7 +101,6 @@ sf_pdma_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dest, dma_addr_t src,
111101
desc->async_tx = vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
112102

113103
spin_lock_irqsave(&chan->vchan.lock, iflags);
114-
chan->desc = desc;
115104
sf_pdma_fill_desc(desc, dest, src, len);
116105
spin_unlock_irqrestore(&chan->vchan.lock, iflags);
117106

@@ -170,11 +159,17 @@ static size_t sf_pdma_desc_residue(struct sf_pdma_chan *chan,
170159
unsigned long flags;
171160
u64 residue = 0;
172161
struct sf_pdma_desc *desc;
173-
struct dma_async_tx_descriptor *tx;
162+
struct dma_async_tx_descriptor *tx = NULL;
174163

175164
spin_lock_irqsave(&chan->vchan.lock, flags);
176165

177-
tx = &chan->desc->vdesc.tx;
166+
list_for_each_entry(vd, &chan->vchan.desc_submitted, node)
167+
if (vd->tx.cookie == cookie)
168+
tx = &vd->tx;
169+
170+
if (!tx)
171+
goto out;
172+
178173
if (cookie == tx->chan->completed_cookie)
179174
goto out;
180175

@@ -241,6 +236,19 @@ static void sf_pdma_enable_request(struct sf_pdma_chan *chan)
241236
writel(v, regs->ctrl);
242237
}
243238

239+
static struct sf_pdma_desc *sf_pdma_get_first_pending_desc(struct sf_pdma_chan *chan)
240+
{
241+
struct virt_dma_chan *vchan = &chan->vchan;
242+
struct virt_dma_desc *vdesc;
243+
244+
if (list_empty(&vchan->desc_issued))
245+
return NULL;
246+
247+
vdesc = list_first_entry(&vchan->desc_issued, struct virt_dma_desc, node);
248+
249+
return container_of(vdesc, struct sf_pdma_desc, vdesc);
250+
}
251+
244252
static void sf_pdma_xfer_desc(struct sf_pdma_chan *chan)
245253
{
246254
struct sf_pdma_desc *desc = chan->desc;
@@ -268,8 +276,11 @@ static void sf_pdma_issue_pending(struct dma_chan *dchan)
268276

269277
spin_lock_irqsave(&chan->vchan.lock, flags);
270278

271-
if (vchan_issue_pending(&chan->vchan) && chan->desc)
279+
if (!chan->desc && vchan_issue_pending(&chan->vchan)) {
280+
/* vchan_issue_pending has made a check that desc in not NULL */
281+
chan->desc = sf_pdma_get_first_pending_desc(chan);
272282
sf_pdma_xfer_desc(chan);
283+
}
273284

274285
spin_unlock_irqrestore(&chan->vchan.lock, flags);
275286
}
@@ -298,6 +309,11 @@ static void sf_pdma_donebh_tasklet(struct tasklet_struct *t)
298309
spin_lock_irqsave(&chan->vchan.lock, flags);
299310
list_del(&chan->desc->vdesc.node);
300311
vchan_cookie_complete(&chan->desc->vdesc);
312+
313+
chan->desc = sf_pdma_get_first_pending_desc(chan);
314+
if (chan->desc)
315+
sf_pdma_xfer_desc(chan);
316+
301317
spin_unlock_irqrestore(&chan->vchan.lock, flags);
302318
}
303319

0 commit comments

Comments
 (0)