Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Srinivas Kandagatla" <srinivas.kandagatla@oss.qualcomm.com>
Cc: linux-sound@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v2 04/24] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links
Date: Sun, 22 Nov 2026 16:38:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-228113@kernel.org> (raw)
In-Reply-To: <20261122161437.5994-5-srinivas.kandagatla@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] ASoC: qcom: sdw: dangling DAI stream pointers on stream release
- [High] ASoC: qcom: sdw: stream runtime leak in
  qcom_snd_sdw_setup_dir()
- [Medium] ASoC: qcom: sdw: missing direction check in
  qcom_snd_sdw_get_stream()
- [Low] ASoC: qcom: sdw: missing DAI ID check in qcom_snd_sdw_shutdown()
--

commit 68888aeca44192cad54581921c24aeb932a0fbd3
Author: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>

ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links

This commit splits the SoundWire startup, prepare, hw_free, and shutdown
helpers into per-direction operations so that BE DAI links with
multi_direction_codecs allocate and manage a separate sdw_stream_runtime
for each direction.

> diff --git a/sound/soc/qcom/sdw.c b/sound/soc/qcom/sdw.c
> index 6576b47a4c8c3..953a6eaae1272 100644
> --- a/sound/soc/qcom/sdw.c
> +++ b/sound/soc/qcom/sdw.c
> @@ -5,9 +5,55 @@
>  #include <dt-bindings/sound/qcom,lpass.h>
>  #include <dt-bindings/sound/qcom,q6afe.h>
>  #include <linux/module.h>
> +#include <linux/slab.h>
>  #include <sound/soc.h>
>  #include "sdw.h"
>  
> +static struct sdw_stream_runtime *
> +qcom_snd_sdw_alloc_stream(const char *cpu_name, int dir)
> +{
> +	const char *suffix = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
> +			     "Playback" : "Capture";
> +	struct sdw_stream_runtime *sr;
> +	char *name;
> +
> +	name = kasprintf(GFP_KERNEL, "%s-%s", cpu_name, suffix);
> +	if (!name)
> +		return NULL;
> +
> +	sr = sdw_alloc_stream(name, SDW_STREAM_PCM);
> +	if (!sr) {
> +		kfree(name);
> +		return NULL;
> +	}
> +	return sr;
> +}
> +
> +static void qcom_snd_sdw_release_stream(struct sdw_stream_runtime *sr)
> +{
> +	if (!sr)
> +		return;
> +	kfree(sr->name);
> +	sdw_release_stream(sr);
> +}

[Severity: High]
Does releasing sr without clearing the stream pointer on the codec DAIs
leave a dangling sdw_stream_runtime pointer behind?

qcom_snd_sdw_setup_dir() stores sruntime in each codec DAI via
snd_soc_dai_set_stream(codec_dai, sruntime, dir) (for example in
ctrl->sruntime[dai->id] in qcom_swrm_set_sdw_stream()).  When
qcom_snd_sdw_shutdown() or the error unwind in qcom_snd_sdw_setup_dir() and
qcom_snd_sdw_startup() calls qcom_snd_sdw_release_stream(), sr->name and sr
are freed without calling snd_soc_dai_set_stream(codec_dai, NULL, dir).

Because qcom_swrm_get_sdw_stream() still returns ctrl->sruntime[dai->id], a
subsequent sdw_find_dir_stream() call (such as when another DAI link
sharing the same SoundWire master DAI closes, or when a later startup on a
multi-master link fails before overwriting every DAI's stream pointer)
retrieves the freed pointer and passes it to qcom_snd_sdw_release_stream()
again, causing a use-after-free on sr->name and a double free of sr->name
and sr.

> +
> +static struct sdw_stream_runtime *
> +sdw_find_dir_stream(struct snd_soc_pcm_runtime *rtd, int dir)
> +{
> +	struct snd_soc_dai *codec_dai;
> +	struct sdw_stream_runtime *sr;
> +	int i;
> +
> +	for_each_rtd_codec_dais(rtd, i, codec_dai) {
> +		if (!snd_soc_dai_stream_valid(codec_dai, dir))
> +			continue;
> +		sr = snd_soc_dai_get_stream(codec_dai, dir);
> +		if (!IS_ERR(sr) && sr)
> +			return sr;
> +	}
> +	return NULL;
> +}

[Severity: Medium]
Should qcom_snd_sdw_get_stream() also use sdw_find_dir_stream()?

qcom_swrm_get_sdw_stream() ignores its direction argument and returns
ctrl->sruntime[dai->id]:

drivers/soundwire/qcom.c:qcom_swrm_get_sdw_stream() {
	struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);

	return ctrl->sruntime[dai->id];
}

Because qcom_snd_sdw_get_stream() still iterates over all codec DAIs
without checking snd_soc_dai_stream_valid(codec_dai, substream->stream),
calling it on a multi_direction_codecs link can return the opposite
direction's sdw_stream_runtime when an opposite-direction SoundWire master
DAI appears earlier in the link's codec list.

[ ... ]

> @@ -49,43 +95,42 @@ static bool qcom_snd_is_sdw_dai(int id)
>  	return false;
>  }
>  
> +/*
> + * Allocate a stream runtime for one direction and set it on the codec DAIs
> + * of that direction.
> + */
> +static int qcom_snd_sdw_setup_dir(struct snd_soc_pcm_runtime *rtd, int dir)
> +{
> +	const char *cpu_name = snd_soc_rtd_to_cpu(rtd, 0)->name;
>  	struct sdw_stream_runtime *sruntime;
>  	struct snd_soc_dai *codec_dai;
> +	u32 rx_ch[SDW_MAX_PORTS], tx_ch[SDW_MAX_PORTS];
>  	u32 rx_ch_cnt = 0, tx_ch_cnt = 0;
>  	int ret, i, j;
> +	bool used = false;
>  
> +	for_each_rtd_codec_dais(rtd, i, codec_dai) {
> +		if (snd_soc_dai_stream_valid(codec_dai, dir)) {
> +			used = true;
> +			break;
> +		}
> +	}
> +	if (!used)
> +		return 0;
>  
> +	sruntime = qcom_snd_sdw_alloc_stream(cpu_name, dir);
>  	if (!sruntime)
>  		return -ENOMEM;
>  
>  	for_each_rtd_codec_dais(rtd, i, codec_dai) {
> +		if (!snd_soc_dai_stream_valid(codec_dai, dir))
> +			continue;
> +
> +		ret = snd_soc_dai_set_stream(codec_dai, sruntime, dir);
>  		if (ret < 0 && ret != -ENOTSUPP) {
>  			dev_err(rtd->dev, "Failed to set sdw stream on %s\n", codec_dai->name);
> +			goto err;
>  		} else if (ret == -ENOTSUPP) {
>  			continue;
>  		}

[Severity: High]
Can qcom_snd_sdw_setup_dir() leak sruntime and sruntime->name when none of
the codec DAIs in dir implement SoundWire stream callbacks?

used is set whenever any codec DAI on the link is stream-valid for dir,
so qcom_snd_sdw_alloc_stream() allocates sruntime before checking whether
those DAIs support .set_stream and .get_stream.  On links where the CPU DAI
matches qcom_snd_is_sdw_dai() (such as TX_CODEC_DMA_TX_3 or SLIMBUS_0_TX)
but the codec DAIs in dir all return -ENOTSUPP (or do not implement
.get_stream), qcom_snd_sdw_setup_dir() succeeds without storing sruntime in
any retrievable DAI.  At shutdown, sdw_find_dir_stream() returns NULL and
both sruntime and sruntime->name are leaked on every PCM open/close cycle.

[ ... ]

> @@ -187,9 +289,13 @@ EXPORT_SYMBOL_GPL(qcom_snd_sdw_get_stream);
>  
>  void qcom_snd_sdw_shutdown(struct snd_pcm_substream *substream)
>  {
> -	struct sdw_stream_runtime *sruntime = qcom_snd_sdw_get_stream(substream);
> +	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> +
> +	qcom_snd_sdw_release_stream(sdw_find_dir_stream(rtd, substream->stream));
>  
> -	sdw_release_stream(sruntime);
> +	if (rtd->dai_link->multi_direction_codecs)
> +		qcom_snd_sdw_release_stream(
> +			sdw_find_dir_stream(rtd, substream->stream ^ 1));
>  }
>  EXPORT_SYMBOL_GPL(qcom_snd_sdw_shutdown);

[Severity: Low]
Should qcom_snd_sdw_shutdown() still check qcom_snd_is_sdw_dai(cpu_dai->id)
before looking up and releasing streams?

Previously, qcom_snd_sdw_shutdown() went through qcom_snd_sdw_get_stream(),
which returned NULL when !qcom_snd_is_sdw_dai(cpu_dai->id).  Without that
check, qcom_snd_sdw_shutdown() now queries snd_soc_dai_get_stream() across
codec DAIs on non-SoundWire BE links in machine drivers that share this
shutdown callback across all DAI links.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122161437.5994-1-srinivas.kandagatla@oss.qualcomm.com?part=4

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

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23 17:14 [RFC PATCH 00/23] ASoC: qcom: AudioReach Active speaker protection for WSA884x Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 01/23] ASoC: soc-core: allow default ch_maps on links with more than 8 DAIs Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:04     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 02/23] ASoC: soc-pcm: add multi_direction_codecs link flag Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:04     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 03/23] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:04     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 04/23] ASoC: dt-bindings: qcom,wsa8840: allow one sound DAI cell Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 05/23] ASoC: codecs: wsa884x: add VI sense capture DAI Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:03     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 06/23] ASoC: codecs: wsa884x: register the die temperature as a thermal zone Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 07/23] ASoC: qcom: audioreach: add control link support to graphs Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 08/23] ASoC: qcom: audioreach: run VI module in calibration mode with per-speaker events Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:04     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 09/23] ASoC: qcom: q6apm-dai: add S32_LE format support Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 10/23] ASoC: qcom: audioreach: support up to four speakers in VI feedback Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 11/23] ASoC: qcom: audioreach: cache VI calibration results per speaker Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 12/23] ASoC: qcom: audioreach: make the speaker protection operation mode configurable Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 13/23] ASoC: qcom: audioreach: send cached R0/T0 to the VI module in normal mode Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 14/23] ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 15/23] ASoC: qcom: audioreach: add speaker protection mixer controls Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 16/23] ASoC: qcom: audioreach: pin SPEAKER_PROTECTION_VI media format Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 17/23] ASoC: qcom: audioreach: add live speaker temperature (T0) readback Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 18/23] ASoC: qcom: audioreach: add speaker protection calibration state control Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 19/23] ASoC: qcom: audioreach: keep the last VI calibration result until the DSP reports Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 20/23] ASoC: qcom: x1e80100: carry WSA VI feedback on the WSA Playback BE link Srinivas Kandagatla
2026-11-15 17:36   ` Mark Brown
2026-11-15 21:03     ` Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 21/23] ASoC: qcom: x1e80100: register WSA speaker thermal zones for speaker protection Srinivas Kandagatla
2026-10-23 17:14 ` [RFC PATCH 22/23] arm64: dts: qcom: glymur-crd: add WSA speaker protection VI feedback Srinivas Kandagatla
2026-10-28 13:38   ` Konrad Dybcio
2026-10-28 21:59     ` Srinivas Kandagatla
2026-10-29  8:17       ` Konrad Dybcio
2026-10-23 17:14 ` [RFC PATCH 23/23] arm64: dts: qcom: mahua-t14s: " Srinivas Kandagatla
2026-10-28 12:39 ` [RFC PATCH 00/23] ASoC: qcom: AudioReach Active speaker protection for WSA884x Mark Brown
2026-10-28 16:31 ` Fabian Renn-Giles
2026-10-28 21:56   ` Srinivas Kandagatla
2026-11-15 22:07     ` Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 00/24] ASoC: qcom: AudioReach Active speaker protection for WSA88xx Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 01/24] ASoC: soc-core: always allocate default ch_maps Srinivas Kandagatla
2026-11-22 16:31   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 02/24] ASoC: soc-pcm: add multi_direction_codecs link flag Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 03/24] ASoC: soc-dai: export snd_soc_dai_stream_valid Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 04/24] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links Srinivas Kandagatla
2026-11-22 16:38   ` sashiko-bot [this message]
2026-11-22 16:14 ` [PATCH v2 05/24] ASoC: dt-bindings: qcom,wsa8840: allow one sound DAI cell Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 06/24] ASoC: codecs: wsa884x: add VI sense capture DAI Srinivas Kandagatla
2026-11-22 16:28   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 07/24] ASoC: codecs: wsa883x: " Srinivas Kandagatla
2026-11-22 16:28   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 08/24] ASoC: codecs: wsa884x: register the die temperature as a thermal zone Srinivas Kandagatla
2026-11-22 16:28   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 09/24] ASoC: qcom: audioreach: add control link support to graphs Srinivas Kandagatla
2026-11-22 16:32   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 10/24] ASoC: qcom: audioreach: run VI module in calibration mode with per-speaker events Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 11/24] ASoC: qcom: q6apm-dai: add S32_LE format support Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 12/24] ASoC: qcom: audioreach: cache VI calibration results per speaker Srinivas Kandagatla
2026-11-22 16:22   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 13/24] ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure Srinivas Kandagatla
2026-11-22 16:31   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 14/24] ASoC: qcom: audioreach: send cached R0/T0 to the VI module in normal mode Srinivas Kandagatla
2026-11-22 16:28   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 15/24] ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules Srinivas Kandagatla
2026-11-22 16:31   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 16/24] ASoC: qcom: audioreach: add speaker protection mixer controls Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 17/24] ASoC: qcom: audioreach: pin SPEAKER_PROTECTION_VI media format Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 18/24] ASoC: qcom: audioreach: add live speaker temperature (T0) readback Srinivas Kandagatla
2026-11-22 16:37   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 19/24] ASoC: qcom: audioreach: add speaker protection calibration state control Srinivas Kandagatla
2026-11-22 16:37   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 20/24] ASoC: qcom: x1e80100: carry WSA VI feedback on the WSA Playback BE link Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 21/24] ASoC: qcom: x1e80100: register WSA speaker thermal zones for speaker protection Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 22/24] arm64: dts: qcom: glymur-crd: add WSA speaker protection VI feedback Srinivas Kandagatla
2026-11-22 16:39   ` sashiko-bot
2026-11-22 16:14 ` [PATCH v2 23/24] arm64: dts: qcom: mahua-t14s: " Srinivas Kandagatla
2026-11-22 16:14 ` [PATCH v2 24/24] arm64: dts: qcom: x1e78100-t14s: " Srinivas Kandagatla
2026-11-22 16:37   ` sashiko-bot

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-228113@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=srinivas.kandagatla@oss.qualcomm.com \
    /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