From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Subject: Re: [PATCH v2 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
Date: Thu, 03 Dec 2026 11:44:14 +0100 [thread overview]
Message-ID: <87mrqmve2p.wl-tiwai@suse.de> (raw)
In-Reply-To: <sashiko-outbox-247430@kernel.org>
On Thu, 03 Dec 2026 10:09:55 +0100,
sashiko-bot@kernel.org wrote:
>
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [High] ALSA: pcm: keeping SETUP state on mmapped hw_params() error
> breaks prepare()
> --
>
> commit 2f6fa4226b678a6d8b5f71debbc0c801c44f35a7
> Author: Takashi Iwai <tiwai@suse.de>
>
> ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
>
> This commit skips transitioning the stream to SNDRV_PCM_STATE_OPEN, calling
> hw_free, and freeing pages on snd_pcm_hw_params() errors when
> substream->mmap_count is non-zero.
>
> Link: https://lore.kernel.org/20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com
>
> > diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> > index 897975e43a9bd..6d17be7f9beb8 100644
> > --- a/sound/core/pcm_native.c
> > +++ b/sound/core/pcm_native.c
> > @@ -893,7 +893,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
> >
> > err = 0;
> > _error:
> > - if (err) {
> > + if (err && !atomic_read(&substream->mmap_count)) {
> > /* hardware might be unusable from this time,
> > * so we force application to retry to set
> > * the correct hardware parameter settings
>
> [Severity: High]
> Can skipping the transition to SNDRV_PCM_STATE_OPEN here allow
> ops->prepare() to run on hardware state torn down by a failed
> ops->hw_params()?
>
> On a mapped OSS stream, snd_pcm_oss_make_ready() checks runtime->oss.params
> and runtime->oss.prepare without holding runtime->oss.params_lock, then
> acquires the lock and calls snd_pcm_oss_prepare() without rechecking
> runtime->oss.params:
>
> CPU0 CPU1
> snd_pcm_oss_post()
> snd_pcm_oss_make_ready()
> check oss.params (0)
> check oss.prepare (1)
> snd_pcm_oss_set_rate()
> snd_pcm_oss_change_params_locked()
> snd_pcm_hw_params()
> ops->hw_params() fails
> (state stays SETUP)
> lock(&oss.params_lock)
> snd_pcm_oss_prepare()
> ops->prepare() <-- runs on torn-down hardware
>
> Previously, snd_pcm_hw_params() moved the stream to SNDRV_PCM_STATE_OPEN on
> error, so snd_pcm_pre_prepare() rejected the call with -EBADFD. With the
> stream left in SNDRV_PCM_STATE_SETUP, snd_pcm_pre_prepare() succeeds and
> invokes ops->prepare() after the driver already released its resources.
>
> For example, when snd_emu10k1_playback_hw_params() fails in
> snd_emu10k1_pcm_channel_alloc() after freeing voices, the subsequent
> snd_emu10k1_playback_prepare() dereferences NULL epcm->voices[0] in
> snd_emu10k1_pcm_init_voice().
>
> Should snd_pcm_oss_make_ready() check runtime->oss.params under
> runtime->oss.params_lock and the PCM core still prevent ops->prepare() after
> a failed ops->hw_params() when mmap_count is non-zero?
OK, this race can happen, and adding a check of runtime->oss.params
would be the simplest workaround.
And it'll fix the issue reported for the second patch, too.
https://lore.kernel.org/sashiko-outbox-247452@kernel.org
Takashi
next prev parent reply other threads:[~2026-12-03 10:44 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 8:40 [PATCH v2 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
2026-12-03 8:40 ` [PATCH v2 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
2026-12-03 9:09 ` sashiko-bot
2026-12-03 10:44 ` Takashi Iwai [this message]
2026-12-03 8:40 ` [PATCH v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
2026-12-03 9:23 ` sashiko-bot
2026-12-03 8:40 ` [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
2026-12-03 9:10 ` sashiko-bot
2026-12-03 8:40 ` [PATCH v2 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
2026-12-03 9:09 ` sashiko-bot
2026-12-03 10:47 ` Takashi Iwai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87mrqmve2p.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=linux-sound@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox