Skip to content

Commit dd04bb1

Browse files
committed
ALSA: snd-aloop - fix locking issues (running flag updates)
On SMP machines, the cable->running update must be atomic, otherwise stream is not started correctly sometimes. Signed-off-by: Jaroslav Kysela <perex@perex.cz>
1 parent e74670b commit dd04bb1

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

‎sound/drivers/aloop.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,17 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
263263
return err;
264264
dpcm->last_jiffies = jiffies;
265265
dpcm->pcm_rate_shift = 0;
266-
loopback_timer_start(dpcm);
266+
spin_lock(&cable->lock);
267267
cable->running |= (1 << substream->stream);
268+
spin_unlock(&cable->lock);
269+
loopback_timer_start(dpcm);
268270
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
269271
loopback_active_notify(dpcm);
270272
break;
271273
case SNDRV_PCM_TRIGGER_STOP:
274+
spin_lock(&cable->lock);
272275
cable->running &= ~(1 << substream->stream);
276+
spin_unlock(&cable->lock);
273277
loopback_timer_stop(dpcm);
274278
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
275279
loopback_active_notify(dpcm);
@@ -454,28 +458,30 @@ static void loopback_bytepos_update(struct loopback_pcm *dpcm,
454458
}
455459
}
456460

457-
static void loopback_pos_update(struct loopback_cable *cable)
461+
static unsigned int loopback_pos_update(struct loopback_cable *cable)
458462
{
459463
struct loopback_pcm *dpcm_play =
460464
cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
461465
struct loopback_pcm *dpcm_capt =
462466
cable->streams[SNDRV_PCM_STREAM_CAPTURE];
463467
unsigned long delta_play = 0, delta_capt = 0;
468+
unsigned int running;
464469

465470
spin_lock(&cable->lock);
466-
if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
471+
running = cable->running;
472+
if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
467473
delta_play = jiffies - dpcm_play->last_jiffies;
468474
dpcm_play->last_jiffies += delta_play;
469475
}
470476

471-
if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
477+
if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
472478
delta_capt = jiffies - dpcm_capt->last_jiffies;
473479
dpcm_capt->last_jiffies += delta_capt;
474480
}
475481

476482
if (delta_play == 0 && delta_capt == 0) {
477483
spin_unlock(&cable->lock);
478-
return;
484+
return running;
479485
}
480486

481487
if (delta_play > delta_capt) {
@@ -490,27 +496,27 @@ static void loopback_pos_update(struct loopback_cable *cable)
490496

491497
if (delta_play == 0 && delta_capt == 0) {
492498
spin_unlock(&cable->lock);
493-
return;
499+
return running;
494500
}
495501
/* note delta_capt == delta_play at this moment */
496502
loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
497503
loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
498504
spin_unlock(&cable->lock);
505+
return running;
499506
}
500507

501508
static void loopback_timer_function(unsigned long data)
502509
{
503510
struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
504-
int stream;
511+
unsigned int running;
505512

506-
loopback_pos_update(dpcm->cable);
507-
stream = dpcm->substream->stream;
508-
if (dpcm->cable->running & (1 << stream))
513+
running = loopback_pos_update(dpcm->cable);
514+
if (running & (1 << dpcm->substream->stream)) {
509515
loopback_timer_start(dpcm);
510-
if (dpcm->period_update_pending) {
511-
dpcm->period_update_pending = 0;
512-
if (dpcm->cable->running & (1 << stream))
516+
if (dpcm->period_update_pending) {
517+
dpcm->period_update_pending = 0;
513518
snd_pcm_period_elapsed(dpcm->substream);
519+
}
514520
}
515521
}
516522

0 commit comments

Comments
 (0)