Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] ASoC: stm32: amdf: wrong S16_LE source offset on wrap in
  stm32_afsdm_pcm_cb()
- [High] ASoC: stm32: amdf: broken period elapsed condition in
  stm32_afsdm_pcm_cb()
- [High] ASoC: stm32: amdf: broken watermark and channel limits in
  interleaved mode
- [High] ASoC: stm32: amdf: unsynchronized DMA callback registration,
  stop, and pos update
- [Medium] ASoC: stm32: amdf: SNDRV_PCM_INFO_PAUSE set without pause
  trigger handling
- [Low] ASoC: stm32: amdf: invalid %pK format in dev_dbg() in
  stm32_afsdm_pcm_cb()
- [Low] ASoC: stm32: amdf: missing period_bytes_min in stm32_amdf_pcm_hw
--

commit 2a312f0d863bc42aba2f6fceb749066af9dab645
Author: Olivier Moysan <[email protected]>

ASoC: stm32: add mdf dai support

This commit adds an ASoC DAI and platform driver for PDM digital
microphones connected to the STM32 Multi-function Digital Filter (MDF)
IP via the IIO consumer buffer interface.

> diff --git a/sound/soc/stm/stm32_amdf.c b/sound/soc/stm/stm32_amdf.c
> new file mode 100644
> index 0000000000000..a733a7a2997fa
> --- /dev/null
> +++ b/sound/soc/stm/stm32_amdf.c
> @@ -0,0 +1,375 @@

[ ... ]

> +static const struct snd_pcm_hardware stm32_amdf_pcm_hw = {
> +     .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
> +             SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_PAUSE,
> +     .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
> +
> +     .channels_min = 1,
> +     .channels_max = 1,
> +
> +     .periods_min = 2,
> +     .periods_max = MDF_MAX_PERIODS,
> +
> +     .period_bytes_max = MDF_MAX_PERIOD_SIZE,
> +     .buffer_bytes_max = MDF_MAX_PERIODS * MDF_MAX_PERIOD_SIZE
> +};

[Severity: Low]
Should stm32_amdf_pcm_hw define .period_bytes_min?

Without .period_bytes_min, SNDRV_PCM_HW_PARAM_PERIOD_BYTES has a minimum
of 0.  An unconstrained hw_params request can then fail during interval
refinement with -EINVAL, or userspace can configure a 1-frame period and
set the IIO watermark to 1, generating a DMA interrupt on every frame.

[ ... ]

> +static void stm32_memcpy_32to16(void *dest, const void *src, size_t n)
> +{
> +     unsigned int i = 0;
> +     u16 *d = (u16 *)dest, *s = (u16 *)src;
> +
> +     s++;
> +     for (i = n >> 1; i > 0; i--) {
> +             *d++ = *s++;
> +             s++;
> +     }
> +}
> +
> +static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
> +{
> +     struct stm32_amdf_priv *priv = private;
> +     struct snd_soc_pcm_runtime *rtd = 
> snd_soc_substream_to_rtd(priv->substream);
> +     u8 *pcm_buff = priv->pcm_buff;
> +     u8 *src_buff = (u8 *)data;
> +     unsigned int old_pos = priv->pos;
> +     size_t buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
> +     size_t period_size = snd_pcm_lib_period_bytes(priv->substream);
> +     size_t cur_size, src_size = size;
> +     snd_pcm_format_t format = priv->substream->runtime->format;
> +
> +     if (format == SNDRV_PCM_FORMAT_S16_LE)
> +             src_size >>= 1;
> +     cur_size = src_size;
> +
> +     dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
> +             __func__, &pcm_buff[priv->pos], priv->pos, src_size);

[Severity: Low]
Does %pK work in stm32_afsdm_pcm_cb()?

This callback runs from the DMA completion handler in softirq or hardirq
context, where %pK prints "pK-error" when kptr_restrict is 1; commit
4db4ce15706d ("ASoC: stm32: dfsdm: don't use %pK through printk")
switched the same debug print in stm32_adfsdm.c to %p.

> +
> +     if ((priv->pos + src_size) > buff_size) {
> +             if (format == SNDRV_PCM_FORMAT_S16_LE)
> +                     stm32_memcpy_32to16(&pcm_buff[priv->pos], src_buff, 
> buff_size - priv->pos);
> +             else
> +                     memcpy(&pcm_buff[priv->pos], src_buff, buff_size - 
> priv->pos);
> +             cur_size -= buff_size - priv->pos;
> +             priv->pos = 0;
> +     }
> +
> +     if (format == SNDRV_PCM_FORMAT_S16_LE)
> +             stm32_memcpy_32to16(&pcm_buff[priv->pos], &src_buff[src_size - 
> cur_size], cur_size);
> +     else
> +             memcpy(&pcm_buff[priv->pos], &src_buff[src_size - cur_size], 
> cur_size);

[Severity: High]
Does the second stm32_memcpy_32to16() call read from the wrong offset in
src_buff when the ring buffer wraps in SNDRV_PCM_FORMAT_S16_LE mode?

When format is SNDRV_PCM_FORMAT_S16_LE, src_size and cur_size are halved
to measure 16-bit destination bytes, while src_buff holds 32-bit samples
and stm32_memcpy_32to16() consumes 2 * n source bytes to write n
destination bytes.  After the first copy writes buff_size - priv->pos
destination bytes, indexing &src_buff[src_size - cur_size] advances
src_buff by only half the consumed source bytes instead of
2 * (src_size - cur_size), re-copying earlier samples and misaligning
the 16-bit word offset whenever an odd number of samples was copied
before the wrap.

> +
> +     priv->pos = (priv->pos + cur_size) % buff_size;
> +
> +     if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
> +             snd_pcm_period_elapsed(priv->substream);
> +
> +     return 0;
> +}

[Severity: High]
Does this skip snd_pcm_period_elapsed() whenever old_pos is 0?

At stream start (and at the start of every new buffer cycle when
buff_size is a multiple of period_size), old_pos is 0 and cur_size
equals src_size, so the condition evaluates to false and drops the first
period notification of every buffer cycle.  When old_pos is non-zero,
old_pos % period_size < size also compares the pre-copy PCM byte offset
against the unscaled 32-bit DMA size (which is twice the PCM byte count
in S16_LE mode) rather than checking whether advancing by src_size bytes
crossed a period boundary.

> +
> +static int stm32_amdf_trigger(struct snd_soc_component *component,
> +                           struct snd_pcm_substream *substream, int cmd)
> +{
> +     struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> +     struct stm32_amdf_priv *priv =
> +             snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
> +
> +     switch (cmd) {
> +     case SNDRV_PCM_TRIGGER_START:
> +     case SNDRV_PCM_TRIGGER_RESUME:
> +             priv->pos = 0;
> +             return stm32_mdf_get_buff_cb(priv->iio_ch->indio_dev, 
> stm32_afsdm_pcm_cb, priv);
> +     case SNDRV_PCM_TRIGGER_SUSPEND:
> +     case SNDRV_PCM_TRIGGER_STOP:
> +             return stm32_mdf_release_buff_cb(priv->iio_ch->indio_dev);
> +     }
> +
> +     return -EINVAL;
> +}

[Severity: Medium]
Does stm32_amdf_trigger() fail with -EINVAL on pause requests despite
stm32_amdf_pcm_hw advertising SNDRV_PCM_INFO_PAUSE?

SNDRV_PCM_TRIGGER_PAUSE_PUSH and SNDRV_PCM_TRIGGER_PAUSE_RELEASE are not
handled in the switch and fall through to return -EINVAL.  In addition,
SNDRV_PCM_TRIGGER_RESUME resets priv->pos to 0 instead of preserving the
buffer position across suspend/resume.

[Severity: High]
Can stm32_amdf_trigger() race with an active DMA completion callback in
stm32_mdf_adc_dma_buffer_done()?

stm32_amdf_dai_prepare() starts cyclic IIO DMA via
iio_channel_start_all_cb() before trigger() is called, and DMA continues
running until stm32_amdf_shutdown().  While DMA callbacks are firing,
stm32_mdf_get_buff_cb() and stm32_mdf_release_buff_cb() set or clear
adc->cb and adc->cb_priv without locking or stopping DMA:

drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_dma_buffer_done() {
    ...
                if (adc->bufi >= adc->buf_sz) {
                        if (adc->cb)
                                adc->cb(&adc->rx_buf[old_pos], adc->buf_sz - 
old_pos, adc->cb_priv);
                        adc->bufi = 0;
                        old_pos = 0;
                }
                if (adc->dev_data->type == STM32_MDF_IIO)
                        iio_push_to_buffers(indio_dev, buffer);
                available -= indio_dev->scan_bytes;
        }
        if (adc->cb)
                adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, 
adc->cb_priv);
}

CPU0                                      CPU1 (<softirq>)
stm32_amdf_trigger(..., STOP)
  stm32_mdf_release_buff_cb()
                                          stm32_mdf_adc_dma_buffer_done()
                                            if (adc->cb)
    adc->cb = NULL;
    adc->cb_priv = NULL;
                                              adc->cb(..., adc->cb_priv)
                                                <-- NULL deref

The same NULL dereference can happen on a single CPU if
snd_pcm_period_elapsed() during the first adc->cb() call in
stm32_mdf_adc_dma_buffer_done() triggers an XRUN stop before the second
adc->cb() call, or on START if CPU1 observes adc->cb before adc->cb_priv
is written.  Because STOP does not wait for in-flight callbacks to
finish, stm32_afsdm_pcm_cb() can also keep writing to priv->pcm_buff
after hw_free, and setting priv->pos = 0 mid-copy in stm32_afsdm_pcm_cb()
exposes a transient 0 offset to concurrent lockless
stm32_amdf_pcm_pointer() readers.

[ ... ]

> +static int stm32_amdf_pcm_hw_params(struct snd_soc_component *component,
> +                                 struct snd_pcm_substream *substream,
> +                                 struct snd_pcm_hw_params *params)
> +{
> +     struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> +     struct stm32_amdf_priv *priv =
> +             snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
> +
> +     priv->pcm_buff = substream->runtime->dma_area;
> +
> +     return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
> +                                                params_period_size(params));
> +}

[ ... ]

> +static int stm32_amdf_probe(struct platform_device *pdev)
> +{

[ ... ]

> +     channels_max = stm32_mdf_get_sub_channels_nb(priv->iio_ch->indio_dev);
> +     priv->dai_drv.capture.channels_max = channels_max;

[Severity: High]
Does multi-channel interleaved capture misconfigure the IIO watermark and
channel constraints when channels_max > 1?

In stm32_mdf_audio_init(), the audio IIO device always sets
indio_dev->num_channels = 1 with 32-bit storage, so indio_dev->scan_bytes
is 4 bytes (one sample) rather than a multi-channel frame:

drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_set_watermark() {
    ...
        watermark = min(val, MDF_DMA_BUFFER_SIZE / (2 * scan_bytes));
        adc->buf_sz = 2 * watermark * scan_bytes;
}

Passing params_period_size(params) (in frames) instead of
params_period_size(params) * params_channels(params) sizes each DMA
period to 1 / channels of the ALSA period.  Also, stm32_amdf_pcm_hw
keeps .channels_max = 1 (overriding the DAI's channels_max in
stm32_amdf_pcm_open()), and priv->dai_drv.capture.channels_min is left
at 1 even though the MDF hardware always interleaves all channels_max
filters into the DMA stream.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=11

Reply via email to