Skip to contentSkip to conversation

ASoC: dapm: Fix race condition in widgets power list creation

PATCH Mark Brown 3 participants 9 messages Jan 18, 2011 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Mark Brown

author
#1 lore raw
Quoted 7 lines · Misael Lopez Cruz

Multiple calls to dapm_power_widgets() can create a race condition causing power list to be corrupted. Those scenarios can occur in multistream usecases, stream start/stop along with simultaneous update power calls (mixer, mux, dapm_sync, stream_event).

A new 'dapm_mutex' is added to soc_card, which is held while power list is created.

This needs a bit more analysis - why are we getting multiple simultaneous calls to dapm_power_widgets() and is that itself safe? The ASoC locking model has always been to have a big lock around the entire card rather than to lock subcomponents, and for example this isn't going to make sure we're consistent with register map accesses from other parts of the code.

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#3 lore raw
Quoted 2 lines · Peter Ujfalusi

Hmm, the patch looks quite similar to the patch I've sent back in November:
http://mailman.alsa-project.org/pipermail/alsa-devel/2010-November/033362.html

My thought exactly.

Quoted 9 lines · ext Mark Brown
Quoted 7 lines

This needs a bit more analysis - why are we getting multiple simultaneous calls to dapm_power_widgets() and is that itself safe? The ASoC locking model has always been to have a big lock around the entire card rather than to lock subcomponents, and for example this isn't going to make sure we're consistent with register map accesses from other parts of the code.

Quoted 5 lines

As far as I have traced back than, it was due to the use of dapm_sync in machine drivers. For me it looked that all other paths were satisfactory protected.
In my case the use of SOC_DAPM_PIN_SWITCH() instead of the custom call fixed the race (since this will take a card mutex, AFAIK).

I *suspect* that we're in a similar case and either the locking has been broken by the multi-component stuff or there's other cases that need protection, probably the jack detection code.

Quoted 4 lines

PS: I might received this mail unintentionally, since I was not on the TO/CC, and alsa-devel neither.

Should alsa-devel be on the CC?

Yes, I bounced on the second copy - Misael had sent two copies of the mail, one omitting the CC, and I'd replied to the first one before I saw the second.

#4 lore raw

Resend, since alsa-devel address was mistyped ;)

Quoted 3 lines · ext Mark Brown
Quoted 3 lines

I *suspect* that we're in a similar case and either the locking has been broken by the multi-component stuff or there's other cases that need protection, probably the jack detection code.

The snd_soc_dapm_sync is just a wrapper for dapm_power_widgets, right? It (snd_soc_dapm_sync) is used in soc-core:soc_post_component_init. But there is no need for it, since the snd_soc_dapm_new_widgets will call dapm_power_widgets at the end. so we can remove the snd_soc_dapm_sync from there.
For the rest:
I would replace the snd_soc_dapm_sync calls in soc-dapm.c with dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP); calls. Modify the snd_soc_dapm_sync to use the codec->mutex around the dapm_power_widgets call.
In that way I think all race conditions going to be covered without adding new mutex.

What do you think?
I can make a patch...

--
Péter

author
#5 lore raw
Quoted 8 lines · Peter Ujfalusi
Quoted 3 lines · ext Mark Brown
Quoted 3 lines

I *suspect* that we're in a similar case and either the locking has been broken by the multi-component stuff or there's other cases that need protection, probably the jack detection code.

The snd_soc_dapm_sync is just a wrapper for dapm_power_widgets, right? It (snd_soc_dapm_sync) is used in soc-core:soc_post_component_init.

You're jumping into the analysis mid-way here again...

Quoted 2 lines

Modify the snd_soc_dapm_sync to use the codec->mutex around the dapm_power_widgets call.

This doesn't sound like the right solution in a multi-CODEC system, DAPM will affect multiple devices within the system.

#6 lore raw
Quoted 4 lines · ext Mark Brown
Quoted 2 lines

The snd_soc_dapm_sync is just a wrapper for dapm_power_widgets, right? It (snd_soc_dapm_sync) is used in soc-core:soc_post_component_init.

You're jumping into the analysis mid-way here again...

Unfortunately yes I did :(

Quoted 5 lines
Quoted 2 lines

Modify the snd_soc_dapm_sync to use the codec->mutex around the dapm_power_widgets call.

This doesn't sound like the right solution in a multi-CODEC system, DAPM will affect multiple devices within the system.

Yes, you are right.

But I think the snd_soc_dapm_sync call in soc_post_component_init can be removed, since it has been taken care of the snd_soc_dapm_new_widgets call...
Surely it is not solving the problem, in fact it has nothing to do with the problem ;)

Signature · 1 line
Péter
#7 lore raw
Quoted 3 lines · On Tue, 18 Jan 2011 14:47:17 +0200 Peter Ujfalusi <[email protected]>…

But I think the snd_soc_dapm_sync call in soc_post_component_init can be removed, since it has been taken care of the snd_soc_dapm_new_widgets call...

Looks like it can be removed from machine init callbacks too since the snd_soc_dapm_new_widgets is called after machine initialization in soc_post_component_init.

Signature · 1 line
Jarkko
author
#8 lore raw
In reply to Peter Ujfalusi
Quoted 3 lines · Peter Ujfalusi

But I think the snd_soc_dapm_sync call in soc_post_component_init can be removed, since it has been taken care of the snd_soc_dapm_new_widgets call...

Yes.

#9 lore raw
In reply to Peter Ujfalusi
Quoted 20 lines · On Tuesday 18 January 2011 14:08:51 ext Peter Ujfalusi wrote:

Resend, since alsa-devel address was mistyped ;)

Quoted 3 lines · ext Mark Brown
Quoted 3 lines

I *suspect* that we're in a similar case and either the locking has been broken by the multi-component stuff or there's other cases that need protection, probably the jack detection code.

The snd_soc_dapm_sync is just a wrapper for dapm_power_widgets, right? It (snd_soc_dapm_sync) is used in soc-core:soc_post_component_init. But there is no need for it, since the snd_soc_dapm_new_widgets will call dapm_power_widgets at the end. so we can remove the snd_soc_dapm_sync from there.
For the rest:
I would replace the snd_soc_dapm_sync calls in soc-dapm.c with dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP); calls. Modify the snd_soc_dapm_sync to use the codec->mutex around the dapm_power_widgets call.
In that way I think all race conditions going to be covered without adding new mutex.

Quoted 2 lines

What do you think?
I can make a patch...

Or not.. soc-jack.c:snd_soc_jack_report also calls snd_soc_dapm_sync, while keeping the codec->mutex locked...
Should machine drivers also take the mutex when they call snd_soc_dapm_sync?

Signature · 1 line
Péter
author
#10 lore raw
Quoted 3 lines · Peter Ujfalusi

Or not.. soc-jack.c:snd_soc_jack_report also calls snd_soc_dapm_sync, while keeping the codec->mutex locked...
Should machine drivers also take the mutex when they call snd_soc_dapm_sync?

In general yes.