Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] ALSA: ctxfi: wait for a running system-timer callback in the free path
@ 2026-11-26 12:25 Hanxiang Xu
  2026-11-26 12:52 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-11-26 12:25 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

Currently, ct_systimer_free is aliased to ct_systimer_prepare, which
calls timer_delete_sync_try(). While ct_systimer_prepare() must remain
non-sleeping because it can run under the PCM stream lock, using
timer_delete_sync_try() in the free path is unsafe: if the timer
callback is concurrently running on another CPU, timer_delete_sync_try()
returns -1 without waiting for completion. The caller then proceeds to
free the ct_timer_instance object, resulting in a use-after-free in the
still-running timer callback.

Replace the ct_systimer_free macro with a dedicated function that calls
timer_shutdown_sync(), which waits for any running callback to finish
and prevents the timer from being rearmed before the instance is freed.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
Changes in v2:
 - Set From: to Hanxiang Xu <xuhx@hust.edu.cn> matching Signed-off-by:
 - Use a single blank line before Cc: <stable@vger.kernel.org>
 - Format cleanly with git format-patch to preserve tabs and spacing

 sound/pci/ctxfi/cttimer.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/sound/pci/ctxfi/cttimer.c b/sound/pci/ctxfi/cttimer.c
index 9d6f5df..fea9d61 100644
--- a/sound/pci/ctxfi/cttimer.c
+++ b/sound/pci/ctxfi/cttimer.c
@@ -111,11 +111,17 @@ static void ct_systimer_stop(struct ct_timer_instance *ti)
 
 static void ct_systimer_prepare(struct ct_timer_instance *ti)
 {
+	/* non-sleeping: also runs under the PCM stream lock */
 	ct_systimer_stop(ti);
 	timer_delete_sync_try(&ti->timer);
 }
 
-#define ct_systimer_free	ct_systimer_prepare
+static void ct_systimer_free(struct ct_timer_instance *ti)
+{
+	/* may sleep: wait for a callback running on another CPU */
+	ct_systimer_stop(ti);
+	timer_shutdown_sync(&ti->timer);
+}
 
 static const struct ct_timer_ops ct_systimer_ops = {
 	.init = ct_systimer_init,
-- 
2.43.0


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

* Re: [PATCH v2] ALSA: ctxfi: wait for a running system-timer callback in the free path
  2026-11-26 12:25 [PATCH v2] ALSA: ctxfi: wait for a running system-timer callback in the free path Hanxiang Xu
@ 2026-11-26 12:52 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26 12:52 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: ctxfi: unsynchronized timer delete in
  ct_systimer_prepare()
- [Low] ALSA: ctxfi: missing Fixes trailer for commit b7bbf876087e
--

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

> ALSA: ctxfi: wait for a running system-timer callback in the free path
>
> Currently, ct_systimer_free is aliased to ct_systimer_prepare, which
> calls timer_delete_sync_try(). While ct_systimer_prepare() must remain
> non-sleeping because it can run under the PCM stream lock, using
> timer_delete_sync_try() in the free path is unsafe: if the timer
> callback is concurrently running on another CPU, timer_delete_sync_try()
> returns -1 without waiting for completion. The caller then proceeds to
> free the ct_timer_instance object, resulting in a use-after-free in the
> still-running timer callback.
>
> Replace the ct_systimer_free macro with a dedicated function that calls
> timer_shutdown_sync(), which waits for any running callback to finish
> and prevents the timer from being rearmed before the instance is freed.
>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>

[Severity: Low]
Should this include a Fixes tag for the commit that introduced the
ct_systimer_free alias:

  Fixes: b7bbf876087e ("ALSA: ctxfi - Use native timer interrupt on emu20k1")

> diff --git a/sound/pci/ctxfi/cttimer.c b/sound/pci/ctxfi/cttimer.c
> index 9d6f5df2bc7ef..fea9d61aa4569 100644
> --- a/sound/pci/ctxfi/cttimer.c
> +++ b/sound/pci/ctxfi/cttimer.c
> @@ -111,11 +111,17 @@ static void ct_systimer_stop(struct ct_timer_instance *ti)
>  
>  static void ct_systimer_prepare(struct ct_timer_instance *ti)
>  {
> +	/* non-sleeping: also runs under the PCM stream lock */
>  	ct_systimer_stop(ti);
>  	timer_delete_sync_try(&ti->timer);
>  }

[Severity: Medium]
Does ct_systimer_prepare() actually run under the PCM stream lock, and can
keeping timer_delete_sync_try() here leave ct_systimer_callback() running
concurrently with ct_timer_prepare()?

Now that ct_systimer_free() is separate, ct_systimer_prepare() is only
called from ct_timer_prepare() via atc_pcm_playback_prepare(),
atc_pcm_capture_prepare() and spdif_passthru_playback_prepare().
snd_pcm_prepare() drops pcm_stream_lock_irq before invoking the .prepare
callback via snd_pcm_action_nonatomic(), and those prepare functions
already allocate with GFP_KERNEL and lock atc->atc_mutex before calling
ct_timer_prepare().

Because timer_delete_sync_try() returns -1 without waiting when the
callback is in flight, an active callback can race with prepare and
subsequent start:

CPU0                              CPU1
ct_timer_prepare()                ct_systimer_callback()
  ct_systimer_prepare()             position = ...->pointer(...)
    ct_systimer_stop()
    timer_delete_sync_try() -> -1
  ti->position = 0
                                    ti->position = position
                                      <-- overwrites zeroed position
ct_timer_start()
  ct_systimer_start()
    ti->running = 1
    mod_timer(&ti->timer, ...)
                                    lock(&ti->lock)
                                    if (ti->running)
                                      mod_timer(&ti->timer, ...)
                                        <-- rearms with stale interval

Using timer_delete_sync(&ti->timer) in ct_systimer_prepare() and dropping
the comment would avoid this race.

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

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

end of thread, other threads:[~2026-11-26 12:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 12:25 [PATCH v2] ALSA: ctxfi: wait for a running system-timer callback in the free path Hanxiang Xu
2026-11-26 12:52 ` sashiko-bot

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