Re: [PATCH v2 1/2] ASoC: meson: gx-formatter: change stream lock from mutex to spinlock
From: Martin Blumenstingl
Date: Thu Nov 12 2026 - 22:48:43 EST
On Wed, Nov 11, 2026 at 11:12 AM Valerio Setti <vsetti@xxxxxxxxxxxx> wrote:
>
> Currently a mutex is being used in 'struct gx_stream' to serialize
> access to structure data. This was inherited from the AXG design. An
> important difference though is that in axg-card dai_links are nonatomic
> so callback functions like 'trigger' are allowed to sleep. In GX instead
> dai_links are atomic.
>
> For example, starting playback from userspace with
> 'CONFIG_DEBUG_ATOMIC_SLEEP=y' results in a warning message like:
>
> [ ] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:316
> [ ] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 272, name: speaker-test
> [ ] preempt_count: 1, expected: 0
>
> due to the following call stack:
>
> snd_pcm_do_start (called with spinlock taken)
> dpcm_fe_dai_trigger
> dpcm_fe_dai_do_trigger
> dpcm_dai_trigger_fe_be
> dpcm_be_dai_trigger
> soc_pcm_trigger
> snd_soc_pcm_dai_trigger
> soc_dai_trigger
> aiu_encoder_i2s_trigger
> gx_stream_start (takes 'ts->lock')
>
> Fix this by changing the lock type from mutex to spinlock. This is safe
> on GX platform because all formatters' functions only perform MMIO regmap
> calls that do not sleep.
>
> 'spin_lock_irqsave' is used in 'gx_stream_start'/'gx_stream_stop' because
> trigger runs with IRQs already disabled and stop can also run in IRQ
> context (e.g. in case of XRUN while executing 'aiu_fifo_isr').
> 'spin_lock_irq' is instead used in 'gx_formatter_attach'/
> 'gx_formatter_detach' in order to prevent IRQs on the same CPU that might
> end up calling 'gx_stream_stop' and therefore deadlock.
>
> While at this also improve 'gx_formatter_detach()' by moving
> 'gx_formatter_disable()' under the spinlock guard. This makes it symmetric
> to 'gx_formatter_attach()' and ensures that the prepare/enable/disable
> formatter callbacks are called with spinlock held.
>
> Fixes: a295be8d6210 ("ASoC: meson: gx: add gx-formatter and gx-interface")
> Signed-off-by: Valerio Setti <vsetti@xxxxxxxxxxxx>
Reviewed-by: Martin Blumenstingl <martin.blumenstingl@xxxxxxxxxxxxxx>