All of lore.kernel.org
 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 v4 04/24] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links
Date: Tue, 01 Dec 2026 13:19:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-243376@kernel.org> (raw)
In-Reply-To: <20261201124753.196748-5-srinivas.kandagatla@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ASoC: qcom: sdw: stream runtime and name leak in
  qcom_snd_sdw_setup_dir()
- [Medium] ASoC: qcom: sdw: prepare failure after hw_free sets
  SDW_STREAM_RELEASED
- [Low] ASoC: qcom: sdw: un-updated qcom_snd_sdw_get_stream() and
  startup kernel-doc
--

commit 10caeb868a9089ee3f73cb421335ae8e657200ee
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 helpers so that links with
multi_direction_codecs allocate and manage separate stream runtimes for
playback and capture.

> diff --git a/sound/soc/qcom/sdw.c b/sound/soc/qcom/sdw.c
> index 6576b47a4c8c3..82f1c47a788cb 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);
> +}
> +
> +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;
> +}

[ ... ]

> @@ -49,43 +95,42 @@ static bool qcom_snd_is_sdw_dai(int id)
>  	return false;
>  }
>  
> -/**
> - * qcom_snd_sdw_startup() - Helper to start Soundwire stream for SoC audio card
> - * @substream: The PCM substream from audio, as passed to snd_soc_ops->startup()
> - *
> - * Helper for the SoC audio card (snd_soc_ops->startup()) to allocate and set
> - * Soundwire stream runtime to each codec DAI.
> - *
> - * The shutdown() callback should call sdw_release_stream() on the same
> - * sdw_stream_runtime.
> - *
> - * Return: 0 or errno
> +/*
> + * Allocate a stream runtime for one direction and set it on the codec DAIs
> + * of that direction.
>   */
> -int qcom_snd_sdw_startup(struct snd_pcm_substream *substream)
> +static int qcom_snd_sdw_setup_dir(struct snd_soc_pcm_runtime *rtd, int dir)
>  {
> -	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> -	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
> -	u32 rx_ch[SDW_MAX_PORTS], tx_ch[SDW_MAX_PORTS];
> +	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;
>  
> -	if (!qcom_snd_is_sdw_dai(cpu_dai->id))
> +	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 = sdw_alloc_stream(cpu_dai->name, SDW_STREAM_PCM);
> +	sruntime = qcom_snd_sdw_alloc_stream(cpu_name, dir);
>  	if (!sruntime)
>  		return -ENOMEM;
>  
>  	for_each_rtd_codec_dais(rtd, i, codec_dai) {
> -		ret = snd_soc_dai_set_stream(codec_dai, sruntime,
> -					     substream->stream);
> +		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_set_stream;
> +			goto err;
>  		} else if (ret == -ENOTSUPP) {
> -			/* Ignore unsupported */
>  			continue;
>  		}

[Severity: High]
Does qcom_snd_sdw_setup_dir() leak sruntime and sruntime->name when no
stream-valid codec DAI in dir implements .get_stream?

qcom_snd_sdw_setup_dir() allocates sruntime whenever any codec DAI is
stream-valid for dir, skips codec DAIs that are not stream-valid for dir,
and ignores -ENOTSUPP from snd_soc_dai_set_stream().  At shutdown,
sdw_find_dir_stream() only queries codec DAIs that are stream-valid for
dir via snd_soc_dai_get_stream().  On Qualcomm platforms, only the
qcom_swrm controller DAI implements .get_stream, while slave codecs only
implement .set_stream and LPASS macro or SLIMbus codec DAIs implement
neither.

If a link has no stream-valid qcom_swrm DAI in dir (for example,
va-dai-link using TX_CODEC_DMA_TX_3 with lpass_vamacro where all codec
DAIs return -ENOTSUPP, or WCD capture links referencing <&swr2 0> when
swr2 has qcom,dout-ports = <1> so qcom_swrm_register_dais() registers DAI
0 as playback-only), sdw_find_dir_stream() returns NULL and every PCM
open/close cycle leaks sruntime and sruntime->name (and on <&swr2 0>,
skipping the controller DAI also leaves its stream unset).

> @@ -93,59 +138,106 @@ int qcom_snd_sdw_startup(struct snd_pcm_substream *substream)

[ ... ]

> +/**
> + * qcom_snd_sdw_startup() - Helper to start Soundwire stream for SoC audio card
> + * @substream: The PCM substream from audio, as passed to snd_soc_ops->startup()
> + *
> + * Helper for the SoC audio card (snd_soc_ops->startup()) to allocate and set
> + * Soundwire stream runtime to each codec DAI.
> + *
> + * The shutdown() callback should call sdw_release_stream() on the same
> + * sdw_stream_runtime.
> + *
> + * Return: 0 or errno
> + */
> +int qcom_snd_sdw_startup(struct snd_pcm_substream *substream)

[Severity: Low]
This isn't a bug in the active callers since this patch replaces all
in-tree calls to qcom_snd_sdw_get_stream() with sdw_find_dir_stream(),
leaving qcom_snd_sdw_get_stream() unused, but qcom_snd_sdw_get_stream()
still open-codes the codec DAI loop without checking
snd_soc_dai_stream_valid().  On a multi_direction_codecs link, calling
qcom_snd_sdw_get_stream() can return the opposite direction's stream
runtime because qcom_swrm_get_sdw_stream() ignores its direction
argument.

Also, the kernel-doc comment on qcom_snd_sdw_startup() still says that
shutdown() should call sdw_release_stream(), which would leak the
kasprintf()-allocated sr->name and any opposite-direction stream runtime
instead of releasing them via qcom_snd_sdw_shutdown().

[ ... ]

> @@ -153,15 +245,32 @@ int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
>  	 * accumulated resulting in Click/Pop Noise
>  	 * PA enable/mute are handled as part of codec DAPM and digital mute.
>  	 */
> -
> -	ret = sdw_enable_stream(sruntime);
> -	if (ret) {
> -		sdw_deprepare_stream(sruntime);
> +	ret = qcom_snd_sdw_prepare_dir(rtd, substream->stream);
> +	if (ret)
>  		return ret;
> +
> +	if (rtd->dai_link->multi_direction_codecs) {
> +		struct sdw_stream_runtime *sr_opp =
> +			sdw_find_dir_stream(rtd, substream->stream ^ 1);
> +
> +		/* Skip if no slave registered at hw_params (e.g. VI off) */
> +		if (sr_opp && sr_opp->state != SDW_STREAM_ALLOCATED) {
> +			ret = qcom_snd_sdw_prepare_dir(rtd,
> +						       substream->stream ^ 1);
> +			if (ret) {
> +				struct sdw_stream_runtime *sr =
> +					sdw_find_dir_stream(rtd, substream->stream);
> +				if (sr) {
> +					sdw_disable_stream(sr);
> +					sdw_deprepare_stream(sr);
> +				}
> +				return ret;
> +			}
> +		}
>  	}
> -	*stream_prepared  = true;
>  
> -	return ret;
> +	*stream_prepared = true;
> +	return 0;
>  }
>  EXPORT_SYMBOL_GPL(qcom_snd_sdw_prepare);

[ ... ]

> @@ -197,18 +310,29 @@ int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream, bool *stream_prepa
>  {
>  	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
>  	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
> -	struct sdw_stream_runtime *sruntime;
> +	struct sdw_stream_runtime *sr;
>  
>  	if (!qcom_snd_is_sdw_dai(cpu_dai->id))
>  		return 0;
>  
> -	sruntime = qcom_snd_sdw_get_stream(substream);
> -	if (sruntime && *stream_prepared) {
> -		sdw_disable_stream(sruntime);
> -		sdw_deprepare_stream(sruntime);
> -		*stream_prepared = false;
> +	if (!*stream_prepared)
> +		return 0;
> +
> +	sr = sdw_find_dir_stream(rtd, substream->stream);
> +	if (sr) {
> +		sdw_disable_stream(sr);
> +		sdw_deprepare_stream(sr);
> +	}
> +
> +	if (rtd->dai_link->multi_direction_codecs) {
> +		sr = sdw_find_dir_stream(rtd, substream->stream ^ 1);
> +		if (sr && sr->state != SDW_STREAM_ALLOCATED) {
> +			sdw_disable_stream(sr);
> +			sdw_deprepare_stream(sr);
> +		}
>  	}
>  
> +	*stream_prepared = false;
>  	return 0;
>  }

[Severity: Medium]
Can qcom_snd_sdw_prepare() fail with -EINVAL on a second
hw_params/prepare cycle of an open substream when the opposite-direction
slave is not enabled (such as when VI sense is off)?

During hw_params on a multi_direction_codecs link, even if the
opposite-direction slave codec DAI does not call sdw_stream_add_slave()
(leaving sr_opp->state unchanged), the SoundWire master DAI still calls
qcom_swrm_hw_params() -> sdw_stream_add_master() on sr_opp.  When
hw_free subsequently runs on that substream, qcom_swrm_hw_free() calls
sdw_stream_remove_master(), which sets sr_opp->state to
SDW_STREAM_RELEASED:

drivers/soundwire/stream.c:sdw_stream_remove_master() {
	...
	if (list_empty(&stream->master_list))
		stream->state = SDW_STREAM_RELEASED;
	...
}

On a subsequent hw_params and prepare cycle on the same open substream
with VI sense still off, sr_opp->state remains SDW_STREAM_RELEASED.
Because qcom_snd_sdw_prepare() and qcom_snd_sdw_hw_free() only check
state != SDW_STREAM_ALLOCATED, qcom_snd_sdw_prepare() calls
sdw_prepare_stream(sr_opp) in SDW_STREAM_RELEASED state, which returns
-EINVAL and tears down the primary stream, and qcom_snd_sdw_hw_free()
similarly calls sdw_disable_stream() and sdw_deprepare_stream() on a
stream in SDW_STREAM_RELEASED state.

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

  reply	other threads:[~2026-12-01 13:19 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:47 [PATCH v4 00/24] ASoC: qcom: AudioReach Active speaker protection for WSA88xx Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 01/24] ASoC: soc-core: bump MAX_DEFAULT_CH_MAP_SIZE to 18 Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 02/24] ASoC: soc-pcm: add multi_direction_codecs link flag Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 03/24] ASoC: soc-dai: export snd_soc_dai_stream_valid Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 04/24] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links Srinivas Kandagatla
2026-12-01 13:19   ` sashiko-bot [this message]
2026-12-01 12:47 ` [PATCH v4 05/24] ASoC: dt-bindings: qcom,wsa8840: allow one sound DAI cell Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 06/24] ASoC: codecs: wsa884x: add VI sense capture DAI Srinivas Kandagatla
2026-12-01 13:03   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 07/24] ASoC: codecs: wsa883x: " Srinivas Kandagatla
2026-12-01 13:03   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 08/24] ASoC: codecs: wsa884x: register the die temperature as a thermal zone Srinivas Kandagatla
2026-12-01 13:13   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 09/24] ASoC: qcom: audioreach: add control link support to graphs Srinivas Kandagatla
2026-12-01 13:09   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 10/24] ASoC: qcom: audioreach: run VI module in calibration mode with per-speaker events Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 11/24] ASoC: qcom: q6apm-dai: add S32_LE format support Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 12/24] ASoC: qcom: audioreach: cache VI calibration results per speaker Srinivas Kandagatla
2026-12-01 13:10   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 13/24] ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure Srinivas Kandagatla
2026-12-01 13:12   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 14/24] ASoC: qcom: audioreach: send cached R0/T0 to the VI module in normal mode Srinivas Kandagatla
2026-12-01 13:16   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 15/24] ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules Srinivas Kandagatla
2026-12-01 13:16   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 16/24] ASoC: qcom: audioreach: add speaker protection mixer controls Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 17/24] ASoC: qcom: audioreach: pin SPEAKER_PROTECTION_VI media format Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 18/24] ASoC: qcom: audioreach: add live speaker temperature (T0) readback Srinivas Kandagatla
2026-12-01 13:23   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 19/24] ASoC: qcom: audioreach: add speaker protection calibration state control Srinivas Kandagatla
2026-12-01 13:27   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 20/24] ASoC: qcom: x1e80100: carry WSA VI feedback on the WSA Playback BE link Srinivas Kandagatla
2026-12-01 12:47 ` [PATCH v4 21/24] ASoC: qcom: x1e80100: register WSA speaker thermal zones for speaker protection Srinivas Kandagatla
2026-12-01 13:24   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 22/24] arm64: dts: qcom: glymur-crd: add WSA speaker protection VI feedback Srinivas Kandagatla
2026-12-01 13:31   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 23/24] arm64: dts: qcom: mahua-t14s: " Srinivas Kandagatla
2026-12-01 13:30   ` sashiko-bot
2026-12-01 12:47 ` [PATCH v4 24/24] arm64: dts: qcom: x1e78100-t14s: " Srinivas Kandagatla
2026-12-01 13:27   ` 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-243376@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.