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 v5 15/24] ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules
Date: Fri, 04 Dec 2026 13:50:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-249902@kernel.org> (raw)
In-Reply-To: <20261204113101.1141644-16-srinivas.kandagatla@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] ASoC: qcom: audioreach: sp_cal_watchdog never canceled on
  module unload
- [Medium] ASoC: qcom: audioreach: OOB read in
  audioreach_speaker_protection_send_chunk()
- [Medium] ASoC: qcom: audioreach: audioreach_sp_cfg_walk() rejects last
  sub-word param
- [Medium] ASoC: qcom: audioreach: race in
  audioreach_sp_finish_calibration()
- [Medium] ASoC: qcom: audioreach: audioreach_set_media_format() leaks
  VI cal state
- [Low] ASoC: qcom: audioreach: raw cast drops __le32 in static cfg blob
  parser
--

commit 8b5c2a066041593c52f189b47c51b9e22ea4e028
Author: Ravi Hothi <ravi.hothi@oss.qualcomm.com>

ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules

This commit parses and sends topology static configuration blobs to the
speaker protection and VI sense modules in chunked APM_CMD_SET_CFG commands
padded to 8-byte boundaries, falling back to sending non-record blobs whole.

> diff --git a/sound/soc/qcom/qdsp6/audioreach.c b/sound/soc/qcom/qdsp6/audioreach.c
> index 3555c97c2000d..fd803d6ceccd0 100644
> --- a/sound/soc/qcom/qdsp6/audioreach.c
> +++ b/sound/soc/qcom/qdsp6/audioreach.c

[ ... ]

> +static int audioreach_sp_cfg_walk(const u32 *word, u32 num_words, u32 iid,
> +				  u32 first, u32 *next, u8 *dst, u32 max_bytes)
> +{
> +	u32 pos = first, bytes = 0;
> +
> +	while (pos < num_words) {
> +		u32 param_size, payload_words, avail, rec_bytes;
> +
> +		if (word[pos] != iid || num_words - pos < 4)
> +			return -EINVAL;
> +
> +		param_size = word[pos + 2];
> +		avail = (num_words - pos - 4) * sizeof(u32);
> +		if (param_size < sizeof(u32) && avail && word[pos + 4] == iid) {
> +			payload_words = 0;
> +		} else {
> +			if (param_size > avail)
> +				return -EINVAL;
> +			payload_words = DIV_ROUND_UP(param_size, sizeof(u32));
> +		}

[Severity: Medium]
Can a valid blob end with a sub-word parameter that omits its payload word?

When such a record is last in the blob (num_words - pos == 4), avail is 0,
so the if condition evaluates to false and the else branch returns -EINVAL
instead of setting payload_words to 0.  Checking (!avail || word[pos + 4] ==
iid) handles the final record while still guarding word[pos + 4].

[ ... ]

> +static int audioreach_speaker_protection_send_static_cfg(struct q6apm_graph *graph,
> +							 const struct audioreach_module *module,
> +							 const char *tag)
> +{
> +	u32 size, num_words, pos = 0;
> +	const u32 *word;
> +	int total, rc = 0, chunk = 0;
> +
> +	if (!module->data || !module->data->size)
> +		return 0;
> +
> +	size = le32_to_cpu(module->data->size);
> +	word = (const u32 *)module->data->data;
> +	num_words = size / sizeof(u32);

[Severity: Low]
Does this cast drop the __le32 annotation on module->data->data?

struct audioreach_module_priv_data declares data[] as __le32, so casting it
to const u32 * triggers sparse warnings and reads header words (word[0],
word[pos], word[pos + 2], and word[pos + 4]) in host byte order without
le32_to_cpu().  On big-endian builds, valid parameter-record blobs either
fall back to unchunked sends or fail with -EINVAL.  Keeping word as
const __le32 * and using le32_to_cpu() when inspecting header fields avoids
both.

> +	/* Not laid out as parameter records, so send it as the topology built it */
> +	if (size % sizeof(u32) || !num_words || word[0] != module->instance_id)
> +		return audioreach_speaker_protection_send_chunk(graph, tag, word, size, 0);

[Severity: Medium]
Can this read past the end of module->data when size is not a multiple of 4?

audioreach_get_module_priv_data() truncates array_size down to whole __le32
words when allocating and copying pdata->data[], while leaving the unrounded
byte count in pdata->size:

sound/soc/qcom/qdsp6/topology.c:audioreach_get_module_priv_data() {
    ...
	nwords = array_size / sizeof(__le32);
    ...
	pdata = kzalloc_flex(*pdata, data, nwords);
	if (!pdata)
		return ERR_PTR(-ENOMEM);

	memcpy(pdata, ((u8 *)private->data + sz),
	       struct_size(pdata, data, nwords));
    ...
}

Passing the unrounded size to audioreach_speaker_protection_send_chunk()
causes memcpy() to read 1 to 3 bytes past the end of the slab object,
triggering a KASAN slab-out-of-bounds report and sending adjacent heap bytes
to the DSP.

[ ... ]

> @@ -1989,6 +2126,8 @@ int audioreach_set_media_format(struct q6apm_graph *graph,
>  	case MODULE_ID_SPEAKER_PROTECTION_VI:
>  		rc = audioreach_speaker_protection_vi(graph, module, cfg,
>  						      READ_ONCE(sp_operation_mode));
> +		if (!rc)
> +			rc = audioreach_speaker_protection_vi_static_cfg(graph, module);
>  		if (!rc)
>  			rc = audioreach_module_enable(graph, module, true);

[Severity: Medium]
If audioreach_speaker_protection_vi_static_cfg() fails during calibration
mode, does this leak the armed calibration state?

audioreach_speaker_protection_vi() has already called
audioreach_arm_vi_calibration() and registered DSP events before returning 0.
Returning on a static cfg error leaves sp_operation_mode stuck in
PARAM_ID_SP_VI_OP_MODE_CALIBRATION for up to 60 seconds (causing calibration
retries to fail with -EBUSY and configuring subsequent normal streams into
calibration mode) and leaves sp_vi_r0.armed set.

Even with the later graph-close abort path in the series,
audioreach_vi_calibration_abort() returns early when sp_vi_r0.seen_states is
all zeros, which is always true when prepare fails before the module is
enabled.  Unwinding calibration state on failure here (or sending the static
cfg blob before arming calibration) would avoid the leak.

As a minor side effect in normal mode, audioreach_speaker_protection_vi()
also logs "SP active in Normal mode" before the static cfg blob is validated
or the module is enabled.

[Severity: High]
In the surrounding speaker protection calibration state from commit "ASoC:
qcom: audioreach: send cached R0/T0 to the VI module in normal mode", is
sp_cal_watchdog ever canceled on module unload?

audioreach_trigger_sp_calibration() schedules the 60-second delayed work
sp_cal_watchdog without pinning snd-q6apm, while audioreach_sp_shutdown()
and its call from apm_remove() were dropped (leaving only the declaration in
audioreach.h).  Unloading snd-q6apm while the watchdog is pending or running
causes a use-after-free when the timer fires into freed module text and
data.  Restoring cancel_delayed_work_sync(&sp_cal_watchdog) on removal after
unregistering controls prevents this.

[Severity: Medium]
Also in commit "ASoC: qcom: audioreach: send cached R0/T0 to the VI module
in normal mode", can audioreach_sp_finish_calibration() race with a new
calibration trigger or an in-flight watchdog callback?

audioreach_sp_finish_calibration() clears sp_operation_mode before calling
non-synchronous cancel_delayed_work(&sp_cal_watchdog):

CPU0                              CPU1
audioreach_sp_finish_calibration()
  WRITE_ONCE(sp_operation_mode,
             NORMAL)
                                  audioreach_trigger_sp_calibration()
                                    cmpxchg(..., NORMAL, CALIBRATION)
                                    schedule_delayed_work(...)
  cancel_delayed_work(...)        <-- cancels CPU1's watchdog

This leaves the new run stuck in PARAM_ID_SP_VI_OP_MODE_CALIBRATION
indefinitely if no playback completes.  Because cancel_delayed_work() does
not wait for an executing callback, a stale audioreach_sp_cal_watchdog_fn()
can also run its cmpxchg after a new calibration starts and abort it back to
Normal mode.  Canceling the work synchronously before clearing
sp_operation_mode closes both races, provided sp_vi_r0_lock is not held
across cancel_delayed_work_sync() if the callback ever acquires it.

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

  reply	other threads:[~2026-12-04 13:50 UTC|newest]

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