Skip to content

Commit 22b7de1

Browse files
westeridavem330
authored andcommitted
thunderbolt: Use spinlock in ring serialization
This makes it possible to enqueue frames also from atomic context which is needed for example, when networking packets are sent over a Thunderbolt cable. Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Reviewed-by: Michael Jamet <michael.jamet@intel.com> Reviewed-by: Yehezkel Bernat <yehezkel.bernat@intel.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2a91ec6 commit 22b7de1

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

drivers/thunderbolt/nhi.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ static void ring_work(struct work_struct *work)
212212
struct tb_ring *ring = container_of(work, typeof(*ring), work);
213213
struct ring_frame *frame;
214214
bool canceled = false;
215+
unsigned long flags;
215216
LIST_HEAD(done);
216-
mutex_lock(&ring->lock);
217+
218+
spin_lock_irqsave(&ring->lock, flags);
217219

218220
if (!ring->running) {
219221
/* Move all frames to done and mark them as canceled. */
@@ -241,7 +243,8 @@ static void ring_work(struct work_struct *work)
241243
ring_write_descriptors(ring);
242244

243245
invoke_callback:
244-
mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */
246+
/* allow callbacks to schedule new work */
247+
spin_unlock_irqrestore(&ring->lock, flags);
245248
while (!list_empty(&done)) {
246249
frame = list_first_entry(&done, typeof(*frame), list);
247250
/*
@@ -255,15 +258,17 @@ static void ring_work(struct work_struct *work)
255258

256259
int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
257260
{
261+
unsigned long flags;
258262
int ret = 0;
259-
mutex_lock(&ring->lock);
263+
264+
spin_lock_irqsave(&ring->lock, flags);
260265
if (ring->running) {
261266
list_add_tail(&frame->list, &ring->queue);
262267
ring_write_descriptors(ring);
263268
} else {
264269
ret = -ESHUTDOWN;
265270
}
266-
mutex_unlock(&ring->lock);
271+
spin_unlock_irqrestore(&ring->lock, flags);
267272
return ret;
268273
}
269274
EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
@@ -338,7 +343,7 @@ static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
338343
if (!ring)
339344
goto err;
340345

341-
mutex_init(&ring->lock);
346+
spin_lock_init(&ring->lock);
342347
INIT_LIST_HEAD(&ring->queue);
343348
INIT_LIST_HEAD(&ring->in_flight);
344349
INIT_WORK(&ring->work, ring_work);
@@ -371,8 +376,6 @@ static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
371376
return ring;
372377

373378
err:
374-
if (ring)
375-
mutex_destroy(&ring->lock);
376379
kfree(ring);
377380
mutex_unlock(&nhi->lock);
378381
return NULL;
@@ -419,7 +422,7 @@ void tb_ring_start(struct tb_ring *ring)
419422
u32 flags;
420423

421424
mutex_lock(&ring->nhi->lock);
422-
mutex_lock(&ring->lock);
425+
spin_lock_irq(&ring->lock);
423426
if (ring->nhi->going_away)
424427
goto err;
425428
if (ring->running) {
@@ -466,7 +469,7 @@ void tb_ring_start(struct tb_ring *ring)
466469
ring_interrupt_active(ring, true);
467470
ring->running = true;
468471
err:
469-
mutex_unlock(&ring->lock);
472+
spin_unlock_irq(&ring->lock);
470473
mutex_unlock(&ring->nhi->lock);
471474
}
472475
EXPORT_SYMBOL_GPL(tb_ring_start);
@@ -487,7 +490,7 @@ EXPORT_SYMBOL_GPL(tb_ring_start);
487490
void tb_ring_stop(struct tb_ring *ring)
488491
{
489492
mutex_lock(&ring->nhi->lock);
490-
mutex_lock(&ring->lock);
493+
spin_lock_irq(&ring->lock);
491494
dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
492495
RING_TYPE(ring), ring->hop);
493496
if (ring->nhi->going_away)
@@ -508,7 +511,7 @@ void tb_ring_stop(struct tb_ring *ring)
508511
ring->running = false;
509512

510513
err:
511-
mutex_unlock(&ring->lock);
514+
spin_unlock_irq(&ring->lock);
512515
mutex_unlock(&ring->nhi->lock);
513516

514517
/*
@@ -568,7 +571,6 @@ void tb_ring_free(struct tb_ring *ring)
568571
* to finish before freeing the ring.
569572
*/
570573
flush_work(&ring->work);
571-
mutex_destroy(&ring->lock);
572574
kfree(ring);
573575
}
574576
EXPORT_SYMBOL_GPL(tb_ring_free);

include/linux/thunderbolt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ struct tb_nhi {
448448
* @eof_mask: Bit mask used to detect end of frame PDF
449449
*/
450450
struct tb_ring {
451-
struct mutex lock;
451+
spinlock_t lock;
452452
struct tb_nhi *nhi;
453453
int size;
454454
int hop;

0 commit comments

Comments
 (0)