Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: emu8000: shut down the pcm timer before freeing snd_emu8k_pcm
Date: Wed, 02 Dec 2026 03:22:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-244759@kernel.org> (raw)
In-Reply-To: <20261202103300.4-1-xuhx@hust.edu.cn>

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

      reply	other threads:[~2026-12-02  3:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-244759@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xuhx@hust.edu.cn \
    /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