Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: sb: wait for the period timer before freeing the pcm private data
@ 2026-12-02  2:11 Hanxiang Xu
  2026-12-02  3:25 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:11 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

emu8k_pcm_close() kfrees struct snd_emu8k_pcm with no timer
synchronization: stop_voice() uses plain timer_delete(), which does not
wait for a callback instance that is already running, and the callback's
snd_pcm_period_elapsed(rec->substream) runs outside rec->timer_lock.
A close (or a driver removal freeing the substream) that lands while an
instance is in flight reads rec->substream from freed memory; a freed
callback that still re-arms puts a freed timer_list back into the
wheel.

Use timer_shutdown_sync() in emu8k_pcm_close(): it waits for an
in-flight instance and marks the timer so a late mod_timer() from the
callback is ignored.  In the common (already stopped) path it returns
immediately.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 sound/isa/sb/emu8000_pcm.c | 1 +
 1 file changed, 1 insertion(+), 0 deletions(-)

diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
index d2ca70a..3aeef31 100644
--- a/sound/isa/sb/emu8000_pcm.c
+++ b/sound/isa/sb/emu8000_pcm.c
@@ -246,6 +246,7 @@ static int emu8k_pcm_open(struct snd_pcm_substream *subs)
 static int emu8k_pcm_close(struct snd_pcm_substream *subs)
 {
 	struct snd_emu8k_pcm *rec = subs->runtime->private_data;
+	timer_shutdown_sync(&rec->timer);
 	kfree(rec);
 	subs->runtime->private_data = NULL;
 	return 0;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: sb: wait for the period timer before freeing the pcm private data
  2026-12-02  2:11 [PATCH] ALSA: sb: wait for the period timer before freeing the pcm private data Hanxiang Xu
@ 2026-12-02  3:25 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:25 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: sb: incomplete timer sync and re-arm race in
  emu8k_pcm_timer_func()
--

commit 16e83ea6e63d3c55432a440185c92457f6a2eb84
Author: Hanxiang Xu <xuhx@hust.edu.cn>

ALSA: sb: wait for the period timer before freeing the pcm private data

This commit adds a timer_shutdown_sync() call in emu8k_pcm_close() before
freeing the struct snd_emu8k_pcm instance.

> diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
> index d2ca70aad1621..3aeef313665e8 100644
> --- a/sound/isa/sb/emu8000_pcm.c
> +++ b/sound/isa/sb/emu8000_pcm.c
> @@ -246,6 +246,7 @@ static int emu8k_pcm_open(struct snd_pcm_substream *subs)
>  static int emu8k_pcm_close(struct snd_pcm_substream *subs)
>  {
>  	struct snd_emu8k_pcm *rec = subs->runtime->private_data;
> +	timer_shutdown_sync(&rec->timer);
>  	kfree(rec);
>  	subs->runtime->private_data = NULL;
>  	return 0;

[Severity: High]
Does synchronizing the timer only in emu8k_pcm_close() leave
emu8k_pcm_timer_func() racing with stop_voice(), emu8k_pcm_hw_free() and
driver removal?

emu8k_pcm_timer_func() unconditionally calls mod_timer() under
rec->timer_lock without checking rec->timer_running:

CPU0                              CPU1
<softirq>                         emu8k_pcm_trigger(STOP)
emu8k_pcm_timer_func()              stop_voice()
  spin_lock(&rec->timer_lock)         spin_lock_irqsave(&rec->timer_lock)
  (waits for lock)                    timer_delete(&rec->timer)
                                      rec->timer_running = 0
                                      spin_unlock_irqrestore(...)
  mod_timer(&rec->timer, ...)
  spin_unlock(&rec->timer_lock)
  snd_pcm_period_elapsed(...)     emu8k_pcm_hw_free()
                                    stop_voice()
                                      if (rec->timer_running) <-- false!
                                    emu8k_close_dram(rec->emu)
                                    snd_util_mem_free(..., rec->block)

Once rec->timer is re-armed with rec->timer_running set to 0, later
stop_voice() calls skip timer_delete(&rec->timer).  The 1-jiffy timer
keeps firing and polling EMU8000_CCCA_READ while the stream is stopped,
and runs concurrently with emu8k_pcm_hw_free() (which runs before
emu8k_pcm_close() even during snd_pcm_release_substream()) and
emu8k_pcm_prepare().

In addition, if the driver is removed while userspace holds an open PCM
file descriptor, snd_emu8000_remove() -> snd_device_free() runs
snd_pcm_dev_disconnect() (which calls snd_pcm_stop() and
snd_pcm_sync_stop(), not emu8k_pcm_close()) and then frees the substream
in snd_pcm_free_stream(), so an in-flight or self-rearmed
emu8k_pcm_timer_func() still hits a use-after-free on rec->substream.

Checking rec->timer_running before mod_timer() in emu8k_pcm_timer_func()
and wiring up a .sync_stop callback in emu8k_pcm_ops that calls
timer_delete_sync(&rec->timer) would cover non-closing stops, hw_free,
and device disconnect as well.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202101107.1-1-xuhx@hust.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:11 [PATCH] ALSA: sb: wait for the period timer before freeing the pcm private data Hanxiang Xu
2026-12-02  3:25 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox