All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm
@ 2026-12-02  2:33 Hanxiang Xu
  2026-12-02  3:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:33 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

emu8k_pcm_timer_func() re-arms itself unconditionally every jiffy
while the stream runs (emu8000_pcm.c:200), without checking
rec->timer_running or rec->running.  stop_voice() only performs a
non-synchronizing timer_delete(), so a callback that is already
dequeued on another CPU re-arms the timer after the stop believes it
dead.  emu8k_pcm_close() then kfree()s the struct with the timer
still armed, and the timer core executes the callback on the freed
memory (and re-arms the freed timer_list).

Check the stop flag inside the callback before re-arming, and shut
the timer down synchronously in emu8k_pcm_close() before freeing the
struct.

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

diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
index d2ca70aad1621eecfe1dc39451cb100c85ed3a29..360e0d05a5fd6398feb26bf2efeb64900e479ee9 100644
@@ -197,7 +197,8 @@
 		rec->last_ptr = ptr;
 
 		/* reprogram timer */
-		mod_timer(&rec->timer, jiffies + 1);
+		if (rec->timer_running)
+			mod_timer(&rec->timer, jiffies + 1);
 
 		/* update period */
 		if (rec->period_pos >= (int)rec->period_size) {
@@ -246,6 +247,8 @@ static int emu8k_pcm_close(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	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm
  2026-12-02  2:33 [PATCH] ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm Hanxiang Xu
@ 2026-12-02  3:22 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:22 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: emu8000: incomplete timer stop sync in
  emu8k_pcm_timer_func() and close
--

commit 82fbf8abf709cff25e215ab2626ead1e43756cea
Author: Hanxiang Xu <xuhx@hust.edu.cn>

ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm

This commit checks rec->timer_running before re-arming the PCM timer in
emu8k_pcm_timer_func() and calls timer_shutdown_sync() in emu8k_pcm_close()
before freeing struct snd_emu8k_pcm.

> diff --git a/sound/isa/sb/emu8000_pcm.c b/sound/isa/sb/emu8000_pcm.c
> index d2ca70aad1621..360e0d05a5fd6 100644
> --- a/sound/isa/sb/emu8000_pcm.c
> +++ b/sound/isa/sb/emu8000_pcm.c
> @@ -197,7 +197,8 @@ static void emu8k_pcm_timer_func(struct timer_list *t)
>  		rec->last_ptr = ptr;
>  
>  		/* reprogram timer */
> -		mod_timer(&rec->timer, jiffies + 1);
> +		if (rec->timer_running)
> +			mod_timer(&rec->timer, jiffies + 1);
>  
>  		/* update period */
>  		if (rec->period_pos >= (int)rec->period_size) {
> @@ -246,6 +247,8 @@ 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: Medium]
Can an already-dequeued emu8k_pcm_timer_func() callback still race with
stream teardown or re-preparation before emu8k_pcm_close() is reached?

CPU0                                  CPU1
stop_voice()
  spin_lock(&rec->timer_lock)
  timer_delete(&rec->timer)
  rec->timer_running = 0
  spin_unlock(&rec->timer_lock)
                                      emu8k_pcm_timer_func()
emu8k_pcm_hw_free()                     spin_lock(&rec->timer_lock)
  emu8k_close_dram(rec->emu)            ptr = emu8k_get_curpos(rec, 0)
  snd_util_mem_free(...)                ...
                                        snd_pcm_period_elapsed(...)
                                          <-- runs on stopped/freed hw

Because rec->timer_running is only checked around mod_timer(), a callback
that was already dequeued when stop_voice() ran still reads hardware
registers, updates rec->period_pos and rec->last_ptr, and calls
snd_pcm_period_elapsed() after the stream has stopped.

In addition, synchronizing the timer only in emu8k_pcm_close() is too late
for snd_pcm_release_substream(), which runs do_hw_free() ->
emu8k_pcm_hw_free() before substream->ops->close(), as well as for
SNDRV_PCM_IOCTL_HW_FREE and SNDRV_PCM_IOCTL_PREPARE on an open substream
(where emu8k_pcm_prepare() resets rec->last_ptr, rec->period_pos,
rec->buf_size, and rec->period_size without holding rec->timer_lock).

Checking !rec->timer_running at the start of the rec->timer_lock scope in
emu8k_pcm_timer_func() and wiring up a snd_pcm_ops.sync_stop callback that
calls timer_delete_sync(&rec->timer) would ensure the timer is quiescent
before hw_free and prepare as well as close.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202103300.4-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:22 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:33 [PATCH] ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm Hanxiang Xu
2026-12-02  3:22 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.