Skip to content

Commit 19242d7

Browse files
committed
async_tx: fix multiple dependency submission
Shrink struct dma_async_tx_descriptor and introduce async_tx_channel_switch to properly inject a channel switch interrupt in the descriptor stream. This simplifies the locking model as drivers no longer need to handle dma_async_tx_descriptor.lock. Acked-by: Shannon Nelson <shannon.nelson@intel.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 1c62979 commit 19242d7

4 files changed

Lines changed: 170 additions & 47 deletions

File tree

‎crypto/async_tx/async_tx.c‎

Lines changed: 162 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,19 @@ dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
8989
iter = tx;
9090

9191
/* find the root of the unsubmitted dependency chain */
92-
while (iter->cookie == -EBUSY) {
92+
do {
9393
parent = iter->parent;
94-
if (parent && parent->cookie == -EBUSY)
95-
iter = iter->parent;
96-
else
94+
if (!parent)
9795
break;
98-
}
96+
else
97+
iter = parent;
98+
} while (parent);
99+
100+
/* there is a small window for ->parent == NULL and
101+
* ->cookie == -EBUSY
102+
*/
103+
while (iter->cookie == -EBUSY)
104+
cpu_relax();
99105

100106
status = dma_sync_wait(iter->chan, iter->cookie);
101107
} while (status == DMA_IN_PROGRESS || (iter != tx));
@@ -111,24 +117,33 @@ EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
111117
void
112118
async_tx_run_dependencies(struct dma_async_tx_descriptor *tx)
113119
{
114-
struct dma_async_tx_descriptor *dep_tx, *_dep_tx;
115-
struct dma_device *dev;
120+
struct dma_async_tx_descriptor *next = tx->next;
116121
struct dma_chan *chan;
117122

118-
list_for_each_entry_safe(dep_tx, _dep_tx, &tx->depend_list,
119-
depend_node) {
120-
chan = dep_tx->chan;
121-
dev = chan->device;
122-
/* we can't depend on ourselves */
123-
BUG_ON(chan == tx->chan);
124-
list_del(&dep_tx->depend_node);
125-
tx->tx_submit(dep_tx);
126-
127-
/* we need to poke the engine as client code does not
128-
* know about dependency submission events
129-
*/
130-
dev->device_issue_pending(chan);
123+
if (!next)
124+
return;
125+
126+
tx->next = NULL;
127+
chan = next->chan;
128+
129+
/* keep submitting up until a channel switch is detected
130+
* in that case we will be called again as a result of
131+
* processing the interrupt from async_tx_channel_switch
132+
*/
133+
while (next && next->chan == chan) {
134+
struct dma_async_tx_descriptor *_next;
135+
136+
spin_lock_bh(&next->lock);
137+
next->parent = NULL;
138+
_next = next->next;
139+
next->next = NULL;
140+
spin_unlock_bh(&next->lock);
141+
142+
next->tx_submit(next);
143+
next = _next;
131144
}
145+
146+
chan->device->device_issue_pending(chan);
132147
}
133148
EXPORT_SYMBOL_GPL(async_tx_run_dependencies);
134149

@@ -397,6 +412,92 @@ static void __exit async_tx_exit(void)
397412
}
398413
#endif
399414

415+
416+
/**
417+
* async_tx_channel_switch - queue an interrupt descriptor with a dependency
418+
* pre-attached.
419+
* @depend_tx: the operation that must finish before the new operation runs
420+
* @tx: the new operation
421+
*/
422+
static void
423+
async_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx,
424+
struct dma_async_tx_descriptor *tx)
425+
{
426+
struct dma_chan *chan;
427+
struct dma_device *device;
428+
struct dma_async_tx_descriptor *intr_tx = (void *) ~0;
429+
430+
/* first check to see if we can still append to depend_tx */
431+
spin_lock_bh(&depend_tx->lock);
432+
if (depend_tx->parent && depend_tx->chan == tx->chan) {
433+
tx->parent = depend_tx;
434+
depend_tx->next = tx;
435+
intr_tx = NULL;
436+
}
437+
spin_unlock_bh(&depend_tx->lock);
438+
439+
if (!intr_tx)
440+
return;
441+
442+
chan = depend_tx->chan;
443+
device = chan->device;
444+
445+
/* see if we can schedule an interrupt
446+
* otherwise poll for completion
447+
*/
448+
if (dma_has_cap(DMA_INTERRUPT, device->cap_mask))
449+
intr_tx = device->device_prep_dma_interrupt(chan);
450+
else
451+
intr_tx = NULL;
452+
453+
if (intr_tx) {
454+
intr_tx->callback = NULL;
455+
intr_tx->callback_param = NULL;
456+
tx->parent = intr_tx;
457+
/* safe to set ->next outside the lock since we know we are
458+
* not submitted yet
459+
*/
460+
intr_tx->next = tx;
461+
462+
/* check if we need to append */
463+
spin_lock_bh(&depend_tx->lock);
464+
if (depend_tx->parent) {
465+
intr_tx->parent = depend_tx;
466+
depend_tx->next = intr_tx;
467+
async_tx_ack(intr_tx);
468+
intr_tx = NULL;
469+
}
470+
spin_unlock_bh(&depend_tx->lock);
471+
472+
if (intr_tx) {
473+
intr_tx->parent = NULL;
474+
intr_tx->tx_submit(intr_tx);
475+
async_tx_ack(intr_tx);
476+
}
477+
} else {
478+
if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)
479+
panic("%s: DMA_ERROR waiting for depend_tx\n",
480+
__func__);
481+
tx->tx_submit(tx);
482+
}
483+
}
484+
485+
486+
/**
487+
* submit_disposition - while holding depend_tx->lock we must avoid submitting
488+
* new operations to prevent a circular locking dependency with
489+
* drivers that already hold a channel lock when calling
490+
* async_tx_run_dependencies.
491+
* @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock
492+
* @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch
493+
* @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly
494+
*/
495+
enum submit_disposition {
496+
ASYNC_TX_SUBMITTED,
497+
ASYNC_TX_CHANNEL_SWITCH,
498+
ASYNC_TX_DIRECT_SUBMIT,
499+
};
500+
400501
void
401502
async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
402503
enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
@@ -405,28 +506,54 @@ async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
405506
tx->callback = cb_fn;
406507
tx->callback_param = cb_param;
407508

408-
/* set this new tx to run after depend_tx if:
409-
* 1/ a dependency exists (depend_tx is !NULL)
410-
* 2/ the tx can not be submitted to the current channel
411-
*/
412-
if (depend_tx && depend_tx->chan != chan) {
413-
/* if ack is already set then we cannot be sure
509+
if (depend_tx) {
510+
enum submit_disposition s;
511+
512+
/* sanity check the dependency chain:
513+
* 1/ if ack is already set then we cannot be sure
414514
* we are referring to the correct operation
515+
* 2/ dependencies are 1:1 i.e. two transactions can
516+
* not depend on the same parent
415517
*/
416-
BUG_ON(depend_tx->ack);
518+
BUG_ON(depend_tx->ack || depend_tx->next || tx->parent);
417519

418-
tx->parent = depend_tx;
520+
/* the lock prevents async_tx_run_dependencies from missing
521+
* the setting of ->next when ->parent != NULL
522+
*/
419523
spin_lock_bh(&depend_tx->lock);
420-
list_add_tail(&tx->depend_node, &depend_tx->depend_list);
421-
if (depend_tx->cookie == 0) {
422-
struct dma_chan *dep_chan = depend_tx->chan;
423-
struct dma_device *dep_dev = dep_chan->device;
424-
dep_dev->device_dependency_added(dep_chan);
524+
if (depend_tx->parent) {
525+
/* we have a parent so we can not submit directly
526+
* if we are staying on the same channel: append
527+
* else: channel switch
528+
*/
529+
if (depend_tx->chan == chan) {
530+
tx->parent = depend_tx;
531+
depend_tx->next = tx;
532+
s = ASYNC_TX_SUBMITTED;
533+
} else
534+
s = ASYNC_TX_CHANNEL_SWITCH;
535+
} else {
536+
/* we do not have a parent so we may be able to submit
537+
* directly if we are staying on the same channel
538+
*/
539+
if (depend_tx->chan == chan)
540+
s = ASYNC_TX_DIRECT_SUBMIT;
541+
else
542+
s = ASYNC_TX_CHANNEL_SWITCH;
425543
}
426544
spin_unlock_bh(&depend_tx->lock);
427545

428-
/* schedule an interrupt to trigger the channel switch */
429-
async_trigger_callback(ASYNC_TX_ACK, depend_tx, NULL, NULL);
546+
switch (s) {
547+
case ASYNC_TX_SUBMITTED:
548+
break;
549+
case ASYNC_TX_CHANNEL_SWITCH:
550+
async_tx_channel_switch(depend_tx, tx);
551+
break;
552+
case ASYNC_TX_DIRECT_SUBMIT:
553+
tx->parent = NULL;
554+
tx->tx_submit(tx);
555+
break;
556+
}
430557
} else {
431558
tx->parent = NULL;
432559
tx->tx_submit(tx);

‎drivers/dma/dmaengine.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,6 @@ void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
600600
{
601601
tx->chan = chan;
602602
spin_lock_init(&tx->lock);
603-
INIT_LIST_HEAD(&tx->depend_node);
604-
INIT_LIST_HEAD(&tx->depend_list);
605603
}
606604
EXPORT_SYMBOL(dma_async_tx_descriptor_init);
607605

‎drivers/dma/iop-adma.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
6363
struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
6464
{
6565
BUG_ON(desc->async_tx.cookie < 0);
66-
spin_lock_bh(&desc->async_tx.lock);
6766
if (desc->async_tx.cookie > 0) {
6867
cookie = desc->async_tx.cookie;
6968
desc->async_tx.cookie = 0;
@@ -101,7 +100,6 @@ iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
101100

102101
/* run dependent operations */
103102
async_tx_run_dependencies(&desc->async_tx);
104-
spin_unlock_bh(&desc->async_tx.lock);
105103

106104
return cookie;
107105
}
@@ -275,8 +273,11 @@ iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
275273

276274
static void iop_adma_tasklet(unsigned long data)
277275
{
278-
struct iop_adma_chan *chan = (struct iop_adma_chan *) data;
279-
__iop_adma_slot_cleanup(chan);
276+
struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
277+
278+
spin_lock(&iop_chan->lock);
279+
__iop_adma_slot_cleanup(iop_chan);
280+
spin_unlock(&iop_chan->lock);
280281
}
281282

282283
static struct iop_adma_desc_slot *

‎include/linux/dmaengine.h‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,9 @@ typedef void (*dma_async_tx_callback)(void *dma_async_param);
221221
* @callback: routine to call after this operation is complete
222222
* @callback_param: general parameter to pass to the callback routine
223223
* ---async_tx api specific fields---
224-
* @depend_list: at completion this list of transactions are submitted
225-
* @depend_node: allow this transaction to be executed after another
226-
* transaction has completed, possibly on another channel
224+
* @next: at completion submit this descriptor
227225
* @parent: pointer to the next level up in the dependency chain
228-
* @lock: protect the dependency list
226+
* @lock: protect the parent and next pointers
229227
*/
230228
struct dma_async_tx_descriptor {
231229
dma_cookie_t cookie;
@@ -236,8 +234,7 @@ struct dma_async_tx_descriptor {
236234
dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
237235
dma_async_tx_callback callback;
238236
void *callback_param;
239-
struct list_head depend_list;
240-
struct list_head depend_node;
237+
struct dma_async_tx_descriptor *next;
241238
struct dma_async_tx_descriptor *parent;
242239
spinlock_t lock;
243240
};

0 commit comments

Comments
 (0)