Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] iio: adc: stm32-mdf: race updating cb and cb_priv in stm32_mdf_get_buff_cb() - [Medium] iio: adc: stm32-mdf: out-of-bounds or uninitialized channel in stm32_mdf_audio_init() - [Medium] iio: adc: stm32-mdf: unlocked freq reset in stm32_mdf_adc_write_raw() - [Medium] iio: stm32-mdf: bad mask and bypass in stm32_mdf_adc_apply_filters_config() - [Low] iio: adc: stm32-mdf: inaccurate kernel-doc Return for stm32_mdf_get_sub_channels_nb() --
commit dd4cd2a4f3dd0be22589f1468dca26c3807bd280 Author: Olivier Moysan <[email protected]> iio: adc: stm32-mdf: add dmic audio filter support This commit extends the STM32 MDF filter driver to support digital microphone audio filters and adds callback and sub-channel helpers for audio consumers. > diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c > index 97099b4adffc6..27c47bef71a42 100644 > --- a/drivers/iio/adc/stm32-mdf-adc.c > +++ b/drivers/iio/adc/stm32-mdf-adc.c [ ... ] > @@ -1128,6 +1129,42 @@ static const struct iio_buffer_setup_ops > stm32_mdf_buffer_setup_ops = { > .predisable = &stm32_mdf_adc_predisable, > }; > > +/** > + * stm32_mdf_get_sub_channels_nb() - Get the number of MDF sub-channels > + * @indio_dev: MDF IIO device > + * > + * Return: 0 on success, or a negative error code on failure. > + */ [Severity: Low] The Return description in the kernel-doc says this returns 0 on success or a negative error code on failure, whereas stm32_mdf_get_sub_channels_nb() returns an unsigned int sub-channel count (nb_interleave or 1). > +unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *indio_dev) > +{ > + struct stm32_mdf_adc *adc = iio_priv(indio_dev); > + > + if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) > + return adc->mdf->nb_interleave; > + > + return 1; > +} > +EXPORT_SYMBOL_GPL(stm32_mdf_get_sub_channels_nb); [Severity: Medium] Does stm32_mdf_adc_apply_filters_config() misconfigure MDF_DFLTCICR_REG and drop rsflt_bypass on secondary interleaved filters? drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_apply_filters_config() { if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) { list_for_each_entry(adc_inter, &adc->mdf->filter_list, entry) { if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter)) { adc_inter->datsrc = adc->datsrc; adc_inter->cicmode = adc->cicmode; adc_inter->decim_cic = adc->decim_cic; adc_inter->nbdis = adc->nbdis; adc_inter->hpf_cutoff = adc->hpf_cutoff; stm32_mdf_adc_apply_filters_config(adc_inter, scale); cnt++; } } ... /* Configure CICR */ msk = MDF_SITFCR_SCKSRC_MASK | MDF_DFLTCICR_CICMOD_MASK | MDF_DFLTCICR_MCICD_MASK | MDF_DFLTCICR_SCALE_MASK; val = FIELD_PREP(MDF_SITFCR_SCKSRC_MASK, adc->datsrc) | FIELD_PREP(MDF_DFLTCICR_CICMOD_MASK, adc->cicmode) | FIELD_PREP(MDF_DFLTCICR_MCICD_MASK, adc->decim_cic - 1) | FIELD_PREP(MDF_DFLTCICR_SCALE_MASK, scale); ret = regmap_update_bits(adc->regmap, MDF_DFLTCICR_REG, msk, val); } When programming MDF_DFLTCICR_REG, msk and val use MDF_SITFCR_SCKSRC_MASK (GENMASK(2, 1)) instead of MDF_DFLTCICR_DATSRC_MASK (GENMASK(1, 0)), which shifts adc->datsrc into bits [2:1] and leaves bit 0 untouched. In addition, the interleaved filter loop does not copy adc->rsflt_bypass to adc_inter->rsflt_bypass, leaving the reshape filter enabled on secondary interleaved filters when filter 0 has st,rs-filter-bypass set. [ ... ] > @@ -1474,6 +1514,13 @@ static int stm32_mdf_adc_read_label(struct iio_dev > *indio_dev, const struct iio_ > return sysfs_emit(label, "%s\n", name); > } > > +static const struct iio_info stm32_mdf_info_audio = { > + .hwfifo_set_watermark = stm32_mdf_set_watermark, > + .write_raw = stm32_mdf_adc_write_raw, > + .read_raw = stm32_mdf_adc_read_raw, > + .read_label = stm32_mdf_adc_read_label, > +}; [Severity: Medium] Does stm32_mdf_adc_write_raw() reset adc->requested_sample_freq outside the direct-mode lock on error? drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_write_raw() { ... { IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim); if (IIO_DEV_ACQUIRE_FAILED(claim)) return -EBUSY; /* Set requested_sample_freq right now, as used by mdf_adc_set_samp_freq() */ adc->requested_sample_freq = val; if (adc->dev_data->type == STM32_MDF_IIO) { ret = stm32_mdf_core_lock_kclk_rate(adc->mdf); if (!ret) { ret = mdf_adc_set_samp_freq(indio_dev); stm32_mdf_core_unlock_kclk_rate(adc->mdf); } } } if (ret) adc->requested_sample_freq = 0; ... } The IIO_DEV_ACQUIRE_DIRECT_MODE() guard releases direct mode at the end of the inner block, before adc->requested_sample_freq is cleared. If stm32_mdf_core_lock_kclk_rate() or mdf_adc_set_samp_freq() fails, a concurrent direct-mode writer or buffer enable can observe the failed frequency or have its own valid update overwritten by 0. > @@ -1530,6 +1577,38 @@ static irqreturn_t stm32_mdf_irq(int irq, void *arg) > return IRQ_HANDLED; > } > > +static int stm32_mdf_audio_init(struct device *dev, struct iio_dev > *indio_dev) > +{ > + struct stm32_mdf_adc *adc = iio_priv(indio_dev); > + struct iio_chan_spec *ch; > + int ret; > + > + ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); > + if (!ch) > + return -ENOMEM; > + > + adc->channels = devm_kzalloc(&indio_dev->dev, sizeof(*adc->channels), > GFP_KERNEL); > + if (!adc->channels) > + return -ENOMEM; > + > + ret = stm32_mdf_adc_chan_init(indio_dev, ch); > + if (ret < 0) > + return dev_err_probe(dev, ret, "Channels init failed\n"); > + > + indio_dev->num_channels = 1; > + indio_dev->channels = ch; [Severity: Medium] Can stm32_mdf_adc_chan_init() write past ch and adc->channels or leave them uninitialized here? stm32_mdf_audio_init() allocates a single element for ch and adc->channels, but stm32_mdf_adc_chan_init() loops over every child node without a compatible property and indexes channels[chan_idx] and adc->channels[chan_idx] without checking the count. If a dmic node has more than one channel child node, stm32_mdf_adc_chan_init_one() writes out of bounds on both allocations. If there are no channel child nodes (such as on a secondary interleaved filter), stm32_mdf_adc_chan_init() returns 0, leaving indio_dev->num_channels set to 1 with ch[0] and adc->channels[0] uninitialized. [ ... ] > @@ -1599,11 +1678,77 @@ static const struct stm32_mdf_dev_data > stm32h7_mdf_adc_data = { > .init = stm32_mdf_adc_init, > }; > > +static const struct stm32_mdf_dev_data stm32_mdf_audio_data = { > + .type = STM32_MDF_AUDIO, > + .init = stm32_mdf_audio_init, > +}; > + > +/** > + * stm32_mdf_get_buff_cb() - register a callback that will be called when > + * DMA transfer period is achieved. > + * > + * @iio_dev: Handle to IIO device. > + * @cb: Pointer to callback function: > + * - data: pointer to data buffer > + * - size: size in byte of the data buffer > + * - private: pointer to consumer private structure. > + * @private: Pointer to consumer private structure. > + * > + * Return: 0 on success, or a negative error code on failure. > + */ > +int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev, > + int (*cb)(const void *data, size_t size, void > *private), void *private) > +{ > + struct stm32_mdf_adc *adc; > + > + if (!iio_dev) > + return -EINVAL; > + adc = iio_priv(iio_dev); > + > + if (!adc) > + return -EINVAL; > + > + adc->cb = cb; > + adc->cb_priv = private; > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(stm32_mdf_get_buff_cb); > + > +/** > + * stm32_mdf_release_buff_cb() - Unregister the buffer callback > + * > + * @iio_dev: Handle to IIO device. > + * > + * Return: 0 on success, or a negative error code on failure. > + */ > +int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev) > +{ > + struct stm32_mdf_adc *adc; > + > + if (!iio_dev) > + return -EINVAL; > + adc = iio_priv(iio_dev); > + > + if (!adc) > + return -EINVAL; > + > + adc->cb = NULL; > + adc->cb_priv = NULL; > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(stm32_mdf_release_buff_cb); [Severity: High] Can stm32_mdf_get_buff_cb() and stm32_mdf_release_buff_cb() race with stm32_mdf_adc_dma_buffer_done() and cause a NULL pointer dereference? In the ASoC consumer driver, iio_channel_start_all_cb() is called during DAI prepare, which runs stm32_mdf_adc_postenable() and starts cyclic DMA before PCM trigger start, keeping it running across trigger stop. As a result, the DMA completion callback stm32_mdf_adc_dma_buffer_done() is already running when stm32_mdf_get_buff_cb() and stm32_mdf_release_buff_cb() update adc->cb and adc->cb_priv without locking or ordering: CPU0 CPU1 stm32_mdf_get_buff_cb() adc->cb = cb; stm32_mdf_adc_dma_buffer_done() if (adc->cb) adc->cb(..., adc->cb_priv); <-- cb_priv is still NULL adc->cb_priv = private; Similarly, stm32_mdf_release_buff_cb() clears adc->cb and adc->cb_priv without synchronizing with stm32_mdf_adc_dma_buffer_done(), so CPU1 can pass a cleared adc->cb_priv or jump through a NULL adc->cb after the if (adc->cb) check. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=10
