Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Imre Kaloz" <kaloz@kernel.org>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v2] ALSA: Add driver for SGI RAD1 audio
Date: Mon, 30 Nov 2026 16:11:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-241554@kernel.org> (raw)
In-Reply-To: <20261130154427.17604-1-kaloz@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: rad1: unlocked ring teardown and unprepared rec handling
  in snd_rad1_pcm_ack()
--

commit 10fddb039778896fba936b5a79d001b4e23fb5ff
Author: Imre Kaloz <kaloz@kernel.org>

ALSA: Add driver for SGI RAD1 audio

This commit adds an ALSA PCI audio driver for the SGI RAD1 controller
found in SGI Octane workstations, supporting analog and AES3 I/O.

> diff --git a/sound/pci/rad1.c b/sound/pci/rad1.c
> new file mode 100644
> index 0000000000000..1521557a64a7f
> --- /dev/null
> +++ b/sound/pci/rad1.c

[ ... ]

> +static int snd_rad1_pcm_hw_free(struct snd_pcm_substream *substream)
> +{
> +	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
> +
> +	/* .ack may run before the next prepare sets rec up for a new ring */
> +	memset(&pipe->rec, 0, sizeof(pipe->rec));
> +	if (pipe->ring.area) {
> +		snd_dma_free_pages(&pipe->ring);
> +		pipe->ring.area = NULL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int snd_rad1_pcm_hw_params(struct snd_pcm_substream *substream,
> +				  struct snd_pcm_hw_params *params)
> +{
> +	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
> +		return 0;
> +
> +	snd_rad1_pcm_hw_free(substream);
> +
> +	return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, substream->pcm->card->dev,
> +				   params_buffer_bytes(params), &pipe->ring);
> +}
> +
> +static int snd_rad1_pcm_prepare(struct snd_pcm_substream *substream)
> +{
> +	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
> +	struct snd_rad1 *chip = pipe->chip;
> +	struct snd_pcm_runtime *runtime = substream->runtime;
> +	const struct snd_rad1_pcm_desc *desc = pipe->desc;
> +	dma_addr_t addr = runtime->dma_addr;
> +	u32 ctl;
> +
> +	pipe->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
> +	pipe->period_bytes = snd_pcm_lib_period_bytes(substream);
> +	pipe->period = 0;
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		addr = pipe->ring.addr;
> +		memset(pipe->ring.area, 0, pipe->buffer_bytes);
> +		memset(&pipe->rec, 0, sizeof(pipe->rec));
> +		pipe->rec.hw_buffer_size = pipe->buffer_bytes;
> +		pipe->rec.sw_buffer_size = pipe->buffer_bytes;
> +	}

[ ... ]

> +static void snd_rad1_pcm_resync(struct snd_rad1_pipe *pipe,
> +				snd_pcm_uframes_t hw_ptr)
> +{
> +	unsigned int start = pipe->rec.hw_io;
> +	unsigned int bytes = max(pipe->rec.hw_ready, 0);
> +
> +	if (start + bytes > pipe->buffer_bytes) {
> +		memset(pipe->ring.area, 0, start + bytes - pipe->buffer_bytes);
> +		bytes = pipe->buffer_bytes - start;
> +	}
> +	memset(pipe->ring.area + start, 0, bytes);
> +
> +	pipe->rec.appl_ptr = hw_ptr;
> +	pipe->rec.hw_data = pipe->rec.hw_io;
> +	pipe->rec.sw_data = pipe->rec.sw_io;
> +	pipe->rec.hw_ready = 0;
> +	pipe->rec.sw_ready = 0;
> +}
> +
> +static int snd_rad1_pcm_ack(struct snd_pcm_substream *substream)
> +{
> +	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
> +	struct snd_pcm_runtime *runtime = substream->runtime;
> +	struct snd_pcm_indirect *rec = &pipe->rec;
> +	snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
> +	snd_pcm_uframes_t frames;
> +
> +	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
> +		return 0;
> +
> +	/* a rewind moves appl_ptr back over samples already in the ring */
> +	frames = appl_ptr - rec->appl_ptr;
> +	if (appl_ptr < rec->appl_ptr)
> +		frames += runtime->boundary;
> +	if (frames > runtime->buffer_size)
> +		snd_rad1_pcm_resync(pipe, runtime->status->hw_ptr);
> +
> +	return snd_pcm_indirect_playback_transfer(substream, rec,\n> +						  snd_rad1_pcm_copy);
> +}

[Severity: High]
Can snd_rad1_pcm_ack() race with snd_rad1_pcm_hw_free() or run when
pipe->rec is not prepared, causing a use-after-free or NULL pointer
dereference on pipe->ring.area?

snd_pcm_hw_free(), snd_pcm_hw_params() and snd_pcm_prepare() hold
runtime->buffer_mutex without acquiring the PCM stream lock, while
snd_pcm_sync_ptr() without SNDRV_PCM_SYNC_PTR_HWSYNC acquires only the PCM
stream lock before calling snd_rad1_pcm_ack():

CPU0                                  CPU1
snd_pcm_sync_ptr()
  snd_pcm_stream_lock_irq()
  pcm_lib_apply_appl_ptr()
    snd_rad1_pcm_ack()
      snd_pcm_indirect_playback_transfer()
                                      snd_pcm_hw_free()
                                        snd_rad1_pcm_hw_free()
                                          memset(&pipe->rec, 0, ...)
                                          snd_dma_free_pages(&pipe->ring)
                                          pipe->ring.area = NULL
        snd_rad1_pcm_copy()
          dst[i] = ...  <-- use-after-free or NULL deref on pipe->ring.area

Even without a concurrent caller, if SNDRV_PCM_IOCTL_SYNC_PTR runs after
hw_params or hw_free before prepare (when rec->hw_buffer_size is 0) with
frames > runtime->buffer_size, snd_rad1_pcm_ack() calls
snd_rad1_pcm_resync(), which passes a NULL pipe->ring.area to memset()
after hw_free and sets rec->appl_ptr to a potentially non-zero
runtime->status->hw_ptr left over from an earlier run, causing
snd_pcm_indirect_playback_transfer() to fail with -EPIPE.

Acquiring pcm_stream_lock_irqsave(substream) when resetting pipe->rec in
snd_rad1_pcm_hw_free() and snd_rad1_pcm_prepare() (before freeing
pipe->ring) and returning 0 from snd_rad1_pcm_ack() when
!rec->hw_buffer_size would avoid both issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130154427.17604-1-kaloz@kernel.org?part=1

      reply	other threads:[~2026-11-30 16:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 15:44 [PATCH v2] ALSA: Add driver for SGI RAD1 audio Imre Kaloz
2026-11-30 16:11 ` 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-241554@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kaloz@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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