Re: [PATCH] ALSA: cmipci: Fix possible a data race in snd_cmipci_interrupt()

From: Takashi Iwai
Date: Mon Jan 13 2020 - 03:40:45 EST


On Mon, 13 Jan 2020 09:20:37 +0100,
Jia-Ju Bai wrote:
>
>
>
> On 2020/1/12 16:20, Takashi Iwai wrote:
> > On Sat, 11 Jan 2020 17:30:27 +0100,
> > Jia-Ju Bai wrote:
> >> The functions snd_cmipci_interrupt() and snd_cmipci_capture_trigger()
> >> may be concurrently executed.
> >>
> >> The function snd_cmipci_capture_trigger() calls
> >> snd_cmipci_pcm_trigger(). In snd_cmipci_pcm_trigger(), the variable
> >> rec->running is written with holding a spinlock cm->reg_lock. But in
> >> snd_cmipci_interrupt(), the identical variable cm->channel[0].running
> >> or cm->channel[1].running is read without holding this spinlock. Thus,
> >> a possible data race may occur.
> >>
> >> To fix this data race, in snd_cmipci_interrupt(), the variables
> >> cm->channel[0].running and cm->channel[1].running are read with holding
> >> the spinlock cm->reg_lock.
> >>
> >> This data race is found by the runtime testing of our tool DILP-2.
> >>
> >> Signed-off-by: Jia-Ju Bai <baijiaju1990@xxxxxxxxx>
> > Thanks for the patch.
> >
> > That's indeed a kind of race, but this change won't fix anything in
> > practice, though. The inconsistent running flag between those places,
> > there are two cases:
> >
> > - running became 0 to 1; this cannot happen, as the irq isn't issued
> > before the stream gets started
> >
> > - running became 1 to 0; this means that the stream gets stopped
> > between two points, and it's not better to call
> > snd_pcm_period_elapsed() for an already stopped stream.
>
> Thanks for the reply :)
>
> I am not sure to understand your words.
>
> Do you mean that this code should be also protected by the spinlock?
> ÂÂÂ if (cm->pcm) {
> ÂÂÂ ÂÂÂ if ((status & CM_CHINT0) && cm->channel[0].running)
> ÂÂÂ ÂÂÂ ÂÂÂ snd_pcm_period_elapsed(cm->channel[0].substream);
> ÂÂÂ ÂÂÂ if ((status & CM_CHINT1) && cm->channel[1].running)
> ÂÂÂ ÂÂÂ ÂÂÂ snd_pcm_period_elapsed(cm->channel[1].substream);
> ÂÂÂ }

No, it can't be protected as it would lead to ABBA deadlock.
That said, it's rather safe to leave the code as is.


thanks,

Takashi